User.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\adminapi\controller\v1\user;
  12. use app\services\activity\coupon\StoreCouponIssueServices;
  13. use app\services\system\config\SystemConfigServices;
  14. use app\services\user\UserServices;
  15. use app\adminapi\controller\AuthController;
  16. use crmeb\services\CacheService;
  17. use think\exception\ValidateException;
  18. use think\facade\App;
  19. class User extends AuthController
  20. {
  21. /**
  22. * @var UserServices
  23. */
  24. protected $services;
  25. /**
  26. * user constructor.
  27. * @param App $app
  28. * @param UserServices $services
  29. */
  30. public function __construct(App $app, UserServices $services)
  31. {
  32. parent::__construct($app);
  33. $this->services = $services;
  34. }
  35. /**
  36. * 用户列表
  37. * @return mixed
  38. */
  39. public function index()
  40. {
  41. $where = $this->request->getMore([
  42. ['page', 1],
  43. ['limit', 20],
  44. ['nickname', ''],
  45. ['status', ''],
  46. ['pay_count', ''],
  47. ['is_promoter', ''],
  48. ['order', ''],
  49. ['data', ''],
  50. ['user_type', ''],
  51. ['country', ''],
  52. ['province', ''],
  53. ['city', ''],
  54. ['user_time_type', ''],
  55. ['user_time', ''],
  56. ['sex', ''],
  57. [['level', 0], 0],
  58. [['group_id', 'd'], 0],
  59. ['label_id', ''],
  60. ['now_money', 'normal'],
  61. ['field_key', ''],
  62. ['isMember', ''],
  63. ['balance', []],
  64. ['integral', []],
  65. ['before_pay_time', ''],
  66. ['pay_count_num', []],
  67. ['pay_count_money', []],
  68. ['recharge_count', []],
  69. ['agent_level', 0],
  70. ]);
  71. $where['label_id'] = toIntArray($where['label_id']);
  72. return app('json')->success($this->services->index($where));
  73. }
  74. /**
  75. * 添加用户表单
  76. * @return mixed
  77. * @throws \FormBuilder\Exception\FormBuilderException
  78. */
  79. public function create()
  80. {
  81. return app('json')->success($this->services->saveForm());
  82. }
  83. /**
  84. * 添加编辑用户信息时候的信息
  85. * @param $uid
  86. * @return mixed
  87. * @throws \think\db\exception\DataNotFoundException
  88. * @throws \think\db\exception\DbException
  89. * @throws \think\db\exception\ModelNotFoundException
  90. */
  91. public function userSaveInfo($uid = 0)
  92. {
  93. $data = $this->services->getUserSaveInfo($uid);
  94. return app('json')->success($data);
  95. }
  96. /**
  97. * 保存新建用户
  98. * @return mixed
  99. * @throws \think\Exception
  100. */
  101. public function save()
  102. {
  103. $data = $this->request->postMore([
  104. ['real_name', ''],
  105. ['phone', 0],
  106. ['birthday', ''],
  107. ['card_id', ''],
  108. ['addres', ''],
  109. ['mark', ''],
  110. ['pwd', ''],
  111. ['true_pwd', ''],
  112. ['level', 0],
  113. ['group_id', 0],
  114. ['label_id', []],
  115. ['spread_open', 1],
  116. ['is_promoter', 0],
  117. ['status', 0]
  118. ]);
  119. if (!$data['real_name']) {
  120. return app('json')->fail('请填写姓名和电话');
  121. }
  122. if (!$data['phone']) {
  123. return app('json')->fail('请填写姓名和电话');
  124. }
  125. if (!check_phone($data['phone'])) {
  126. return app('json')->fail('手机号格式错误');
  127. }
  128. if ($this->services->count(['phone' => $data['phone'], 'is_del' => 0])) {
  129. return app('json')->fail('手机号已经存在');
  130. }
  131. $data['nickname'] = $data['real_name'];
  132. if ($data['card_id']) {
  133. if (!check_card($data['card_id'])) return app('json')->fail('请输入正确的身份证');
  134. }
  135. if (!$data['pwd']) {
  136. return app('json')->fail('请输入密码');
  137. }
  138. if (!$data['true_pwd']) {
  139. return app('json')->fail('请输入确认密码');
  140. }
  141. if ($data['pwd'] != $data['true_pwd']) {
  142. return app('json')->fail('两次输入的密码不一致');
  143. }
  144. if (strlen($data['pwd']) < 6 || strlen($data['pwd']) > 32) {
  145. return app('json')->fail('账号密码必须是在6到32位之间');
  146. }
  147. $data['pwd'] = md5($data['pwd']);
  148. unset($data['true_pwd']);
  149. $data['avatar'] = sys_config('h5_avatar');
  150. $data['adminId'] = $this->adminId;
  151. $data['user_type'] = 'h5';
  152. $label = $data['label_id'];
  153. unset($data['label_id']);
  154. foreach ($label as $k => $v) {
  155. if (!$v) {
  156. unset($label[$k]);
  157. }
  158. }
  159. $data['birthday'] = empty($data['birthday']) ? 0 : strtotime($data['birthday']);
  160. $data['add_time'] = time();
  161. $this->services->transaction(function () use ($data, $label) {
  162. $res = true;
  163. $userInfo = $this->services->save($data);
  164. $this->services->rewardNewUser((int)$userInfo->uid);
  165. app()->make(StoreCouponIssueServices::class)->userFirstSubGiveCoupon((int)$userInfo->uid);
  166. if ($label) {
  167. $res = $this->services->saveSetLabel([$userInfo->uid], $label);
  168. }
  169. if ($data['level']) {
  170. $res = $this->services->saveGiveLevel((int)$userInfo->uid, (int)$data['level']);
  171. }
  172. if (!$res) {
  173. return app('json')->fail('保存失败');
  174. }
  175. });
  176. return app('json')->success('添加成功');
  177. }
  178. /**
  179. * 获取用户账户详情
  180. * @param $id
  181. * @return mixed
  182. * @throws \think\db\exception\DataNotFoundException
  183. * @throws \think\db\exception\DbException
  184. * @throws \think\db\exception\ModelNotFoundException
  185. */
  186. public function read($id)
  187. {
  188. if (is_string($id)) {
  189. $id = (int)$id;
  190. }
  191. return app('json')->success($this->services->read($id));
  192. }
  193. /**
  194. * 赠送会员等级表单
  195. * @param $id
  196. * @return mixed
  197. */
  198. public function give_level($id)
  199. {
  200. if (!$id) return app('json')->fail('参数错误');
  201. return app('json')->success($this->services->giveLevel((int)$id));
  202. }
  203. /**
  204. * 执行赠送会员等级
  205. * @param $id
  206. * @return mixed
  207. * @throws \think\db\exception\DataNotFoundException
  208. * @throws \think\db\exception\DbException
  209. * @throws \think\db\exception\ModelNotFoundException
  210. */
  211. public function save_give_level($id)
  212. {
  213. if (!$id) return app('json')->fail('参数错误');
  214. list($level_id) = $this->request->postMore([
  215. ['level_id', 0],
  216. ], true);
  217. return app('json')->success($this->services->saveGiveLevel((int)$id, (int)$level_id) ? '赠送成功' : '赠送失败');
  218. }
  219. /**
  220. * 赠送付费会员时长表单
  221. * @param $id
  222. * @return mixed
  223. * @throws \FormBuilder\Exception\FormBuilderException
  224. */
  225. public function give_level_time($id)
  226. {
  227. if (!$id) return app('json')->fail('参数错误');
  228. return app('json')->success($this->services->giveLevelTime((int)$id));
  229. }
  230. /**
  231. * 执行赠送付费会员时长
  232. * @param $id
  233. * @return mixed
  234. * @throws \think\db\exception\DataNotFoundException
  235. * @throws \think\db\exception\DbException
  236. * @throws \think\db\exception\ModelNotFoundException
  237. */
  238. public function save_give_level_time($id)
  239. {
  240. if (!$id) return app('json')->fail('参数错误');
  241. list($days) = $this->request->postMore([
  242. ['days', 0],
  243. ], true);
  244. return app('json')->success($this->services->saveGiveLevelTime((int)$id, (int)$days) ? '赠送成功' : '赠送失败');
  245. }
  246. /**
  247. * 清除会员等级
  248. * @param $id
  249. * @return mixed
  250. */
  251. public function del_level($id)
  252. {
  253. if (!$id) return app('json')->fail('参数错误');
  254. return app('json')->success($this->services->cleanUpLevel((int)$id) ? '清除成功' : '清除失败');
  255. }
  256. /**
  257. * 设置会员分组
  258. * @return mixed
  259. */
  260. public function set_group()
  261. {
  262. list($uids) = $this->request->postMore([
  263. ['uids', []],
  264. ], true);
  265. if (!$uids) return app('json')->fail('参数错误');
  266. return app('json')->success($this->services->setGroup($uids));
  267. }
  268. /**
  269. * 保存会员分组
  270. * @return mixed
  271. */
  272. public function save_set_group()
  273. {
  274. list($group_id, $uids) = $this->request->postMore([
  275. ['group_id', 0],
  276. ['uids', ''],
  277. ], true);
  278. if (!$uids) return app('json')->fail('参数错误');
  279. if (!$group_id) return app('json')->fail('请选择分组');
  280. $uids = explode(',', $uids);
  281. return app('json')->success($this->services->saveSetGroup($uids, (int)$group_id) ? '设置成功' : '设置失败');
  282. }
  283. /**
  284. * 设置用户标签
  285. * @return mixed
  286. */
  287. public function set_label()
  288. {
  289. list($uids) = $this->request->postMore([
  290. ['uids', []],
  291. ], true);
  292. $uid = implode(',', $uids);
  293. if (!$uid) return app('json')->fail('参数错误');
  294. return app('json')->success($this->services->setLabel($uids));
  295. }
  296. /**
  297. * 保存用户标签
  298. * @return mixed
  299. */
  300. public function save_set_label()
  301. {
  302. list($labels, $uids, $label_type) = $this->request->postMore([
  303. ['label_id', []],
  304. ['uids', ''],
  305. ['label_type', 0],
  306. ], true);
  307. if (!$uids) return app('json')->fail('参数错误');
  308. if (!$labels) return app('json')->fail('请选择标签');
  309. $uids = explode(',', $uids);
  310. return app('json')->success($this->services->saveSetLabel($uids, $labels, $label_type) ? '设置成功' : '设置失败');
  311. }
  312. /**
  313. * 编辑其他
  314. * @param $id
  315. * @return mixed
  316. * @throws \FormBuilder\Exception\FormBuilderException
  317. */
  318. public function edit_other($id, $type)
  319. {
  320. if (!$id) return app('json')->fail('数据不存在');
  321. return app('json')->success($this->services->editOther((int)$id, $type));
  322. }
  323. /**
  324. * 执行编辑其他
  325. * @param $id
  326. * @return mixed
  327. * @throws \think\Exception
  328. * @throws \think\db\exception\DataNotFoundException
  329. * @throws \think\db\exception\ModelNotFoundException
  330. */
  331. public function update_other($id)
  332. {
  333. $data = $this->request->postMore([
  334. ['money_status', 0],
  335. ['money', 0],
  336. ['integration_status', 0],
  337. ['integration', 0],
  338. ['mark', ''],
  339. ]);
  340. if (!$id) return app('json')->fail('参数错误');
  341. $data['adminId'] = $this->adminId;
  342. $data['money'] = (string)$data['money'];
  343. $data['integration'] = (string)$data['integration'];
  344. $data['is_other'] = true;
  345. return app('json')->success($this->services->updateInfo($id, $data) ? '修改成功' : '修改失败');
  346. }
  347. /**
  348. * 编辑会员信息
  349. * @param $id
  350. * @return mixed
  351. * @throws \FormBuilder\Exception\FormBuilderException
  352. */
  353. public function edit($id)
  354. {
  355. if (!$id) return app('json')->fail('参数错误');
  356. return app('json')->success($this->services->edit($id));
  357. }
  358. /**
  359. * 修改用户
  360. * @param $id
  361. * @return mixed
  362. * @throws \think\Exception
  363. * @throws \think\db\exception\DataNotFoundException
  364. * @throws \think\db\exception\ModelNotFoundException
  365. */
  366. public function update($id)
  367. {
  368. $data = $this->request->postMore([
  369. ['money_status', 0],
  370. ['is_promoter', 0],
  371. ['real_name', ''],
  372. ['card_id', ''],
  373. ['birthday', ''],
  374. ['mark', ''],
  375. ['money', 0],
  376. ['integration_status', 0],
  377. ['integration', 0],
  378. ['status', 0],
  379. ['level', 0],
  380. ['phone', 0],
  381. ['addres', ''],
  382. ['label_id', []],
  383. ['group_id', 0],
  384. ['pwd', ''],
  385. ['true_pwd'],
  386. ['spread_open', 1]
  387. ]);
  388. if (!$id) return app('json')->fail('参数错误');
  389. if (!$data['real_name']) {
  390. return app('json')->fail('请填写姓名和电话');
  391. }
  392. if (!$data['phone']) {
  393. return app('json')->fail('请填写姓名和电话');
  394. }
  395. if ($data['phone']) {
  396. if (!preg_match("/^1[3456789]\d{9}$/", $data['phone'])) return app('json')->fail('手机号格式错误');
  397. }
  398. if ($this->services->count(['phone' => $data['phone'], 'is_del' => 0, 'not_uid' => $id])) {
  399. return app('json')->fail('手机号已经存在');
  400. }
  401. if ($data['card_id']) {
  402. if (!check_card($data['card_id'])) return app('json')->fail('请输入正确的身份证');
  403. }
  404. if ($data['pwd']) {
  405. if (!$data['true_pwd']) {
  406. return app('json')->fail('请输入确认密码');
  407. }
  408. if ($data['pwd'] != $data['true_pwd']) {
  409. return app('json')->fail('两次输入的密码不一致');
  410. }
  411. if (strlen($data['pwd']) < 6 || strlen($data['pwd']) > 32) {
  412. return app('json')->fail('账号密码必须是在6到32位之间');
  413. }
  414. $data['pwd'] = md5($data['pwd']);
  415. } else {
  416. unset($data['pwd']);
  417. }
  418. unset($data['true_pwd']);
  419. $data['adminId'] = $this->adminId;
  420. $data['money'] = (string)$data['money'];
  421. $data['integration'] = (string)$data['integration'];
  422. return app('json')->success($this->services->updateInfo($id, $data) ? '修改成功' : '修改失败');
  423. }
  424. /**
  425. * 获取单个用户信息
  426. * @param $id
  427. * @return mixed
  428. */
  429. public function oneUserInfo($id)
  430. {
  431. $data = $this->request->getMore([
  432. ['type', ''],
  433. ]);
  434. $id = (int)$id;
  435. if ($data['type'] == '') return app('json')->fail('参数错误');
  436. return app('json')->success($this->services->oneUserInfo($id, $data['type']));
  437. }
  438. /**
  439. * 同步微信粉丝用户
  440. * @return mixed
  441. */
  442. public function syncWechatUsers()
  443. {
  444. $this->services->syncWechatUsers();
  445. return app('json')->success('加入消息队列成功');
  446. }
  447. /**
  448. * 新人礼
  449. * @return \think\Response
  450. * @author wuhaotian
  451. * @email 442384644@qq.com
  452. * @date 2024/9/21
  453. */
  454. public function getNewGift()
  455. {
  456. $data = [
  457. 'reward_money' => intval(sys_config('reward_money')),
  458. 'reward_integral' => intval(sys_config('reward_integral')),
  459. 'reward_coupon' => sys_config('reward_coupon') == '' ? [] : sys_config('reward_coupon')
  460. ];
  461. return app('json')->success($data);
  462. }
  463. /**
  464. * 保存新人礼
  465. * @return \think\Response
  466. * @author wuhaotian
  467. * @email 442384644@qq.com
  468. * @date 2024/9/21
  469. */
  470. public function saveNewGift()
  471. {
  472. $data = $this->request->postMore([
  473. ['reward_money', 0],
  474. ['reward_integral', 0],
  475. ['reward_coupon', '']
  476. ]);
  477. $configServices = app()->make(SystemConfigServices::class);
  478. foreach ($data as $k => $v) {
  479. $configServices->update($k, ['value' => json_encode($v)], 'menu_name');
  480. }
  481. CacheService::clear();
  482. return app('json')->success('保存成功');
  483. }
  484. }