User.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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\user;
  12. use app\common\repositories\user\UserSpreadLogRepository;
  13. use crmeb\basic\BaseController;
  14. use app\common\repositories\store\coupon\StoreCouponRepository;
  15. use app\common\repositories\store\coupon\StoreCouponUserRepository;
  16. use app\common\repositories\store\order\StoreOrderRepository;
  17. use app\common\repositories\user\UserBillRepository;
  18. use app\common\repositories\user\UserGroupRepository;
  19. use app\common\repositories\user\UserLabelRepository;
  20. use app\common\repositories\user\UserRepository;
  21. use app\common\repositories\wechat\WechatNewsRepository;
  22. use app\common\repositories\wechat\WechatUserRepository;
  23. use app\validate\admin\UserNowMoneyValidate;
  24. use app\validate\admin\UserValidate;
  25. use FormBuilder\Exception\FormBuilderException;
  26. use think\App;
  27. use think\db\exception\DataNotFoundException;
  28. use think\db\exception\DbException;
  29. use think\db\exception\ModelNotFoundException;
  30. use think\facade\Db;
  31. /**
  32. * Class User
  33. * @package app\controller\admin\user
  34. * @author xaboy
  35. * @day 2020-05-07
  36. */
  37. class User extends BaseController
  38. {
  39. /**
  40. * @var UserRepository
  41. */
  42. protected $repository;
  43. /**
  44. * User constructor.
  45. * @param App $app
  46. * @param UserRepository $repository
  47. */
  48. public function __construct(App $app, UserRepository $repository)
  49. {
  50. parent::__construct($app);
  51. $this->repository = $repository;
  52. }
  53. /**
  54. * @return mixed
  55. * @throws DataNotFoundException
  56. * @throws DbException
  57. * @throws ModelNotFoundException
  58. * @author xaboy
  59. * @day 2020-05-07
  60. */
  61. public function lst()
  62. {
  63. /*
  64. * 昵称,分组,标签,地址,性别,
  65. */
  66. $where = $this->request->params([
  67. 'label_id',
  68. 'user_type',
  69. 'sex',
  70. 'is_promoter',
  71. 'country',
  72. 'pay_count',
  73. 'user_time_type',
  74. 'user_time',
  75. 'nickname',
  76. 'province',
  77. 'city',
  78. 'group_id']);
  79. [$page, $limit] = $this->getPage();
  80. return app('json')->success($this->repository->getList($where, $page, $limit));
  81. }
  82. public function spreadList($uid)
  83. {
  84. $where = $this->request->params(['level', 'keyword', 'date']);
  85. [$page, $limit] = $this->getPage();
  86. return app('json')->success($this->repository->getLevelList($uid, $where, $page, $limit));
  87. }
  88. public function spreadOrder($uid)
  89. {
  90. $where = $this->request->params(['level', 'keyword', 'date']);
  91. [$page, $limit] = $this->getPage();
  92. return app('json')->success($this->repository->subOrder($uid, $page, $limit, $where));
  93. }
  94. public function clearSpread($uid)
  95. {
  96. $this->repository->update($uid, ['spread_uid' => 0]);
  97. return app('json')->success('清除成功');
  98. }
  99. /**
  100. * @param $id
  101. * @return mixed
  102. * @throws DataNotFoundException
  103. * @throws DbException
  104. * @throws FormBuilderException
  105. * @throws ModelNotFoundException
  106. * @author xaboy
  107. * @day 2020-05-09
  108. */
  109. public function updateForm($id)
  110. {
  111. if (!$this->repository->exists($id))
  112. return app('json')->fail('数据不存在');
  113. return app('json')->success(formToData($this->repository->userForm($id)));
  114. }
  115. /**
  116. * @param $id
  117. * @param UserValidate $validate
  118. * @param UserLabelRepository $labelRepository
  119. * @param UserGroupRepository $groupRepository
  120. * @return mixed
  121. * @throws DbException
  122. * @author xaboy
  123. * @day 2020-05-09
  124. */
  125. public function update($id, UserValidate $validate, UserLabelRepository $labelRepository, UserGroupRepository $groupRepository)
  126. {
  127. $data = $this->request->params(['real_name', 'phone', 'birthday', 'card_id', 'addres', 'mark', 'group_id', ['label_id', []], ['is_promoter', 0]]);
  128. $validate->check($data);
  129. if (!$this->repository->exists($id))
  130. return app('json')->fail('数据不存在');
  131. if ($data['group_id'] && !$groupRepository->exists($data['group_id']))
  132. return app('json')->fail('分组不存在');
  133. $label_id = (array)$data['label_id'];
  134. foreach ($label_id as $k => $value) {
  135. $label_id[$k] = (int)$value;
  136. if (!$labelRepository->exists((int)$value))
  137. return app('json')->fail('标签不存在');
  138. }
  139. $data['label_id'] = implode(',', $label_id);
  140. if ($data['is_promoter'])
  141. $data['promoter_time'] = date('Y-m-d H:i:s');
  142. if(!$data['birthday']) unset($data['birthday']);
  143. $this->repository->update($id, $data);
  144. return app('json')->success('编辑成功');
  145. }
  146. /**
  147. * @param $id
  148. * @param UserLabelRepository $labelRepository
  149. * @return mixed
  150. * @throws DbException
  151. * @author xaboy
  152. * @day 2020-05-08
  153. */
  154. public function changeLabel($id, UserLabelRepository $labelRepository)
  155. {
  156. $label_id = (array)$this->request->param('label_id', []);
  157. if (!$this->repository->exists($id))
  158. return app('json')->fail('数据不存在');
  159. foreach ($label_id as $k => $value) {
  160. $label_id[$k] = (int)$value;
  161. if (!$labelRepository->exists((int)$value))
  162. return app('json')->fail('标签不存在');
  163. }
  164. $label_id = implode(',', $label_id);
  165. $this->repository->update($id, compact('label_id'));
  166. return app('json')->success('修改成功');
  167. }
  168. /**
  169. * @param UserLabelRepository $labelRepository
  170. * @return mixed
  171. * @throws DbException
  172. * @author xaboy
  173. * @day 2020-05-08
  174. */
  175. public function batchChangeLabel(UserLabelRepository $labelRepository)
  176. {
  177. $label_id = (array)$this->request->param('label_id', []);
  178. $ids = (array)$this->request->param('ids', []);
  179. if (!count($ids))
  180. return app('json')->fail('数据不存在');
  181. foreach ($label_id as $k => $value) {
  182. $label_id[$k] = (int)$value;
  183. if (!$labelRepository->exists((int)$value))
  184. return app('json')->fail('标签不存在');
  185. }
  186. $this->repository->batchChangeLabelId($ids, $label_id);
  187. return app('json')->success('修改成功');
  188. }
  189. /**
  190. * @param $id
  191. * @return mixed
  192. * @throws DataNotFoundException
  193. * @throws DbException
  194. * @throws FormBuilderException
  195. * @throws ModelNotFoundException
  196. * @author xaboy
  197. * @day 2020-05-08
  198. */
  199. public function changeLabelForm($id)
  200. {
  201. if (!$this->repository->exists($id))
  202. return app('json')->fail('数据不存在');
  203. return app('json')->success(formToData($this->repository->changeLabelForm($id)));
  204. }
  205. /**
  206. * @return mixed
  207. * @throws DataNotFoundException
  208. * @throws DbException
  209. * @throws FormBuilderException
  210. * @throws ModelNotFoundException
  211. * @author xaboy
  212. * @day 2020-05-08
  213. */
  214. public function batchChangeLabelForm()
  215. {
  216. $ids = $this->request->param('ids', '');
  217. $ids = array_filter(explode(',', $ids));
  218. if (!count($ids))
  219. return app('json')->fail('数据不存在');
  220. return app('json')->success(formToData($this->repository->changeLabelForm($ids)));
  221. }
  222. /**
  223. * @return mixed
  224. * @throws DataNotFoundException
  225. * @throws DbException
  226. * @throws FormBuilderException
  227. * @throws ModelNotFoundException
  228. * @author xaboy
  229. * @day 2020-05-08
  230. */
  231. public function batchChangeGroupForm()
  232. {
  233. $ids = $this->request->param('ids', '');
  234. $ids = array_filter(explode(',', $ids));
  235. if (!count($ids))
  236. return app('json')->fail('数据不存在');
  237. return app('json')->success(formToData($this->repository->changeGroupForm($ids)));
  238. }
  239. /**
  240. * @param $id
  241. * @param UserGroupRepository $groupRepository
  242. * @return mixed
  243. * @throws DbException
  244. * @author xaboy
  245. * @day 2020-05-07
  246. */
  247. public function changeGroup($id, UserGroupRepository $groupRepository)
  248. {
  249. $group_id = (int)$this->request->param('group_id', 0);
  250. if (!$this->repository->exists($id))
  251. return app('json')->fail('数据不存在');
  252. if ($group_id && !$groupRepository->exists($group_id))
  253. return app('json')->fail('分组不存在');
  254. $this->repository->update($id, compact('group_id'));
  255. return app('json')->success('修改成功');
  256. }
  257. /**
  258. * @param UserGroupRepository $groupRepository
  259. * @return mixed
  260. * @throws DbException
  261. * @author xaboy
  262. * @day 2020-05-07
  263. */
  264. public function batchChangeGroup(UserGroupRepository $groupRepository)
  265. {
  266. $group_id = (int)$this->request->param('group_id', 0);
  267. $ids = (array)$this->request->param('ids', []);
  268. if (!count($ids))
  269. return app('json')->fail('数据不存在');
  270. if ($group_id && !$groupRepository->exists($group_id))
  271. return app('json')->fail('分组不存在');
  272. $this->repository->batchChangeGroupId($ids, $group_id);
  273. return app('json')->success('修改成功');
  274. }
  275. /**
  276. * @param $id
  277. * @return mixed
  278. * @throws FormBuilderException
  279. * @throws DataNotFoundException
  280. * @throws DbException
  281. * @throws ModelNotFoundException
  282. * @author xaboy
  283. * @day 2020-05-07
  284. */
  285. public function changeGroupForm($id)
  286. {
  287. if (!$this->repository->exists($id))
  288. return app('json')->fail('数据不存在');
  289. return app('json')->success(formToData($this->repository->changeGroupForm($id)));
  290. }
  291. /**
  292. * @param $id
  293. * @return mixed
  294. * @throws FormBuilderException
  295. * @author xaboy
  296. * @day 2020-05-07
  297. */
  298. public function changeNowMoneyForm($id)
  299. {
  300. if (!$this->repository->exists($id))
  301. return app('json')->fail('数据不存在');
  302. return app('json')->success(formToData($this->repository->changeNowMoneyForm($id)));
  303. }
  304. /**
  305. * @param $id
  306. * @param UserNowMoneyValidate $validate
  307. * @return mixed
  308. * @throws DataNotFoundException
  309. * @throws DbException
  310. * @throws ModelNotFoundException
  311. * @author xaboy
  312. * @day 2020-05-07
  313. */
  314. public function changeNowMoney($id, UserNowMoneyValidate $validate)
  315. {
  316. $data = $this->request->params(['now_money', 'type']);
  317. $validate->check($data);
  318. if (!$this->repository->exists($id))
  319. return app('json')->fail('数据不存在');
  320. $this->repository->changeNowMoney($id, $this->request->adminId(), $data['type'], $data['now_money']);
  321. return app('json')->success('修改成功');
  322. }
  323. /**
  324. * @param WechatNewsRepository $wechatNewsRepository
  325. * @param WechatUserRepository $wechatUserRepository
  326. * @return mixed
  327. * @author xaboy
  328. * @day 2020-05-11
  329. */
  330. public function sendNews(WechatNewsRepository $wechatNewsRepository, WechatUserRepository $wechatUserRepository)
  331. {
  332. $ids = array_filter(array_unique(explode(',', $this->request->param('ids'))));
  333. $news_id = (int)$this->request->param('news_id', 0);
  334. if (!$news_id)
  335. return app('json')->fail('请选择图文消息');
  336. if (!$wechatNewsRepository->exists($news_id))
  337. return app('json')->fail('数据不存在');
  338. if (!count($ids))
  339. return app('json')->fail('请选择微信用户');
  340. $wechatUserRepository->sendNews($news_id, $ids);
  341. return app('json')->success('发送成功');
  342. }
  343. public function promoterList()
  344. {
  345. $where = $this->request->params(['keyword','date', 'user_type', 'status', 'sex', 'group_id']);
  346. $where['is_promoter'] = 1;
  347. [$page, $limit] = $this->getPage();
  348. return app('json')->success($this->repository->getList($where, $page, $limit));
  349. }
  350. public function detail($id)
  351. {
  352. if (!$this->repository->exists($id))
  353. return app('json')->fail('数据不存在');
  354. return app('json')->success($this->repository->userOrderDetail($id));
  355. }
  356. public function order($id, StoreOrderRepository $repository)
  357. {
  358. if (!$this->repository->exists($id))
  359. return app('json')->fail('数据不存在');
  360. [$page, $limit] = $this->getPage();
  361. return app('json')->success($repository->userList($id, $page, $limit));
  362. }
  363. public function coupon($id, StoreCouponUserRepository $repository)
  364. {
  365. if (!$this->repository->exists($id))
  366. return app('json')->fail('数据不存在');
  367. [$page, $limit] = $this->getPage();
  368. return app('json')->success($repository->userList(['uid' => $id], $page, $limit));
  369. }
  370. public function bill($id, UserBillRepository $repository)
  371. {
  372. if (!$this->repository->exists(intval($id)))
  373. return app('json')->fail('数据不存在');
  374. [$page, $limit] = $this->getPage();
  375. return app('json')->success($repository->userList([
  376. 'now_money' => 0,
  377. 'status' => 1
  378. ], $id, $page, $limit));
  379. }
  380. public function spreadLog($id)
  381. {
  382. if (!$this->repository->exists((int)$id))
  383. return app('json')->fail('数据不存在');
  384. [$page, $limit] = $this->getPage();
  385. return app('json')->success(app()->make(UserSpreadLogRepository::class)->getList(['uid' => $id], $page, $limit));
  386. }
  387. public function spreadForm($id)
  388. {
  389. if (!$this->repository->exists((int)$id))
  390. return app('json')->fail('数据不存在');
  391. return app('json')->success(formToData($this->repository->changeSpreadForm($id)));
  392. }
  393. public function spread($id)
  394. {
  395. if (!$this->repository->exists((int)$id))
  396. return app('json')->fail('数据不存在');
  397. $spid = $this->request->param('spid');
  398. $spid = (int)($spid['id'] ?? $spid);
  399. if ($spid == $id)
  400. return app('json')->fail('不能选自己');
  401. if (!$spid || !$this->repository->exists($spid))
  402. return app('json')->fail('推荐人不存在');
  403. $this->repository->changeSpread($id, $spid, $this->request->adminId());
  404. return app('json')->success('修改成功');
  405. }
  406. }