User.php 13 KB

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