User.php 15 KB

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