User.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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. if (!$data['true_pwd']) {
  122. return app('json')->fail(400263);
  123. }
  124. if ($data['pwd'] != $data['true_pwd']) {
  125. return app('json')->fail(400264);
  126. }
  127. if (strlen($data['pwd']) < 6 || strlen($data['pwd']) > 32) {
  128. return app('json')->fail(400762);
  129. }
  130. $data['pwd'] = md5($data['pwd']);
  131. } else {
  132. unset($data['pwd']);
  133. }
  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. */
  168. public function read($id)
  169. {
  170. if (is_string($id)) {
  171. $id = (int)$id;
  172. }
  173. return app('json')->success($this->services->read($id));
  174. }
  175. /**
  176. * 赠送会员等级表单
  177. * @param $id
  178. * @return mixed
  179. */
  180. public function give_level($id)
  181. {
  182. if (!$id) return app('json')->fail(100100);
  183. return app('json')->success($this->services->giveLevel((int)$id));
  184. }
  185. /**
  186. * 执行赠送会员等级
  187. * @param $id
  188. * @return mixed
  189. */
  190. public function save_give_level($id)
  191. {
  192. if (!$id) return app('json')->fail(100100);
  193. list($level_id) = $this->request->postMore([
  194. ['level_id', 0],
  195. ], true);
  196. return app('json')->success($this->services->saveGiveLevel((int)$id, (int)$level_id) ? 400218 : 400219);
  197. }
  198. /**
  199. * 赠送付费会员时长表单
  200. * @param $id
  201. * @return mixed
  202. */
  203. public function give_level_time($id)
  204. {
  205. if (!$id) return app('json')->fail(100100);
  206. return app('json')->success($this->services->giveLevelTime((int)$id));
  207. }
  208. /**
  209. * 执行赠送付费会员时长
  210. * @param $id
  211. * @return mixed
  212. */
  213. public function save_give_level_time($id)
  214. {
  215. if (!$id) return app('json')->fail(100100);
  216. list($days) = $this->request->postMore([
  217. ['days', 0],
  218. ], true);
  219. return app('json')->success($this->services->saveGiveLevelTime((int)$id, (int)$days) ? 400218 : 400219);
  220. }
  221. /**
  222. * 清除会员等级
  223. * @param $id
  224. * @return mixed
  225. */
  226. public function del_level($id)
  227. {
  228. if (!$id) return app('json')->fail(100100);
  229. return app('json')->success($this->services->cleanUpLevel((int)$id) ? 400185 : 400186);
  230. }
  231. /**
  232. * 设置会员分组
  233. * @return mixed
  234. */
  235. public function set_group()
  236. {
  237. list($uids) = $this->request->postMore([
  238. ['uids', []],
  239. ], true);
  240. if (!$uids) return app('json')->fail(100100);
  241. return app('json')->success($this->services->setGroup($uids));
  242. }
  243. /**
  244. * 保存会员分组
  245. * @return mixed
  246. */
  247. public function save_set_group()
  248. {
  249. list($group_id, $uids) = $this->request->postMore([
  250. ['group_id', 0],
  251. ['uids', ''],
  252. ], true);
  253. if (!$uids) return app('json')->fail(100100);
  254. if (!$group_id) return app('json')->fail(400316);
  255. $uids = explode(',', $uids);
  256. return app('json')->success($this->services->saveSetGroup($uids, (int)$group_id) ? 100014 : 100015);
  257. }
  258. /**
  259. * 设置用户标签
  260. * @return mixed
  261. */
  262. public function set_label()
  263. {
  264. list($uids) = $this->request->postMore([
  265. ['uids', []],
  266. ], true);
  267. $uid = implode(',', $uids);
  268. if (!$uid) return app('json')->fail(100100);
  269. return app('json')->success($this->services->setLabel($uids));
  270. }
  271. /**
  272. * 保存用户标签
  273. * @return mixed
  274. */
  275. public function save_set_label()
  276. {
  277. list($lables, $uids) = $this->request->postMore([
  278. ['label_id', []],
  279. ['uids', ''],
  280. ], true);
  281. if (!$uids) return app('json')->fail(100100);
  282. if (!$lables) return app('json')->fail(400317);
  283. $uids = explode(',', $uids);
  284. return app('json')->success($this->services->saveSetLabel($uids, $lables) ? 100014 : 100015);
  285. }
  286. /**
  287. * 编辑其他
  288. * @param $id
  289. * @return mixed
  290. * @throws \FormBuilder\Exception\FormBuilderException
  291. */
  292. public function edit_other($id)
  293. {
  294. if (!$id) return app('json')->fail(100026);
  295. return app('json')->success($this->services->editOther((int)$id));
  296. }
  297. /**
  298. * 执行编辑其他
  299. * @param $id
  300. * @return mixed
  301. * @throws \think\Exception
  302. * @throws \think\db\exception\DataNotFoundException
  303. * @throws \think\db\exception\ModelNotFoundException
  304. */
  305. public function update_other($id)
  306. {
  307. $data = $this->request->postMore([
  308. ['money_status', 0],
  309. ['money', 0],
  310. ['integration_status', 0],
  311. ['integration', 0],
  312. ]);
  313. if (!$id) return app('json')->fail(100100);
  314. $data['adminId'] = $this->adminId;
  315. $data['money'] = (string)$data['money'];
  316. $data['integration'] = (string)$data['integration'];
  317. $data['is_other'] = true;
  318. return app('json')->success($this->services->updateInfo($id, $data) ? 100001 : 100007);
  319. }
  320. /**
  321. * 编辑会员信息
  322. * @param $id
  323. * @return mixed
  324. * @throws \FormBuilder\Exception\FormBuilderException
  325. */
  326. public function edit($id)
  327. {
  328. if (!$id) return app('json')->fail(100100);
  329. return app('json')->success($this->services->edit($id));
  330. }
  331. /**
  332. * 修改用户
  333. * @param $id
  334. * @return mixed
  335. * @throws \think\Exception
  336. * @throws \think\db\exception\DataNotFoundException
  337. * @throws \think\db\exception\ModelNotFoundException
  338. */
  339. public function update($id)
  340. {
  341. $data = $this->request->postMore([
  342. ['money_status', 0],
  343. ['is_promoter', 0],
  344. ['real_name', ''],
  345. ['card_id', ''],
  346. ['birthday', ''],
  347. ['mark', ''],
  348. ['money', 0],
  349. ['integration_status', 0],
  350. ['integration', 0],
  351. ['status', 0],
  352. ['level', 0],
  353. ['phone', 0],
  354. ['addres', ''],
  355. ['label_id', []],
  356. ['group_id', 0],
  357. ['pwd', ''],
  358. ['true_pwd'],
  359. ['spread_open', 1]
  360. ]);
  361. if (!$id) return app('json')->fail(100100);
  362. if ($data['phone']) {
  363. if (!preg_match("/^1[3456789]\d{9}$/", $data['phone'])) return app('json')->fail(400252);
  364. }
  365. if ($data['card_id']) {
  366. if (!check_card($data['card_id'])) return app('json')->fail(400315);
  367. }
  368. if ($data['pwd']) {
  369. if (!$data['true_pwd']) {
  370. return app('json')->fail(400263);
  371. }
  372. if ($data['pwd'] != $data['true_pwd']) {
  373. return app('json')->fail(400264);
  374. }
  375. if (strlen($data['pwd']) < 6 || strlen($data['pwd']) > 32) {
  376. return app('json')->fail(400762);
  377. }
  378. $data['pwd'] = md5($data['pwd']);
  379. } else {
  380. unset($data['pwd']);
  381. }
  382. unset($data['true_pwd']);
  383. $data['adminId'] = $this->adminId;
  384. $data['money'] = (string)$data['money'];
  385. $data['integration'] = (string)$data['integration'];
  386. return app('json')->success($this->services->updateInfo($id, $data) ? 100001 : 100007);
  387. }
  388. /**
  389. * 获取单个用户信息
  390. * @param $id
  391. * @return mixed
  392. */
  393. public function oneUserInfo($id)
  394. {
  395. $data = $this->request->getMore([
  396. ['type', ''],
  397. ]);
  398. $id = (int)$id;
  399. if ($data['type'] == '') return app('json')->fail(100100);
  400. return app('json')->success($this->services->oneUserInfo($id, $data['type']));
  401. }
  402. /**
  403. * 同步微信粉丝用户
  404. * @return mixed
  405. */
  406. public function syncWechatUsers()
  407. {
  408. $this->services->syncWechatUsers();
  409. return app('json')->success(400318);
  410. }
  411. }