User.php 15 KB

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