User.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\admin\v1\user;
  12. use app\jobs\BatchHandleJob;
  13. use app\services\other\queue\QueueServices;
  14. use app\services\product\product\StoreProductLogServices;
  15. use app\services\user\group\UserGroupServices;
  16. use app\services\user\label\UserLabelServices;
  17. use app\services\user\UserBatchProcessServices;
  18. use app\services\user\UserServices;
  19. use app\controller\admin\AuthController;
  20. use app\services\user\UserSpreadServices;
  21. use app\services\user\UserWechatuserServices;
  22. use think\exception\ValidateException;
  23. use think\facade\App;
  24. class User extends AuthController
  25. {
  26. /**
  27. * user constructor.
  28. * @param App $app
  29. * @param UserServices $services
  30. */
  31. public function __construct(App $app, UserServices $services)
  32. {
  33. parent::__construct($app);
  34. $this->services = $services;
  35. }
  36. /**
  37. * 显示资源列表头部
  38. *
  39. * @return \think\Response
  40. */
  41. public function type_header()
  42. {
  43. $list = $this->services->typeHead();
  44. return $this->success(compact('list'));
  45. }
  46. /**
  47. * 显示资源列表
  48. *
  49. * @return \think\Response
  50. */
  51. public function index()
  52. {
  53. $where = $this->request->getMore([
  54. ['page', 1],
  55. ['limit', 20],
  56. ['nickname', ''],
  57. ['status', ''],
  58. ['pay_count', ''],
  59. ['is_promoter', ''],
  60. ['order', ''],
  61. ['data', ''],
  62. ['user_type', ''],
  63. ['country', ''],
  64. ['province', ''],
  65. ['city', ''],
  66. ['user_time_type', ''],
  67. ['user_time', ''],
  68. ['sex', ''],
  69. [['level', 0], 0],
  70. [['group_id', 'd'], 0],
  71. [['label_id', 'd'], 0],
  72. ['now_money', 'normal'],
  73. ['field_key', ''],
  74. ['isMember', ''],
  75. ['label_ids', '']
  76. ]);
  77. if ($where['label_ids']) {
  78. $where['label_id'] = explode(',', $where['label_ids']);
  79. unset($where['label_ids']);
  80. }
  81. $where['user_time_type'] = $where['user_time_type'] == 'all' ? '' : $where['user_time_type'];
  82. return $this->success($this->services->index($where));
  83. }
  84. /**
  85. * 补充信息表单
  86. * @param $id
  87. * @return mixed
  88. * @throws \FormBuilder\Exception\FormBuilderException
  89. */
  90. public function extendInfoForm($id)
  91. {
  92. return $this->success($this->services->extendInfoForm((int)$id));
  93. }
  94. /**
  95. * 保存用户补充信息
  96. * @param $id
  97. * @return mixed
  98. */
  99. public function saveExtendForm($id)
  100. {
  101. $data = $this->request->post();
  102. if (!$data) {
  103. return $this->fail('请提交要保存信息');
  104. }
  105. $this->services->saveExtendForm((int)$id, $data);
  106. return $this->success('保存成功');
  107. }
  108. /**
  109. * 显示创建资源表单页.
  110. *
  111. * @return \think\Response
  112. */
  113. public function create()
  114. {
  115. return $this->success($this->services->saveForm());
  116. }
  117. /**
  118. * 保存新建的资源
  119. *
  120. * @param \think\Request $request
  121. * @return \think\Response
  122. */
  123. public function save()
  124. {
  125. $data = $this->request->postMore([
  126. ['is_promoter', 0],
  127. ['real_name', ''],
  128. ['card_id', ''],
  129. ['birthday', ''],
  130. ['mark', ''],
  131. ['status', 0],
  132. ['level', 0],
  133. ['phone', 0],
  134. ['addres', ''],
  135. ['label_id', []],
  136. ['group_id', 0],
  137. ['pwd', ''],
  138. ['true_pwd', ''],
  139. ['spread_open', 1],
  140. ['sex', 0],
  141. ['provincials', ''],
  142. ['province', 0],
  143. ['city', 0],
  144. ['area', 0],
  145. ['street', 0],
  146. ['extend_info', []],
  147. ]);
  148. $this->validate(['pwd' => $data['pwd']], \app\validate\admin\user\UserValidate::class);
  149. if ($data['phone']) {
  150. if (!check_phone($data['phone'])) {
  151. return $this->fail('手机号码格式不正确');
  152. }
  153. if ($this->services->count(['phone' => $data['phone']])) {
  154. return $this->fail('手机号已经存在不能添加相同的手机号用户');
  155. }
  156. $data['nickname'] = substr_replace($data['phone'], '****', 3, 4);
  157. }
  158. if ($data['card_id']) {
  159. try {
  160. if (!check_card($data['card_id'])) return $this->fail('请输入正确的身份证');
  161. } catch (\Throwable $e) {
  162. // return $this->fail('请输入正确的身份证');
  163. }
  164. }
  165. if ($data['birthday']) {
  166. if (strtotime($data['birthday']) > time()) return $this->fail('生日请选择今天之前日期');
  167. }
  168. if ($data['pwd']) {
  169. if (!$data['true_pwd']) {
  170. return $this->fail('请输入确认密码');
  171. }
  172. if ($data['pwd'] != $data['true_pwd']) {
  173. return $this->fail('两次输入的密码不一致');
  174. }
  175. if ($data['pwd'] == '123456') {
  176. return $this->fail('您设置的密码太过简单');
  177. }
  178. $data['pwd'] = md5($data['pwd']);
  179. } else {
  180. unset($data['pwd']);
  181. }
  182. unset($data['true_pwd']);
  183. $data['avatar'] = sys_config('h5_avatar');
  184. $data['adminId'] = $this->adminId;
  185. $data['user_type'] = 'h5';
  186. $labels = $data['label_id'];
  187. unset($data['label_id']);
  188. foreach ($labels as $k => $v) {
  189. if (!$v) {
  190. unset($labels[$k]);
  191. }
  192. }
  193. $data['birthday'] = empty($data['birthday']) ? 0 : strtotime($data['birthday']);
  194. $data['add_time'] = time();
  195. $data['extend_info'] = $this->services->handelExtendInfo($data['extend_info']);
  196. $this->services->transaction(function () use ($data, $labels) {
  197. $res = true;
  198. $userInfo = $this->services->save($data);
  199. $res = $this->services->saveSetLabel([$userInfo->uid], $labels);
  200. if ($data['level']) {
  201. $res = $this->services->saveGiveLevel((int)$userInfo->uid, (int)$data['level']);
  202. }
  203. if (!$res) {
  204. throw new ValidateException('保存添加用户失败');
  205. }
  206. event('user.register', [$this->services->get((int)$userInfo->uid), true, 0]);
  207. });
  208. event('user.create', $data);
  209. return $this->success('添加成功');
  210. }
  211. /**
  212. * 显示指定的资源
  213. *
  214. * @param int $id
  215. * @return \think\Response
  216. */
  217. public function read($id)
  218. {
  219. if (is_string($id)) {
  220. $id = (int)$id;
  221. }
  222. return $this->success($this->services->read($id));
  223. }
  224. /**
  225. * 赠送会员等级
  226. * @param int $uid
  227. * @return mixed
  228. * */
  229. public function give_level($id)
  230. {
  231. if (!$id) return $this->fail('缺少参数');
  232. return $this->success($this->services->giveLevel((int)$id));
  233. }
  234. /**
  235. * 执行赠送会员等级
  236. * @param int $uid
  237. * @return mixed
  238. * */
  239. public function save_give_level($id)
  240. {
  241. if (!$id) return $this->fail('缺少参数');
  242. [$level_id] = $this->request->postMore([
  243. ['level_id', 0],
  244. ], true);
  245. return $this->success($this->services->saveGiveLevel((int)$id, (int)$level_id) ? '赠送成功' : '赠送失败');
  246. }
  247. /**
  248. * 赠送付费会员时长
  249. * @param int $uid
  250. * @return mixed
  251. * */
  252. public function give_level_time($id)
  253. {
  254. if (!$id) return $this->fail('缺少参数');
  255. return $this->success($this->services->giveLevelTime((int)$id));
  256. }
  257. /**
  258. * 执行赠送付费会员时长
  259. * @param int $uid
  260. * @return mixed
  261. * */
  262. public function save_give_level_time($id)
  263. {
  264. if (!$id) return $this->fail('缺少参数');
  265. [$days_status, $days] = $this->request->postMore([
  266. ['days_status', 1],
  267. ['days', 0],
  268. ], true);
  269. return $this->success($this->services->saveGiveLevelTime((int)$id, (int)$days, (int)$days_status) ? '赠送成功' : '赠送失败');
  270. }
  271. /**
  272. * 清除会员等级
  273. * @param int $uid
  274. * @return json
  275. */
  276. public function del_level($id)
  277. {
  278. if (!$id) return $this->fail('缺少参数');
  279. return $this->success($this->services->cleanUpLevel((int)$id) ? '清除成功' : '清除失败');
  280. }
  281. /**
  282. * 设置会员分组
  283. * @param $id
  284. * @return mixed
  285. * @throws \FormBuilder\Exception\FormBuilderException
  286. * @throws \think\db\exception\DataNotFoundException
  287. * @throws \think\db\exception\DbException
  288. * @throws \think\db\exception\ModelNotFoundException
  289. */
  290. public function set_group()
  291. {
  292. [$uids, $all, $where] = $this->request->postMore([
  293. ['uids', []],
  294. ['all', 0],
  295. ['where', []],
  296. ], true);
  297. return $this->success($this->services->setGroup($uids, $all, $where));
  298. }
  299. /**
  300. * 保存会员分组
  301. * @param $id
  302. * @return mixed
  303. */
  304. public function save_set_group()
  305. {
  306. [$group_id, $uids, $all, $where] = $this->request->postMore([
  307. ['group_id', 0],
  308. ['uids', ''],
  309. ['all', 0],
  310. ['where', ""],
  311. ], true);
  312. if (!$uids && $all == 0) return $this->fail('缺少参数');
  313. if (!$group_id) return $this->fail('请选择分组');
  314. $type = 2;//代表设置用户标签
  315. if ($all == 0) {
  316. $uids = explode(',', $uids);
  317. $where = [];
  318. }
  319. if ($all == 1) {
  320. $where = $where ? json_decode($where, true) : [];
  321. /** @var UserWechatuserServices $userWechatUser */
  322. $userWechatUser = app()->make(UserWechatuserServices::class);
  323. $fields = 'u.uid';
  324. [$list, $count] = $userWechatUser->getWhereUserList($where, $fields);
  325. $uids = array_unique(array_column($list, 'uid'));
  326. $where = [];
  327. }
  328. /** @var UserGroupServices $userGroup */
  329. $userGroup = app()->make(UserGroupServices::class);
  330. if (!$userGroup->getGroup($group_id)) {
  331. return $this->fail('该分组不存在');
  332. }
  333. /** @var QueueServices $queueService */
  334. $queueService = app()->make(QueueServices::class);
  335. $queueService->setQueueData($where, 'uid', $uids, $type, $group_id);
  336. //加入队列
  337. BatchHandleJob::dispatch([$group_id, $type]);
  338. return $this->success('后台程序已执行用户分组任务!');
  339. }
  340. /**
  341. * 设置用户标签
  342. * @return mixed
  343. * @throws \FormBuilder\Exception\FormBuilderException
  344. * @throws \think\db\exception\DataNotFoundException
  345. * @throws \think\db\exception\DbException
  346. * @throws \think\db\exception\ModelNotFoundException
  347. */
  348. public function set_label()
  349. {
  350. [$uids, $all, $where] = $this->request->postMore([
  351. ['uids', []],
  352. ['all', 0],
  353. ['where', ""],
  354. ], true);
  355. return $this->success($this->services->setLabel($uids, $all, $where));
  356. }
  357. /**
  358. * 保存用户标签
  359. * @return mixed
  360. */
  361. public function save_set_label()
  362. {
  363. [$lables, $uids, $all, $where] = $this->request->postMore([
  364. ['label_id', []],
  365. ['uids', ''],
  366. ['all', 0],
  367. ['where', ""],
  368. ], true);
  369. if (!$uids && $all == 0) return $this->fail('缺少参数');
  370. if (!$lables) return $this->fail('请选择标签');
  371. if ($all == 0) {
  372. $uids = is_array($uids) ? $uids : explode(',', $uids);
  373. $where = [];
  374. }
  375. if ($all == 1) {
  376. $where = $where ? (is_string($where) ? json_decode($where, true) : $where) : [];
  377. /** @var UserWechatuserServices $userWechatUser */
  378. $userWechatUser = app()->make(UserWechatuserServices::class);
  379. $fields = 'u.uid';
  380. [$list, $count] = $userWechatUser->getWhereUserList($where, $fields);
  381. $uids = array_unique(array_column($list, 'uid'));
  382. $where = [];
  383. }
  384. /** @var UserLabelServices $userLabelServices */
  385. $userLabelServices = app()->make(UserLabelServices::class);
  386. $count = $userLabelServices->getCount([['id', 'IN', $lables]]);
  387. if ($count != count($lables)) {
  388. return app('json')->fail('用户标签不存在或被删除');
  389. }
  390. $type = 3;//批量设置用户标签
  391. /** @var QueueServices $queueService */
  392. $queueService = app()->make(QueueServices::class);
  393. $queueService->setQueueData($where, 'uid', $uids, $type, $lables);
  394. //加入队列
  395. BatchHandleJob::dispatch([$lables, $type]);
  396. return $this->success('后台程序已执行批量设置用户标签任务!');
  397. }
  398. /**
  399. * 编辑其他
  400. * @param $id
  401. * @return mixed
  402. * @throws \FormBuilder\Exception\FormBuilderException
  403. */
  404. public function edit_other($id)
  405. {
  406. if (!$id) return $this->fail('数据不存在');
  407. return $this->success($this->services->editOther((int)$id));
  408. }
  409. /**
  410. * 执行编辑其他
  411. * @param int $id
  412. * @return mixed
  413. */
  414. public function update_other($id)
  415. {
  416. $data = $this->request->postMore([
  417. ['money_status', 0],
  418. ['money', 0],
  419. ['integration_status', 0],
  420. ['integration', 0],
  421. ]);
  422. if (!$id) return $this->fail('数据不存在');
  423. $data['adminId'] = $this->adminId;
  424. $data['money'] = (string)$data['money'];
  425. $data['integration'] = (string)$data['integration'];
  426. $data['is_other'] = true;
  427. return $this->success($this->services->updateInfo($id, $data) ? '修改成功' : '修改失败');
  428. }
  429. /**
  430. * 修改user表状态
  431. *
  432. * @return array
  433. */
  434. public function set_status($status, $id)
  435. {
  436. // if ($status == '' || $id == 0) return $this->fail('参数错误');
  437. // UserModel::where(['uid' => $id])->update(['status' => $status]);
  438. return $this->success($status == 0 ? '禁用成功' : '解禁成功');
  439. }
  440. /**
  441. * 编辑会员信息
  442. * @param $id
  443. * @return mixed|\think\response\Json|void
  444. */
  445. public function edit($id)
  446. {
  447. if (!$id) return $this->fail('数据不存在');
  448. return $this->success($this->services->edit($id));
  449. }
  450. public function update($id)
  451. {
  452. $data = $this->request->postMore([
  453. ['money_status', 0],
  454. ['is_promoter', 0],
  455. ['real_name', ''],
  456. ['card_id', ''],
  457. ['birthday', ''],
  458. ['mark', ''],
  459. ['money', 0],
  460. ['integration_status', 0],
  461. ['integration', 0],
  462. ['status', 0],
  463. ['level', 0],
  464. ['phone', 0],
  465. ['addres', ''],
  466. ['label_id', []],
  467. ['group_id', 0],
  468. ['pwd', ''],
  469. ['true_pwd'],
  470. ['spread_open', 1],
  471. ['sex', 0],
  472. ['provincials', ''],
  473. ['province', 0],
  474. ['city', 0],
  475. ['area', 0],
  476. ['street', 0],
  477. ['spread_uid', -1],
  478. ['extend_info', []]
  479. ]);
  480. if ($data['phone']) {
  481. if (!check_phone($data['phone'])) return $this->fail('手机号码格式不正确');
  482. }
  483. if ($data['card_id']) {
  484. try {
  485. if (!check_card($data['card_id'])) return $this->fail('请输入正确的身份证');
  486. } catch (\Throwable $e) {
  487. // return $this->fail('请输入正确的身份证');
  488. }
  489. }
  490. if ($data['birthday']) {
  491. if (strtotime($data['birthday']) > time()) return $this->fail('生日请选择今天之前日期');
  492. }
  493. if ($data['pwd']) {
  494. if (!$data['true_pwd']) {
  495. return $this->fail('请输入确认密码');
  496. }
  497. if ($data['pwd'] != $data['true_pwd']) {
  498. return $this->fail('两次输入的密码不一致');
  499. }
  500. if ($data['pwd'] == '123456') {
  501. return $this->fail('您设置的密码太过简单');
  502. }
  503. $this->validate(['pwd' => $data['pwd']], \app\validate\admin\user\UserValidate::class);
  504. $data['pwd'] = md5($data['pwd']);
  505. } else {
  506. unset($data['pwd']);
  507. }
  508. $userInfo = $this->services->get($id);
  509. if (!$userInfo) {
  510. return $this->fail('用户不存在');
  511. }
  512. if (!in_array($data['spread_uid'], [0, -1])) {
  513. $spreadUid = $data['spread_uid'];
  514. if ($id == $spreadUid) {
  515. return $this->fail('上级推广人不能为自己');
  516. }
  517. if (!$this->services->count(['uid' => $spreadUid])) {
  518. return $this->fail('上级用户不存在');
  519. }
  520. $spreadInfo = $this->services->get($spreadUid);
  521. if ($spreadInfo->spread_uid == $id) {
  522. return $this->fail('上级推广人不能为自己下级');
  523. }
  524. }
  525. unset($data['true_pwd']);
  526. if (!$id) return $this->fail('数据不存在');
  527. $data['adminId'] = $this->adminId;
  528. $data['money'] = (string)$data['money'];
  529. $data['integration'] = (string)$data['integration'];
  530. if ($data['extend_info']) {
  531. $data['extend_info'] = $this->services->handelExtendInfo($data['extend_info']);
  532. }
  533. return $this->success($this->services->updateInfo((int)$id, $data) ? '修改成功' : '修改失败');
  534. }
  535. /**
  536. * 获取单个用户信息
  537. * @param $id 用户id
  538. * @return mixed
  539. */
  540. public function oneUserInfo($id)
  541. {
  542. $data = $this->request->getMore([
  543. ['type', ''],
  544. ]);
  545. $id = (int)$id;
  546. if ($data['type'] == '') return $this->fail('缺少参数');
  547. return $this->success($this->services->oneUserInfo($id, $data['type']));
  548. }
  549. /**
  550. * 同步微信粉丝用户
  551. * @return mixed
  552. */
  553. public function syncWechatUsers()
  554. {
  555. $this->services->syncWechatUsers();
  556. return $this->success('加入消息队列成功,正在异步执行中');
  557. }
  558. /**
  559. * 商品浏览记录
  560. * @param $id
  561. * @param StoreProductLogServices $services
  562. * @return mixed
  563. * @throws \think\db\exception\DataNotFoundException
  564. * @throws \think\db\exception\DbException
  565. * @throws \think\db\exception\ModelNotFoundException
  566. */
  567. public function visitList($id, StoreProductLogServices $services)
  568. {
  569. $where['uid'] = (int)$id;
  570. $where['type'] = 'visit';
  571. return app('json')->success($services->getList($where, 'product_id'));
  572. }
  573. /**
  574. * 获取推广人记录
  575. * @param $id
  576. * @param UserSpreadServices $services
  577. * @return mixed
  578. * @throws \think\db\exception\DataNotFoundException
  579. * @throws \think\db\exception\DbException
  580. * @throws \think\db\exception\ModelNotFoundException
  581. */
  582. public function spreadList($id, UserSpreadServices $services)
  583. {
  584. $where['store_id'] = 0;
  585. $where['staff_id'] = 0;
  586. $where['uid'] = $id;
  587. return app('json')->success($services->getSpreadList($where, '*', ['spreadUser', 'admin'], false));
  588. }
  589. /**
  590. * 用户批量操作
  591. * @return mixed
  592. * @throws \think\db\exception\DataNotFoundException
  593. * @throws \think\db\exception\DbException
  594. * @throws \think\db\exception\ModelNotFoundException
  595. */
  596. public function batchProcess(UserBatchProcessServices $batchProcessServices)
  597. {
  598. [$type, $uids, $all, $where, $data] = $this->request->postMore([
  599. ['type', 1],
  600. ['uids', ''],
  601. ['all', 0],
  602. ['where', ""],
  603. ['data', []]
  604. ], true);
  605. if (!$uids && $all == 0) return $this->fail('请选择批处理用户');
  606. if (!$data) {
  607. return $this->fail('请选择批处理数据');
  608. }
  609. //批量操作
  610. $batchProcessServices->batchProcess((int)$type, $uids, $data, !!$all, (array)$where);
  611. return app('json')->success('已加入消息队列,请稍后查看');
  612. }
  613. }