User.php 16 KB

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