User.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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\store\user;
  12. use app\services\store\DeliveryServiceServices;
  13. use think\facade\App;
  14. use app\controller\store\AuthController;
  15. use app\services\store\StoreUserServices;
  16. use app\services\user\UserServices;
  17. use app\services\other\queue\QueueServices;
  18. use app\services\user\label\UserLabelServices;
  19. use app\jobs\BatchHandleJob;
  20. /**
  21. * 门店用户
  22. * Class User
  23. * @package app\controller\store\user
  24. */
  25. class User extends AuthController
  26. {
  27. /**
  28. * DeliveryService constructor.
  29. * @param App $app
  30. * @param StoreUserServices $services
  31. */
  32. public function __construct(App $app, StoreUserServices $services)
  33. {
  34. parent::__construct($app);
  35. $this->services = $services;
  36. }
  37. /**
  38. * 搜索用户
  39. * @param UserServices $services
  40. * @return mixed
  41. * @throws \think\db\exception\DataNotFoundException
  42. * @throws \think\db\exception\DbException
  43. * @throws \think\db\exception\ModelNotFoundException
  44. */
  45. public function search(UserServices $services)
  46. {
  47. $data = $this->request->getMore([
  48. ['keyword', ''],
  49. ['field_key', ''],
  50. ]);
  51. if ($data['field_key'] == 'all') {
  52. $data['field_key'] = '';
  53. }
  54. if ($data['field_key'] && in_array($data['field_key'], ['uid', 'phone', 'bar_code'])) {
  55. $where[$data['field_key']] = trim($data['keyword']);
  56. } else {
  57. $where['store_like'] = trim($data['keyword']);
  58. }
  59. $result = $services->getUserList($where, 'uid,nickname,avatar,phone,now_money,user_type');
  60. $list = $result['list'] ?? [];
  61. $count = $result['count'] ?? 0;
  62. if ($list) {
  63. foreach ($list as &$item) {
  64. //用户类型
  65. if ($item['user_type'] == 'routine') {
  66. $item['user_type'] = '小程序';
  67. } else if ($item['user_type'] == 'wechat') {
  68. $item['user_type'] = '公众号';
  69. } else if ($item['user_type'] == 'h5') {
  70. $item['user_type'] = 'H5';
  71. } else if ($item['user_type'] == 'pc') {
  72. $item['user_type'] = 'PC';
  73. } else if ($item['user_type'] == 'app') {
  74. $item['user_type'] = 'APP';
  75. } else $item['user_type'] = '其他';
  76. }
  77. }
  78. return app('json')->success(compact('list', 'count'));
  79. }
  80. /**
  81. * 显示资源列表
  82. *
  83. * @return \think\Response
  84. */
  85. public function index()
  86. {
  87. $where = $this->request->getMore([
  88. ['page', 1],
  89. ['limit', 20],
  90. ['nickname', ''],
  91. ['status', ''],
  92. ['pay_count', ''],
  93. ['is_promoter', ''],
  94. ['order', ''],
  95. ['data', ''],
  96. ['user_type', ''],
  97. ['country', ''],
  98. ['province', ''],
  99. ['city', ''],
  100. ['user_time_type', ''],
  101. ['user_time', ''],
  102. ['sex', ''],
  103. [['level', 0], 0],
  104. [['group_id', 'd'], 0],
  105. [['label_id', 'd'], 0],
  106. ['now_money', 'normal'],
  107. ['field_key', ''],
  108. ['isMember', '']
  109. ]);
  110. return app('json')->success($this->services->index($where, $this->storeId));
  111. }
  112. /**
  113. * 显示创建资源表单页.
  114. *
  115. * @return \think\Response
  116. */
  117. public function create()
  118. {
  119. return app('json')->success($this->services->saveForm());
  120. }
  121. /**
  122. * 保存新建的资源
  123. *
  124. * @param \think\Request $request
  125. * @return \think\Response
  126. */
  127. public function save()
  128. {
  129. $data = $this->request->postMore([
  130. ['is_promoter', 0],
  131. ['real_name', ''],
  132. ['card_id', ''],
  133. ['birthday', ''],
  134. ['mark', ''],
  135. ['status', 0],
  136. ['level', 0],
  137. ['phone', 0],
  138. ['addres', ''],
  139. ['label_id', []],
  140. ['group_id', 0],
  141. ['pwd', ''],
  142. ['true_pwd', ''],
  143. ['spread_open', 1]
  144. ]);
  145. if ($data['phone']) {
  146. if (!check_phone($data['phone'])) {
  147. return app('json')->fail('手机号码格式不正确');
  148. }
  149. if ($this->services->count(['phone' => $data['phone']])) {
  150. return app('json')->fail('手机号已经存在不能添加相同的手机号用户');
  151. }
  152. $data['nickname'] = substr_replace($data['phone'], '****', 3, 4);
  153. }
  154. if ($data['card_id']) {
  155. try {
  156. if (!check_card($data['card_id'])) return app('json')->fail('请输入正确的身份证');
  157. } catch (\Throwable $e) {
  158. // return app('json')->fail('请输入正确的身份证');
  159. }
  160. }
  161. if ($data['pwd']) {
  162. if (!$data['true_pwd']) {
  163. return app('json')->fail('请输入确认密码');
  164. }
  165. if ($data['pwd'] != $data['true_pwd']) {
  166. return app('json')->fail('两次输入的密码不一致');
  167. }
  168. $data['pwd'] = md5($data['pwd']);
  169. } else {
  170. unset($data['pwd']);
  171. }
  172. unset($data['true_pwd']);
  173. $data['avatar'] = sys_config('h5_avatar');
  174. $data['adminId'] = $this->adminId;
  175. $data['user_type'] = 'h5';
  176. $lables = $data['label_id'];
  177. unset($data['label_id']);
  178. foreach ($lables as $k => $v) {
  179. if (!$v) {
  180. unset($lables[$k]);
  181. }
  182. }
  183. $data['birthday'] = empty($data['birthday']) ? 0 : strtotime($data['birthday']);
  184. $data['add_time'] = time();
  185. $this->services->transaction(function () use ($data, $lables) {
  186. $res = true;
  187. $userInfo = $this->services->save($data);
  188. if ($lables) {
  189. $res = $this->services->saveSetLabel([$userInfo->uid], $lables);
  190. }
  191. if ($data['level']) {
  192. $res = $this->services->saveGiveLevel((int)$userInfo->uid, (int)$data['level']);
  193. }
  194. if (!$res) {
  195. throw new ValidateException('保存添加用户失败');
  196. }
  197. event('user.register', [$this->services->get((int)$userInfo->uid), true, 0]);
  198. });
  199. return app('json')->success('添加成功');
  200. }
  201. /**
  202. * 显示指定的资源
  203. *
  204. * @param int $id
  205. * @param UserServices $services
  206. * @return \think\Response
  207. */
  208. public function read($id, UserServices $services)
  209. {
  210. if (is_string($id)) {
  211. $id = (int)$id;
  212. }
  213. return app('json')->success($services->read($id));
  214. }
  215. /**
  216. * 设置用户标签表单
  217. * @param UserServices $services
  218. * @return mixed
  219. */
  220. public function set_label(UserServices $services)
  221. {
  222. [$uids, $all, $where] = $this->request->postMore([
  223. ['uids', []],
  224. ['all', 0],
  225. ['where', ""],
  226. ], true);
  227. return app('json')->success($services->setLabel($uids, $all, $where, 1, (int)$this->storeId));
  228. }
  229. /**
  230. * 批量设置用户标签
  231. * @return mixed
  232. */
  233. public function save_set_label()
  234. {
  235. [$lables, $uids, $all, $where] = $this->request->postMore([
  236. ['label_id', []],
  237. ['uids', ''],
  238. ['all', 0],
  239. ['where', ""],
  240. ], true);
  241. if (!$uids && $all == 0) return app('json')->fail('缺少参数');
  242. if (!$lables || (count($lables) == 1 && !$lables[0])) return app('json')->fail('请选择标签');
  243. if ($all == 0) {
  244. $uids = explode(',', $uids);
  245. $where = [];
  246. }
  247. if ($all == 1) {
  248. $uids = [];
  249. $where = $where ? json_decode($where, true) : [];
  250. }
  251. /** @var UserLabelServices $userLabelServices */
  252. $userLabelServices = app()->make(UserLabelServices::class);
  253. $count = $userLabelServices->getCount([['id', 'IN', $lables]]);
  254. if ($count != count($lables)) {
  255. return app('json')->fail('有用户标签不存在或被删除');
  256. }
  257. $type = 3;//批量设置用户标签
  258. /** @var QueueServices $queueService */
  259. $queueService = app()->make(QueueServices::class);
  260. $queueService->setQueueData($where, 'uid', $uids, $type, $lables);
  261. //加入队列
  262. BatchHandleJob::dispatch([$lables, $type, ['store_id' => $this->storeId]]);
  263. return app('json')->success('后台程序已执行批量设置用户标签任务!');
  264. }
  265. /**
  266. * 编辑其他
  267. * @param $id
  268. * @return mixed
  269. * @throws \FormBuilder\Exception\FormBuilderException
  270. */
  271. public function edit_other($id)
  272. {
  273. if (!$id) return app('json')->fail('数据不存在');
  274. return app('json')->success($this->services->editOther((int)$id));
  275. }
  276. /**
  277. * 执行编辑其他
  278. * @param int $id
  279. * @return mixed
  280. */
  281. public function update_other($id)
  282. {
  283. $data = $this->request->postMore([
  284. ['money_status', 0],
  285. ['money', 0],
  286. ['integration_status', 0],
  287. ['integration', 0],
  288. ]);
  289. if (!$id) return app('json')->fail('数据不存在');
  290. $data['adminId'] = $this->adminId;
  291. $data['money'] = (string)$data['money'];
  292. $data['integration'] = (string)$data['integration'];
  293. $data['is_other'] = true;
  294. return app('json')->success($this->services->updateInfo($id, $data) ? '修改成功' : '修改失败');
  295. }
  296. /**
  297. * 编辑会员信息
  298. * @param $id
  299. * @return mixed|\think\response\Json|void
  300. */
  301. public function edit($id)
  302. {
  303. if (!$id) return app('json')->fail('数据不存在');
  304. return app('json')->success($this->services->edit($id));
  305. }
  306. public function update($id)
  307. {
  308. $data = $this->request->postMore([
  309. ['money_status', 0],
  310. ['is_promoter', 0],
  311. ['real_name', ''],
  312. ['card_id', ''],
  313. ['birthday', ''],
  314. ['mark', ''],
  315. ['money', 0],
  316. ['integration_status', 0],
  317. ['integration', 0],
  318. ['status', 0],
  319. ['level', 0],
  320. ['phone', 0],
  321. ['addres', ''],
  322. ['label_id', ''],
  323. ['group_id', 0],
  324. ['pwd', ''],
  325. ['true_pwd'],
  326. ['spread_open', 1]
  327. ]);
  328. if ($data['phone']) {
  329. if (!check_phone($data['phone'])) return app('json')->fail('手机号码格式不正确');
  330. }
  331. if ($data['card_id']) {
  332. try {
  333. if (!check_card($data['card_id'])) return app('json')->fail('请输入正确的身份证');
  334. } catch (\Throwable $e) {
  335. // return app('json')->fail('请输入正确的身份证');
  336. }
  337. }
  338. if ($data['pwd']) {
  339. if (!$data['true_pwd']) {
  340. return app('json')->fail('请输入确认密码');
  341. }
  342. if ($data['pwd'] != $data['true_pwd']) {
  343. return app('json')->fail('两次输入的密码不一致');
  344. }
  345. $data['pwd'] = md5($data['pwd']);
  346. } else {
  347. unset($data['pwd']);
  348. }
  349. unset($data['true_pwd']);
  350. if (!$id) return app('json')->fail('数据不存在');
  351. $data['adminId'] = $this->adminId;
  352. $data['money'] = (string)$data['money'];
  353. $data['integration'] = (string)$data['integration'];
  354. return app('json')->success($this->services->updateInfo($id, $data) ? '修改成功' : '修改失败');
  355. }
  356. /**
  357. * 获取单个用户信息
  358. * @param $id 用户id
  359. * @param UserServices $services
  360. * @return mixed
  361. */
  362. public function oneUserInfo($id, UserServices $services)
  363. {
  364. $data = $this->request->getMore([
  365. ['type', ''],
  366. ]);
  367. $id = (int)$id;
  368. if ($data['type'] == '') return app('json')->fail('缺少参数');
  369. return app('json')->success($services->oneUserInfo($id, $data['type']));
  370. }
  371. }