User.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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\user;
  12. use app\common\repositories\store\ExcelRepository;
  13. use app\common\repositories\user\UserSpreadLogRepository;
  14. use app\common\repositories\user\UserVisitRepository;
  15. use crmeb\basic\BaseController;
  16. use app\common\repositories\store\coupon\StoreCouponRepository;
  17. use app\common\repositories\store\coupon\StoreCouponUserRepository;
  18. use app\common\repositories\store\order\StoreOrderRepository;
  19. use app\common\repositories\user\UserBillRepository;
  20. use app\common\repositories\user\UserGroupRepository;
  21. use app\common\repositories\user\UserLabelRepository;
  22. use app\common\repositories\user\UserRepository;
  23. use app\common\repositories\wechat\WechatNewsRepository;
  24. use app\common\repositories\wechat\WechatUserRepository;
  25. use app\validate\admin\UserNowMoneyValidate;
  26. use app\validate\admin\UserValidate;
  27. use crmeb\services\ExcelService;
  28. use FormBuilder\Exception\FormBuilderException;
  29. use think\App;
  30. use think\db\exception\DataNotFoundException;
  31. use think\db\exception\DbException;
  32. use think\db\exception\ModelNotFoundException;
  33. use think\facade\Db;
  34. /**
  35. * Class User
  36. * @package app\controller\admin\user
  37. * @author xaboy
  38. * @day 2020-05-07
  39. */
  40. class User extends BaseController
  41. {
  42. /**
  43. * @var UserRepository
  44. */
  45. protected $repository;
  46. /**
  47. * User constructor.
  48. * @param App $app
  49. * @param UserRepository $repository
  50. */
  51. public function __construct(App $app, UserRepository $repository)
  52. {
  53. parent::__construct($app);
  54. $this->repository = $repository;
  55. }
  56. /**
  57. * @return mixed
  58. * @throws DataNotFoundException
  59. * @throws DbException
  60. * @throws ModelNotFoundException
  61. * @author xaboy
  62. * @day 2020-05-07
  63. */
  64. public function lst()
  65. {
  66. /*
  67. * 昵称,分组,标签,地址,性别,
  68. */
  69. $where = $this->request->params([
  70. 'label_id',
  71. 'user_type',
  72. 'sex',
  73. 'is_promoter',
  74. 'country',
  75. 'pay_count',
  76. 'user_time_type',
  77. 'user_time',
  78. 'nickname',
  79. 'province',
  80. 'city',
  81. 'group_id',
  82. 'phone',
  83. 'uid',
  84. ]);
  85. [$page, $limit] = $this->getPage();
  86. return app('json')->success($this->repository->getList($where, $page, $limit));
  87. }
  88. public function spreadList($uid)
  89. {
  90. $where = $this->request->params(['level', 'keyword', 'date']);
  91. [$page, $limit] = $this->getPage();
  92. return app('json')->success($this->repository->getLevelList($uid, $where, $page, $limit));
  93. }
  94. public function spreadOrder($uid)
  95. {
  96. $where = $this->request->params(['level', 'keyword', 'date']);
  97. [$page, $limit] = $this->getPage();
  98. return app('json')->success($this->repository->subOrder($uid, $page, $limit, $where));
  99. }
  100. public function clearSpread($uid)
  101. {
  102. $this->repository->update($uid, ['spread_uid' => 0]);
  103. return app('json')->success('清除成功');
  104. }
  105. public function createForm()
  106. {
  107. return app('json')->success(formToData($this->repository->createForm()));
  108. }
  109. public function create(UserValidate $validate)
  110. {
  111. $data = $this->request->params([
  112. 'account',
  113. 'pwd',
  114. 'repwd',
  115. 'nickname',
  116. 'avatar',
  117. 'real_name',
  118. 'phone',
  119. 'sex',
  120. 'status',
  121. 'card_id',
  122. ['is_promoter', 0]
  123. ]);
  124. $validate->scene('create')->check($data);
  125. $data['pwd'] = $this->repository->encodePassword($data['repwd']);
  126. unset($data['repwd']);
  127. if ($data['is_promoter']) $data['promoter_time'] = date('Y-m-d H:i:s');
  128. $this->repository->create('h5',$data);
  129. return app('json')->success('添加成功');
  130. }
  131. public function changePasswordForm($id)
  132. {
  133. return app('json')->success(formToData($this->repository->changePasswordForm($id)));
  134. }
  135. public function changePassword($id)
  136. {
  137. $data = $this->request->params([
  138. 'pwd',
  139. 'repwd',
  140. ]);
  141. if (!$data['pwd'] || !$data['repwd'])
  142. return app('json')->fail('密码不能为空');
  143. if ($data['pwd'] !== $data['repwd'])
  144. return app('json')->fail('密码不一致');
  145. $data['pwd'] = $this->repository->encodePassword($data['repwd']);
  146. unset($data['repwd']);
  147. $this->repository->update($id,$data);
  148. return app('json')->success('修改成功');
  149. }
  150. /**
  151. * @param $id
  152. * @return mixed
  153. * @throws DataNotFoundException
  154. * @throws DbException
  155. * @throws FormBuilderException
  156. * @throws ModelNotFoundException
  157. * @author xaboy
  158. * @day 2020-05-09
  159. */
  160. public function updateForm($id)
  161. {
  162. if (!$this->repository->exists($id))
  163. return app('json')->fail('数据不存在');
  164. return app('json')->success(formToData($this->repository->userForm($id)));
  165. }
  166. /**
  167. * @param $id
  168. * @param UserValidate $validate
  169. * @param UserLabelRepository $labelRepository
  170. * @param UserGroupRepository $groupRepository
  171. * @return mixed
  172. * @throws DbException
  173. * @author xaboy
  174. * @day 2020-05-09
  175. */
  176. public function update($id, UserValidate $validate, UserLabelRepository $labelRepository, UserGroupRepository $groupRepository)
  177. {
  178. $data = $this->request->params(['real_name', 'phone', 'birthday', 'card_id', 'addres', 'mark', 'group_id', ['label_id', []], ['is_promoter', 0], ['status', 0]]);
  179. $validate->check($data);
  180. if (!$this->repository->exists($id))
  181. return app('json')->fail('数据不存在');
  182. if ($data['group_id'] && !$groupRepository->exists($data['group_id']))
  183. return app('json')->fail('分组不存在');
  184. $label_id = (array)$data['label_id'];
  185. foreach ($label_id as $k => $value) {
  186. $label_id[$k] = (int)$value;
  187. if (!$labelRepository->exists((int)$value))
  188. return app('json')->fail('标签不存在');
  189. }
  190. $data['label_id'] = implode(',', $label_id);
  191. if ($data['is_promoter'])
  192. $data['promoter_time'] = date('Y-m-d H:i:s');
  193. if(!$data['birthday']) unset($data['birthday']);
  194. $this->repository->update($id, $data);
  195. return app('json')->success('编辑成功');
  196. }
  197. /**
  198. * @param $id
  199. * @param UserLabelRepository $labelRepository
  200. * @return mixed
  201. * @throws DbException
  202. * @author xaboy
  203. * @day 2020-05-08
  204. */
  205. public function changeLabel($id, UserLabelRepository $labelRepository)
  206. {
  207. $label_id = (array)$this->request->param('label_id', []);
  208. if (!$this->repository->exists($id))
  209. return app('json')->fail('数据不存在');
  210. foreach ($label_id as $k => $value) {
  211. $label_id[$k] = (int)$value;
  212. if (!$labelRepository->exists((int)$value))
  213. return app('json')->fail('标签不存在');
  214. }
  215. $label_id = implode(',', $label_id);
  216. $this->repository->update($id, compact('label_id'));
  217. return app('json')->success('修改成功');
  218. }
  219. /**
  220. * @param UserLabelRepository $labelRepository
  221. * @return mixed
  222. * @throws DbException
  223. * @author xaboy
  224. * @day 2020-05-08
  225. */
  226. public function batchChangeLabel(UserLabelRepository $labelRepository)
  227. {
  228. $label_id = (array)$this->request->param('label_id', []);
  229. $ids = (array)$this->request->param('ids', []);
  230. if (!count($ids))
  231. return app('json')->fail('数据不存在');
  232. foreach ($label_id as $k => $value) {
  233. $label_id[$k] = (int)$value;
  234. if (!$labelRepository->exists((int)$value))
  235. return app('json')->fail('标签不存在');
  236. }
  237. $this->repository->batchChangeLabelId($ids, $label_id);
  238. return app('json')->success('修改成功');
  239. }
  240. /**
  241. * @param $id
  242. * @return mixed
  243. * @throws DataNotFoundException
  244. * @throws DbException
  245. * @throws FormBuilderException
  246. * @throws ModelNotFoundException
  247. * @author xaboy
  248. * @day 2020-05-08
  249. */
  250. public function changeLabelForm($id)
  251. {
  252. if (!$this->repository->exists($id))
  253. return app('json')->fail('数据不存在');
  254. return app('json')->success(formToData($this->repository->changeLabelForm($id)));
  255. }
  256. /**
  257. * @return mixed
  258. * @throws DataNotFoundException
  259. * @throws DbException
  260. * @throws FormBuilderException
  261. * @throws ModelNotFoundException
  262. * @author xaboy
  263. * @day 2020-05-08
  264. */
  265. public function batchChangeLabelForm()
  266. {
  267. $ids = $this->request->param('ids', '');
  268. $ids = array_filter(explode(',', $ids));
  269. if (!count($ids))
  270. return app('json')->fail('数据不存在');
  271. return app('json')->success(formToData($this->repository->changeLabelForm($ids)));
  272. }
  273. /**
  274. * @return mixed
  275. * @throws DataNotFoundException
  276. * @throws DbException
  277. * @throws FormBuilderException
  278. * @throws ModelNotFoundException
  279. * @author xaboy
  280. * @day 2020-05-08
  281. */
  282. public function batchChangeGroupForm()
  283. {
  284. $ids = $this->request->param('ids', '');
  285. $ids = array_filter(explode(',', $ids));
  286. if (!count($ids))
  287. return app('json')->fail('数据不存在');
  288. return app('json')->success(formToData($this->repository->changeGroupForm($ids)));
  289. }
  290. /**
  291. * @param $id
  292. * @param UserGroupRepository $groupRepository
  293. * @return mixed
  294. * @throws DbException
  295. * @author xaboy
  296. * @day 2020-05-07
  297. */
  298. public function changeGroup($id, UserGroupRepository $groupRepository)
  299. {
  300. $group_id = (int)$this->request->param('group_id', 0);
  301. if (!$this->repository->exists($id))
  302. return app('json')->fail('数据不存在');
  303. if ($group_id && !$groupRepository->exists($group_id))
  304. return app('json')->fail('分组不存在');
  305. $this->repository->update($id, compact('group_id'));
  306. return app('json')->success('修改成功');
  307. }
  308. /**
  309. * @param UserGroupRepository $groupRepository
  310. * @return mixed
  311. * @throws DbException
  312. * @author xaboy
  313. * @day 2020-05-07
  314. */
  315. public function batchChangeGroup(UserGroupRepository $groupRepository)
  316. {
  317. $group_id = (int)$this->request->param('group_id', 0);
  318. $ids = (array)$this->request->param('ids', []);
  319. if (!count($ids))
  320. return app('json')->fail('数据不存在');
  321. if ($group_id && !$groupRepository->exists($group_id))
  322. return app('json')->fail('分组不存在');
  323. $this->repository->batchChangeGroupId($ids, $group_id);
  324. return app('json')->success('修改成功');
  325. }
  326. /**
  327. * @param $id
  328. * @return mixed
  329. * @throws FormBuilderException
  330. * @throws DataNotFoundException
  331. * @throws DbException
  332. * @throws ModelNotFoundException
  333. * @author xaboy
  334. * @day 2020-05-07
  335. */
  336. public function changeGroupForm($id)
  337. {
  338. if (!$this->repository->exists($id))
  339. return app('json')->fail('数据不存在');
  340. return app('json')->success(formToData($this->repository->changeGroupForm($id)));
  341. }
  342. /**
  343. * @param $id
  344. * @return mixed
  345. * @throws FormBuilderException
  346. * @author xaboy
  347. * @day 2020-05-07
  348. */
  349. public function changeNowMoneyForm($id)
  350. {
  351. if (!$this->repository->exists($id))
  352. return app('json')->fail('数据不存在');
  353. return app('json')->success(formToData($this->repository->changeNowMoneyForm($id)));
  354. }
  355. public function changeIntegralForm($id)
  356. {
  357. if (!$this->repository->exists($id))
  358. return app('json')->fail('数据不存在');
  359. return app('json')->success(formToData($this->repository->changeIntegralForm($id)));
  360. }
  361. /**
  362. * @param $id
  363. * @param UserNowMoneyValidate $validate
  364. * @return mixed
  365. * @throws DataNotFoundException
  366. * @throws DbException
  367. * @throws ModelNotFoundException
  368. * @author xaboy
  369. * @day 2020-05-07
  370. */
  371. public function changeNowMoney($id, UserNowMoneyValidate $validate)
  372. {
  373. $data = $this->request->params(['now_money', 'type']);
  374. $validate->check($data);
  375. if (!$this->repository->exists($id))
  376. return app('json')->fail('数据不存在');
  377. $this->repository->changeNowMoney($id, $this->request->adminId(), $data['type'], $data['now_money']);
  378. return app('json')->success('修改成功');
  379. }
  380. public function changeIntegral($id, UserNowMoneyValidate $validate)
  381. {
  382. $data = $this->request->params(['now_money', 'type']);
  383. $validate->check($data);
  384. if (!$this->repository->exists($id))
  385. return app('json')->fail('数据不存在');
  386. $this->repository->changeIntegral($id, $this->request->adminId(), $data['type'], $data['now_money']);
  387. return app('json')->success('修改成功');
  388. }
  389. /**
  390. * @param WechatNewsRepository $wechatNewsRepository
  391. * @param WechatUserRepository $wechatUserRepository
  392. * @return mixed
  393. * @author xaboy
  394. * @day 2020-05-11
  395. */
  396. public function sendNews(WechatNewsRepository $wechatNewsRepository, WechatUserRepository $wechatUserRepository)
  397. {
  398. $ids = $this->request->param('ids');
  399. if (!is_array($ids)) $ids = explode(',', $this->request->param('ids'));
  400. $ids = array_filter(array_unique($ids));
  401. $news_id = (int)$this->request->param('news_id', 0);
  402. if (!$news_id)
  403. return app('json')->fail('请选择图文消息');
  404. if (!$wechatNewsRepository->exists($news_id))
  405. return app('json')->fail('数据不存在');
  406. if (!count($ids))
  407. return app('json')->fail('请选择微信用户');
  408. $wechatUserRepository->sendNews($news_id, $ids);
  409. return app('json')->success('发送成功');
  410. }
  411. public function promoterList()
  412. {
  413. $where = $this->request->params(['keyword', 'date', 'brokerage_level']);
  414. [$page, $limit] = $this->getPage();
  415. return app('json')->success($this->repository->promoterList($where, $page, $limit));
  416. }
  417. public function promoterCount()
  418. {
  419. return app('json')->success($this->repository->promoterCount());
  420. }
  421. public function detail($id)
  422. {
  423. if (!$this->repository->exists($id))
  424. return app('json')->fail('数据不存在');
  425. return app('json')->success($this->repository->userOrderDetail($id));
  426. }
  427. public function order($id, StoreOrderRepository $repository)
  428. {
  429. if (!$this->repository->exists($id))
  430. return app('json')->fail('数据不存在');
  431. [$page, $limit] = $this->getPage();
  432. return app('json')->success($repository->userList($id, $page, $limit));
  433. }
  434. public function coupon($id, StoreCouponUserRepository $repository)
  435. {
  436. if (!$this->repository->exists($id))
  437. return app('json')->fail('数据不存在');
  438. [$page, $limit] = $this->getPage();
  439. return app('json')->success($repository->userList(['uid' => $id], $page, $limit));
  440. }
  441. public function bill($id, UserBillRepository $repository)
  442. {
  443. if (!$this->repository->exists(intval($id)))
  444. return app('json')->fail('数据不存在');
  445. [$page, $limit] = $this->getPage();
  446. return app('json')->success($repository->userList([
  447. 'now_money' => 0,
  448. 'status' => 1
  449. ], $id, $page, $limit));
  450. }
  451. public function spreadLog($id)
  452. {
  453. if (!$this->repository->exists((int)$id))
  454. return app('json')->fail('数据不存在');
  455. [$page, $limit] = $this->getPage();
  456. return app('json')->success(app()->make(UserSpreadLogRepository::class)->getList(['uid' => $id], $page, $limit));
  457. }
  458. public function spreadForm($id)
  459. {
  460. if (!$this->repository->exists((int)$id))
  461. return app('json')->fail('数据不存在');
  462. return app('json')->success(formToData($this->repository->changeSpreadForm($id)));
  463. }
  464. public function spread($id)
  465. {
  466. if (!$this->repository->exists((int)$id))
  467. return app('json')->fail('数据不存在');
  468. $spid = $this->request->param('spid');
  469. $spid = (int)($spid['id'] ?? $spid);
  470. if ($spid == $id)
  471. return app('json')->fail('不能选自己');
  472. if ($spid && !$this->repository->exists($spid))
  473. return app('json')->fail('推荐人不存在');
  474. $this->repository->changeSpread($id, $spid, $this->request->adminId());
  475. return app('json')->success('修改成功');
  476. }
  477. public function searchLog()
  478. {
  479. $where = $this->request->params(['date', 'keyword', 'nickname', 'user_type']);
  480. $merId = $this->request->merId();
  481. $where['type'] = ['searchMerchant', 'searchProduct'];
  482. if ($merId) {
  483. $where['mer_id'] = $merId;
  484. }
  485. [$page, $limit] = $this->getPage();
  486. return app('json')->success(app()->make(UserVisitRepository::class)->getSearchLog($where, $page, $limit));
  487. }
  488. public function exportSearchLog()
  489. {
  490. $where = $this->request->params(['date', 'keyword', 'nickname', 'user_type']);
  491. $merId = $this->request->merId();
  492. $where['type'] = ['searchMerchant', 'searchProduct'];
  493. if ($merId) {
  494. $where['mer_id'] = $merId;
  495. }
  496. [$page, $limit] = $this->getPage();
  497. $data = app()->make(ExcelService::class)->searchLog($where, $page, $limit);
  498. return app('json')->success($data);
  499. }
  500. public function memberForm($id)
  501. {
  502. return app('json')->success(formToData($this->repository->memberForm($id,1)));
  503. }
  504. public function memberSave($id)
  505. {
  506. $data = $this->request->params(['member_level']);
  507. if (!$this->repository->exists((int)$id))
  508. return app('json')->fail('数据不存在');
  509. $this->repository->updateLevel($id, $data, 1);
  510. return app('json')->success('修改成功');
  511. }
  512. public function spreadLevelForm($id)
  513. {
  514. return app('json')->success(formToData($this->repository->memberForm($id,0)));
  515. }
  516. public function spreadLevelSave($id)
  517. {
  518. $brokerage_level = $this->request->params(['brokerage_level']);
  519. if (!$this->repository->exists((int)$id))
  520. return app('json')->fail('数据不存在');
  521. $this->repository->updateLevel($id, $brokerage_level, 0);
  522. return app('json')->success('修改成功');
  523. }
  524. public function svipForm($id)
  525. {
  526. return app('json')->success(formToData($this->repository->svipForm($id)));
  527. }
  528. public function svipUpdate($id)
  529. {
  530. $data = $this->request->params(['is_svip','add_time','type']);
  531. $this->repository->svipUpdate($id, $data,$this->request->adminId());
  532. return app('json')->success('修改成功');
  533. }
  534. }