User.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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\AdminBaseController;
  13. use app\Request;
  14. use app\services\user\UserBillServices;
  15. use app\services\user\UserGroupServices;
  16. use app\services\user\UserLevelServices;
  17. use app\services\user\UserServices;
  18. use app\services\user\UserSpreadServices;
  19. use app\services\user\UserWechatuserServices;
  20. use app\validate\admin\user\UserValidate;
  21. use qiniu\exceptions\AdminException;
  22. use think\db\exception\DataNotFoundException;
  23. use think\db\exception\DbException;
  24. use think\db\exception\ModelNotFoundException;
  25. class User extends AdminBaseController
  26. {
  27. /**
  28. * user constructor.
  29. * @param Request $request
  30. * @param UserServices $services
  31. */
  32. public function __construct(Request $request, UserServices $services)
  33. {
  34. parent::__construct($request);
  35. $this->service = $services;
  36. $this->searchable = [
  37. ['page', 1],
  38. ['limit', 20],
  39. ['nickname', ''],
  40. ['status', ''],
  41. ['is_promoter', ''],
  42. ['order', ''],
  43. ['data', ''],
  44. ['user_type', ''],
  45. ['country', ''],
  46. ['province', ''],
  47. ['city', ''],
  48. ['user_time_type', ''],
  49. ['user_time', ''],
  50. ['sex', ''],
  51. [['level', 0], 0],
  52. [['group_id', 'd'], 0],
  53. ['now_money', 'normal'],
  54. ['field_key', ''],
  55. ];
  56. }
  57. /**
  58. * 显示资源列表头部
  59. *
  60. * @return \think\Response
  61. */
  62. public function typeHeader()
  63. {
  64. $list = $this->service->typeHead();
  65. return $this->success(compact('list'));
  66. }
  67. /**
  68. * 显示资源列表
  69. *
  70. * @return \think\Response
  71. * @throws DataNotFoundException
  72. * @throws DbException
  73. * @throws ModelNotFoundException
  74. */
  75. public function index()
  76. {
  77. $where = $this->request->getMore($this->searchable);
  78. $where['user_time_type'] = $where['user_time_type'] == 'all' ? '' : $where['user_time_type'];
  79. return $this->success($this->service->userIndex($where));
  80. }
  81. /**
  82. * 后台添加用户
  83. *
  84. * @return \think\Response
  85. * @throws DataNotFoundException
  86. * @throws DbException
  87. * @throws ModelNotFoundException
  88. */
  89. public function save()
  90. {
  91. $data = $this->request->postMore([
  92. ['is_promoter', 0],
  93. ['real_name', ''],
  94. ['card_id', ''],
  95. ['birthday', ''],
  96. ['mark', ''],
  97. ['status', 1],
  98. ['level', 0],
  99. ['phone', 0],
  100. ['group_id', 0],
  101. ['pwd', ''],
  102. ['true_pwd', ''],
  103. ['trade_pwd', ''],
  104. ['true_trade_pwd', ''],
  105. ['sex', 0],
  106. ['provincials', ''],
  107. ['spread_uid', 0],
  108. ['province', 0],
  109. ['city', 0],
  110. ['area', 0],
  111. ['street', 0],
  112. ]);
  113. $this->validate(['pwd' => $data['pwd'], 'phone' => $data['phone']], app()->make(UserValidate::class));
  114. if ($this->service->be(['phone' => $data['phone']])) {
  115. return $this->error('手机号已经存在不能添加相同的手机号用户');
  116. }
  117. $data['nickname'] = substr_replace($data['phone'], '****', 3, 4);
  118. if ($data['card_id']) {
  119. if (!check_card($data['card_id'])) return $this->error('请输入正确的身份证号码');
  120. }
  121. if ($data['birthday']) {
  122. if (strtotime($data['birthday']) > time()) return $this->error('生日请选择今天之前日期');
  123. }
  124. if (!$data['true_pwd']) {
  125. return $this->error('请输入确认密码');
  126. }
  127. if ($data['pwd'] != $data['true_pwd']) {
  128. return $this->error('两次输入的密码不一致');
  129. }
  130. if (!check_password($data['pwd'])) {
  131. return $this->error('您设置的密码太过简单:至少6位,最多18位,包含大小写字母、数字和特殊字符');
  132. }
  133. [$data['pwd'], $data['salt']] = password($data['pwd']);
  134. unset($data['true_pwd']);
  135. if ($data['trade_pwd']) {
  136. if (!$data['true_trade_pwd']) {
  137. return $this->error('请确认交易密码');
  138. }
  139. if (!check_trade_password($data['trade_pwd'])) return $this->error('交易密码为6位数字');
  140. if ($data['trade_pwd'] != $data['true_trade_pwd']) {
  141. return $this->error('两次输入的交易密码不一致');
  142. }
  143. [$data['trade_pwd'], $data['trade_salt']] = password($data['trade_pwd']);
  144. unset($data['true_trade_pwd']);
  145. }
  146. $data['avatar'] = sys_config('h5_avatar');
  147. $data['admin_id'] = $this->adminId;
  148. $data['user_type'] = 'h5';
  149. $data['birthday'] = empty($data['birthday']) ? 0 : strtotime($data['birthday']);
  150. $data['add_time'] = time();
  151. $data['account'] = create_account();
  152. $spread_uid = $data['spread_uid'] ?? 0;
  153. unset($data['spread_uid']);
  154. $this->service->transaction(function () use ($data, $spread_uid) {
  155. $res = true;
  156. $userInfo = $this->service->create($data);
  157. if ($spread_uid > 0) {
  158. /** @var UserSpreadServices $spread */
  159. $spread = app()->make(UserSpreadServices::class);
  160. $spread->adminSetSpread($userInfo->uid, $spread_uid);
  161. }
  162. if ($data['level']) {
  163. $res = $this->service->saveGiveLevel((int)$userInfo->uid, (int)$data['level']);
  164. }
  165. if (!$res) {
  166. throw new AdminException('保存添加用户失败');
  167. }
  168. });
  169. return $this->success('添加成功');
  170. }
  171. /**
  172. * 执行赠送会员等级
  173. * @param $uid
  174. * @return mixed
  175. * @throws DbException
  176. */
  177. public function give_level($uid)
  178. {
  179. if (!$uid) return $this->error('缺少参数');
  180. [$level_id] = $this->request->postMore([
  181. ['level_id', 0],
  182. ], true);
  183. return $this->success($this->service->saveGiveLevel((int)$uid, (int)$level_id) ? '赠送成功' : '赠送失败');
  184. }
  185. /**
  186. * 清除会员等级
  187. * @param $uid
  188. * @return mixed
  189. */
  190. public function del_level($uid)
  191. {
  192. if (!$uid) return $this->error('缺少参数');
  193. return $this->success($this->service->cleanUpLevel((int)$uid) ? '清除成功' : '清除失败');
  194. }
  195. /**
  196. * 保存会员分组
  197. * @return mixed
  198. * @throws DataNotFoundException
  199. * @throws DbException
  200. * @throws ModelNotFoundException
  201. */
  202. public function set_group()
  203. {
  204. [$group_id, $uids, $all, $where] = $this->request->postMore([
  205. ['group_id', 0],
  206. ['uids', ''],
  207. ['all', 0],
  208. ['where', ""],
  209. ], true);
  210. if (!$uids && $all == 0) return $this->error('缺少参数');
  211. if (!$group_id) return $this->error('请选择分组');
  212. if ($all == 0) {
  213. $uids = explode(',', $uids);
  214. }
  215. if ($all == 1) {
  216. $where = $where ? json_decode($where, true) : [];
  217. /** @var UserWechatuserServices $userWechatUser */
  218. $userWechatUser = app()->make(UserWechatuserServices::class);
  219. $fields = 'u.uid';
  220. [$list, $count] = $userWechatUser->getWhereUserList($where, $fields);
  221. $uids = array_unique(array_column($list, 'uid'));
  222. }
  223. /** @var UserGroupServices $userGroup */
  224. $userGroup = app()->make(UserGroupServices::class);
  225. if (!$userGroup->get($group_id)) {
  226. return $this->error('该分组不存在');
  227. }
  228. $this->service->setUserGroup($uids, $group_id);
  229. return $this->success('已设置用户分组!');
  230. }
  231. /**
  232. * 执行编辑其他
  233. * @param $uid
  234. * @return mixed
  235. */
  236. public function updateAccount($uid)
  237. {
  238. if (!$uid) return $this->error('数据不存在');
  239. list($money_type, $pm, $num, $mark) = $this->request->postMore([
  240. ['money_type', ''],
  241. ['pm', 1],
  242. ['num', 0],
  243. ['mark', '']
  244. ], true);
  245. if (!$money_type) return $this->error('请选择要操作的字段');
  246. if ($num <= 0) return $this->error('请输入要操作的金额');
  247. return $this->success($this->service->updateAccount($uid, $money_type, $pm, $num, $mark, $this->adminId) ? '修改成功' : '修改失败');
  248. }
  249. /**
  250. * 修改user表状态
  251. *
  252. * @return array
  253. */
  254. public function set_status($status, $uid)
  255. {
  256. if ($status == '' || $uid == 0) return $this->error('参数错误');
  257. $this->service->update($uid, ['status' => $status], 'uid');
  258. return $this->success($status == 0 ? '禁用成功' : '解禁成功');
  259. }
  260. /**
  261. * @param $id
  262. * @return mixed
  263. * @throws DataNotFoundException
  264. * @throws DbException
  265. * @throws ModelNotFoundException
  266. */
  267. public function update($id)
  268. {
  269. $data = $this->request->postMore([
  270. ['is_promoter', -1],
  271. ['real_name', ''],
  272. ['card_id', ''],
  273. ['birthday', ''],
  274. ['mark', ''],
  275. ['status', 0],
  276. ['level', 0],
  277. ['phone', 0],
  278. ['group_id', 0],
  279. ['pwd', ''],
  280. ['true_pwd'],
  281. ['trade_pwd', ''],
  282. ['true_trade_pwd', ''],
  283. ['sex', 0],
  284. ['provincials', ''],
  285. ['province', 0],
  286. ['city', 0],
  287. ['area', 0],
  288. ['street', 0],
  289. ['spread_uid', -1],
  290. ]);
  291. if ($data['phone']) {
  292. if (!check_phone($data['phone'])) return $this->error('手机号码格式不正确');
  293. }
  294. if ($data['card_id']) {
  295. if (!check_card($data['card_id'])) return $this->error('请输入正确的身份证');
  296. }
  297. if ($data['birthday']) {
  298. if (strtotime($data['birthday']) > time()) return $this->error('生日请选择今天之前日期');
  299. }
  300. if ($data['pwd']) {
  301. if (!$data['true_pwd']) {
  302. return $this->error('请输入确认密码');
  303. }
  304. if ($data['pwd'] != $data['true_pwd']) {
  305. return $this->error('两次输入的密码不一致');
  306. }
  307. if (!check_password($data['pwd'])) {
  308. return $this->error('您设置的密码太过简单:至少6位,最多18位,包含大小写字母、数字和特殊字符');
  309. }
  310. $this->validate(['pwd' => $data['pwd']], UserValidate::class);
  311. [$data['pwd'], $data['salt']] = password($data['pwd']);
  312. } else {
  313. unset($data['pwd']);
  314. }
  315. unset($data['true_pwd']);
  316. if ($data['trade_pwd']) {
  317. if (!$data['true_trade_pwd']) {
  318. return $this->error('请确认交易密码');
  319. }
  320. if (!check_trade_password($data['trade_pwd'])) return $this->error('交易密码为6位数字');
  321. if ($data['trade_pwd'] != $data['true_trade_pwd']) {
  322. return $this->error('两次输入的交易密码不一致');
  323. }
  324. [$data['trade_pwd'], $data['trade_salt']] = password($data['trade_pwd']);
  325. } else {
  326. unset($data['trade_pwd']);
  327. }
  328. unset($data['true_trade_pwd']);
  329. $userInfo = $this->service->get($id);
  330. if (!$userInfo) {
  331. return $this->error('用户不存在');
  332. }
  333. if ($data['spread_uid'] != -1) {
  334. $spreadUid = $data['spread_uid'];
  335. if ($id == $spreadUid) {
  336. return $this->error('上级推广人不能为自己');
  337. }
  338. if (!$this->service->be(['uid' => $spreadUid])) {
  339. return $this->error('上级用户不存在');
  340. }
  341. $spreadInfo = $this->service->get($spreadUid);
  342. if ($spreadInfo->spread_uid == $id) {
  343. return $this->error('上级推广人不能为自己下级');
  344. }
  345. }
  346. if (!$id) return $this->error('数据不存在');
  347. $data['admin_id'] = $this->adminId;
  348. $res = $this->service->updateInfo((int)$id, $data);
  349. if ($res) {
  350. $userInfo = $this->service->get($id);
  351. /** @var UserLevelServices $levelServices */
  352. $levelServices = app()->make(UserLevelServices::class);
  353. $levelServices->detection((int)$userInfo['uid']);
  354. }
  355. return $this->success($res ? '修改成功' : '修改失败');
  356. }
  357. /**
  358. * 获取单个用户信息
  359. * @param int $id 用户id
  360. * @return mixed
  361. */
  362. public function oneUserInfo($type, int $id)
  363. {
  364. $data = $this->request->get();
  365. if (!$type) return $this->error('缺少参数');
  366. return $this->success($this->service->oneUserInfo($id, $type, $data));
  367. }
  368. /**
  369. * 同步微信粉丝用户
  370. * @return mixed
  371. */
  372. public function syncWechatUsers()
  373. {
  374. $this->service->syncWechatUsers();
  375. return $this->success('加入消息队列成功,正在异步执行中');
  376. }
  377. /**
  378. * 用户注销
  379. * @return mixed
  380. */
  381. public function delete($id)
  382. {
  383. if (!$id) return app('json')->fail('用户不存在');
  384. event('user.cancelUser', [$id]);
  385. return $this->success('注销成功');
  386. }
  387. }