User.php 13 KB

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