User.php 13 KB

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