User.php 15 KB

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