User.php 13 KB

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