User.php 14 KB

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