User.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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. *
  31. * @return \think\Response
  32. */
  33. public function type_header()
  34. {
  35. $list = $this->services->typeHead();
  36. return app('json')->success(compact('list'));
  37. }
  38. /**
  39. * 显示资源列表
  40. *
  41. * @return \think\Response
  42. */
  43. public function index()
  44. {
  45. $where = $this->request->getMore([
  46. ['page', 1],
  47. ['limit', 20],
  48. ['nickname', ''],
  49. ['status', ''],
  50. ['pay_count', ''],
  51. ['is_promoter', ''],
  52. ['order', ''],
  53. ['data', ''],
  54. ['user_type', ''],
  55. ['country', ''],
  56. ['province', ''],
  57. ['city', ''],
  58. ['user_time_type', ''],
  59. ['user_time', ''],
  60. ['sex', ''],
  61. [['level', 0], 0],
  62. [['group_id', 'd'], 0],
  63. ['label_id', ''],
  64. ['now_money', 'normal'],
  65. ['field_key', ''],
  66. ['isMember', '']
  67. ]);
  68. return app('json')->success($this->services->index($where));
  69. }
  70. /**
  71. * 显示创建资源表单页.
  72. *
  73. * @return \think\Response
  74. */
  75. public function create()
  76. {
  77. return app('json')->success($this->services->saveForm());
  78. }
  79. /**
  80. * 添加编辑用户信息时候的信息
  81. * @param int $uid
  82. * @return mixed
  83. * @throws \think\db\exception\DataNotFoundException
  84. * @throws \think\db\exception\DbException
  85. * @throws \think\db\exception\ModelNotFoundException
  86. */
  87. public function userSaveInfo($uid = 0)
  88. {
  89. $data = $this->services->getUserSaveInfo($uid);
  90. return app('json')->success($data);
  91. }
  92. /**
  93. * 保存新建的资源
  94. *
  95. * @param \think\Request $request
  96. * @return \think\Response
  97. */
  98. public function save()
  99. {
  100. $data = $this->request->postMore([
  101. ['real_name', ''],
  102. ['phone', 0],
  103. ['birthday', ''],
  104. ['card_id', ''],
  105. ['addres', ''],
  106. ['mark', ''],
  107. ['pwd', ''],
  108. ['true_pwd', ''],
  109. ['level', 0],
  110. ['group_id', 0],
  111. ['label_id', []],
  112. ['spread_open', 1],
  113. ['is_promoter', 0],
  114. ['status', 0]
  115. ]);
  116. if ($data['phone']) {
  117. if (!check_phone($data['phone'])) {
  118. return app('json')->fail('手机号码格式不正确');
  119. }
  120. if ($this->services->count(['phone' => $data['phone']])) {
  121. return app('json')->fail('手机号已经存在不能添加相同的手机号用户');
  122. }
  123. $data['nickname'] = substr_replace($data['phone'], '****', 3, 4);
  124. }
  125. if ($data['card_id']) {
  126. if (!check_card($data['card_id'])) return app('json')->fail('请输入正确的身份证');
  127. }
  128. if ($data['pwd']) {
  129. if (!$data['true_pwd']) {
  130. return app('json')->fail('请输入确认密码');
  131. }
  132. if ($data['pwd'] != $data['true_pwd']) {
  133. return app('json')->fail('两次输入的密码不一致');
  134. }
  135. $data['pwd'] = md5($data['pwd']);
  136. } else {
  137. unset($data['pwd']);
  138. }
  139. unset($data['true_pwd']);
  140. $data['avatar'] = sys_config('h5_avatar');
  141. $data['adminId'] = $this->adminId;
  142. $data['user_type'] = 'h5';
  143. $label = $data['label_id'];
  144. unset($data['label_id']);
  145. foreach ($label as $k => $v) {
  146. if (!$v) {
  147. unset($label[$k]);
  148. }
  149. }
  150. $data['birthday'] = empty($data['birthday']) ? 0 : strtotime($data['birthday']);
  151. $data['add_time'] = time();
  152. $this->services->transaction(function () use ($data, $label) {
  153. $res = true;
  154. $userInfo = $this->services->save($data);
  155. if ($label) {
  156. $res = $this->services->saveSetLabel([$userInfo->uid], $label);
  157. }
  158. if ($data['level']) {
  159. $res = $this->services->saveGiveLevel((int)$userInfo->uid, (int)$data['level']);
  160. }
  161. if (!$res) {
  162. throw new ValidateException('保存添加用户失败');
  163. }
  164. });
  165. return app('json')->success('添加成功');
  166. }
  167. /**
  168. * 显示指定的资源
  169. *
  170. * @param int $id
  171. * @return \think\Response
  172. */
  173. public function read($id)
  174. {
  175. if (is_string($id)) {
  176. $id = (int)$id;
  177. }
  178. return app('json')->success($this->services->read($id));
  179. }
  180. /**
  181. * 赠送会员等级
  182. * @param int $uid
  183. * @return mixed
  184. * */
  185. public function give_level($id)
  186. {
  187. if (!$id) return app('json')->fail('缺少参数');
  188. return app('json')->success($this->services->giveLevel((int)$id));
  189. }
  190. /*
  191. * 执行赠送会员等级
  192. * @param int $uid
  193. * @return mixed
  194. * */
  195. public function save_give_level($id)
  196. {
  197. if (!$id) return app('json')->fail('缺少参数');
  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) ? '赠送成功' : '赠送失败');
  202. }
  203. /**
  204. * 赠送付费会员时长
  205. * @param int $uid
  206. * @return mixed
  207. * */
  208. public function give_level_time($id)
  209. {
  210. if (!$id) return app('json')->fail('缺少参数');
  211. return app('json')->success($this->services->giveLevelTime((int)$id));
  212. }
  213. /*
  214. * 执行赠送付费会员时长
  215. * @param int $uid
  216. * @return mixed
  217. * */
  218. public function save_give_level_time($id)
  219. {
  220. if (!$id) return app('json')->fail('缺少参数');
  221. list($days) = $this->request->postMore([
  222. ['days', 0],
  223. ], true);
  224. return app('json')->success($this->services->saveGiveLevelTime((int)$id, (int)$days) ? '赠送成功' : '赠送失败');
  225. }
  226. /**
  227. * 清除会员等级
  228. * @param int $uid
  229. * @return json
  230. */
  231. public function del_level($id)
  232. {
  233. if (!$id) return app('json')->fail('缺少参数');
  234. return app('json')->success($this->services->cleanUpLevel((int)$id) ? '清除成功' : '清除失败');
  235. }
  236. /**
  237. * 设置会员分组
  238. * @param $id
  239. * @return mixed
  240. * @throws \FormBuilder\Exception\FormBuilderException
  241. * @throws \think\db\exception\DataNotFoundException
  242. * @throws \think\db\exception\DbException
  243. * @throws \think\db\exception\ModelNotFoundException
  244. */
  245. public function set_group()
  246. {
  247. list($uids) = $this->request->postMore([
  248. ['uids', []],
  249. ], true);
  250. if (!$uids) return app('json')->fail('缺少参数');
  251. return app('json')->success($this->services->setGroup($uids));
  252. }
  253. /**
  254. * 保存会员分组
  255. * @param $id
  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('缺少参数');
  265. if (!$group_id) return app('json')->fail('请选择分组');
  266. $uids = explode(',', $uids);
  267. return app('json')->success($this->services->saveSetGroup($uids, (int)$group_id) ? '设置分组成功' : '设置分组失败或无改动');
  268. }
  269. /**
  270. * 设置用户标签
  271. * @return mixed
  272. * @throws \FormBuilder\Exception\FormBuilderException
  273. * @throws \think\db\exception\DataNotFoundException
  274. * @throws \think\db\exception\DbException
  275. * @throws \think\db\exception\ModelNotFoundException
  276. */
  277. public function set_label()
  278. {
  279. list($uids) = $this->request->postMore([
  280. ['uids', []],
  281. ], true);
  282. $uid = implode(',', $uids);
  283. if (!$uid) return app('json')->fail('缺少参数');
  284. return app('json')->success($this->services->setLabel($uids));
  285. }
  286. /**
  287. * 保存用户标签
  288. * @return mixed
  289. */
  290. public function save_set_label()
  291. {
  292. list($lables, $uids) = $this->request->postMore([
  293. ['label_id', []],
  294. ['uids', ''],
  295. ], true);
  296. if (!$uids) return app('json')->fail('缺少参数');
  297. if (!$lables) return app('json')->fail('请选择标签');
  298. $uids = explode(',', $uids);
  299. return app('json')->success($this->services->saveSetLabel($uids, $lables) ? '设置标签成功' : '设置标签失败');
  300. }
  301. /**
  302. * 编辑其他
  303. * @param $id
  304. * @return mixed
  305. * @throws \FormBuilder\Exception\FormBuilderException
  306. */
  307. public function edit_other($id)
  308. {
  309. if (!$id) return app('json')->fail('数据不存在');
  310. return app('json')->success($this->services->editOther((int)$id));
  311. }
  312. /**
  313. * 执行编辑其他
  314. * @param int $id
  315. * @return mixed
  316. */
  317. public function update_other($id)
  318. {
  319. $data = $this->request->postMore([
  320. ['money_status', 0],
  321. ['money', 0],
  322. ['integration_status', 0],
  323. ['integration', 0],
  324. ]);
  325. if (!$id) return app('json')->fail('数据不存在');
  326. $data['adminId'] = $this->adminId;
  327. $data['money'] = (string)$data['money'];
  328. $data['integration'] = (string)$data['integration'];
  329. $data['is_other'] = true;
  330. return app('json')->success($this->services->updateInfo($id, $data) ? '修改成功' : '修改失败');
  331. }
  332. /**
  333. * 修改user表状态
  334. *
  335. * @return array
  336. */
  337. public function set_status($status, $id)
  338. {
  339. // if ($status == '' || $id == 0) return app('json')->fail('参数错误');
  340. // UserModel::where(['uid' => $id])->update(['status' => $status]);
  341. return app('json')->success($status == 0 ? '禁用成功' : '解禁成功');
  342. }
  343. /**
  344. * 编辑会员信息
  345. * @param $id
  346. * @return mixed|\think\response\Json|void
  347. */
  348. public function edit($id)
  349. {
  350. if (!$id) return app('json')->fail('数据不存在');
  351. return app('json')->success($this->services->edit($id));
  352. }
  353. public function update($id)
  354. {
  355. $data = $this->request->postMore([
  356. ['money_status', 0],
  357. ['is_promoter', 0],
  358. ['real_name', ''],
  359. ['card_id', ''],
  360. ['birthday', ''],
  361. ['mark', ''],
  362. ['money', 0],
  363. ['integration_status', 0],
  364. ['integration', 0],
  365. ['status', 0],
  366. ['level', 0],
  367. ['phone', 0],
  368. ['addres', ''],
  369. ['label_id', []],
  370. ['group_id', 0],
  371. ['pwd', ''],
  372. ['true_pwd'],
  373. ['spread_open', 1]
  374. ]);
  375. if ($data['phone']) {
  376. if (!preg_match("/^1[3456789]\d{9}$/", $data['phone'])) return app('json')->fail('手机号码格式不正确');
  377. }
  378. if ($data['card_id']) {
  379. if (!check_card($data['card_id'])) return app('json')->fail('请输入正确的身份证');
  380. }
  381. if ($data['pwd']) {
  382. if (!$data['true_pwd']) {
  383. return app('json')->fail('请输入确认密码');
  384. }
  385. if ($data['pwd'] != $data['true_pwd']) {
  386. return app('json')->fail('两次输入的密码不一致');
  387. }
  388. $data['pwd'] = md5($data['pwd']);
  389. } else {
  390. unset($data['pwd']);
  391. }
  392. unset($data['true_pwd']);
  393. if (!$id) return app('json')->fail('数据不存在');
  394. $data['adminId'] = $this->adminId;
  395. $data['money'] = (string)$data['money'];
  396. $data['integration'] = (string)$data['integration'];
  397. return app('json')->success($this->services->updateInfo($id, $data) ? '修改成功' : '修改失败');
  398. }
  399. /**
  400. * 获取单个用户信息
  401. * @param $id 用户id
  402. * @return mixed
  403. */
  404. public function oneUserInfo($id)
  405. {
  406. $data = $this->request->getMore([
  407. ['type', ''],
  408. ]);
  409. $id = (int)$id;
  410. if ($data['type'] == '') return app('json')->fail('缺少参数');
  411. return app('json')->success($this->services->oneUserInfo($id, $data['type']));
  412. }
  413. /**
  414. * 同步微信粉丝用户
  415. * @return mixed
  416. */
  417. public function syncWechatUsers()
  418. {
  419. $this->services->syncWechatUsers();
  420. return app('json')->success('加入消息队列成功,正在异步执行中');
  421. }
  422. }