User.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. *
  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(400252);
  119. }
  120. if ($this->services->count(['phone' => $data['phone'], 'is_del' => 0])) {
  121. return app('json')->fail(400314);
  122. }
  123. if (trim($data['real_name']) != '') {
  124. $data['nickname'] = $data['real_name'];
  125. } else {
  126. $data['nickname'] = substr_replace($data['phone'], '****', 3, 4);
  127. }
  128. }
  129. if ($data['card_id']) {
  130. if (!check_card($data['card_id'])) return app('json')->fail(400315);
  131. }
  132. if ($data['pwd']) {
  133. if (!$data['true_pwd']) {
  134. return app('json')->fail(400263);
  135. }
  136. if ($data['pwd'] != $data['true_pwd']) {
  137. return app('json')->fail(400264);
  138. }
  139. $data['pwd'] = md5($data['pwd']);
  140. } else {
  141. unset($data['pwd']);
  142. }
  143. unset($data['true_pwd']);
  144. $data['avatar'] = sys_config('h5_avatar');
  145. $data['adminId'] = $this->adminId;
  146. $data['user_type'] = 'h5';
  147. $label = $data['label_id'];
  148. unset($data['label_id']);
  149. foreach ($label as $k => $v) {
  150. if (!$v) {
  151. unset($label[$k]);
  152. }
  153. }
  154. $data['birthday'] = empty($data['birthday']) ? 0 : strtotime($data['birthday']);
  155. $data['add_time'] = time();
  156. $this->services->transaction(function () use ($data, $label) {
  157. $res = true;
  158. $userInfo = $this->services->save($data);
  159. if ($label) {
  160. $res = $this->services->saveSetLabel([$userInfo->uid], $label);
  161. }
  162. if ($data['level']) {
  163. $res = $this->services->saveGiveLevel((int)$userInfo->uid, (int)$data['level']);
  164. }
  165. if (!$res) {
  166. return app('json')->fail(100006);
  167. }
  168. });
  169. return app('json')->success(100021);
  170. }
  171. /**
  172. * 显示指定的资源
  173. *
  174. * @param int $id
  175. * @return \think\Response
  176. */
  177. public function read($id)
  178. {
  179. if (is_string($id)) {
  180. $id = (int)$id;
  181. }
  182. return app('json')->success($this->services->read($id));
  183. }
  184. /**
  185. * 赠送会员等级
  186. * @param int $uid
  187. * @return mixed
  188. * */
  189. public function give_level($id)
  190. {
  191. if (!$id) return app('json')->fail(100100);
  192. return app('json')->success($this->services->giveLevel((int)$id));
  193. }
  194. /*
  195. * 执行赠送会员等级
  196. * @param int $uid
  197. * @return mixed
  198. * */
  199. public function save_give_level($id)
  200. {
  201. if (!$id) return app('json')->fail(100100);
  202. list($level_id) = $this->request->postMore([
  203. ['level_id', 0],
  204. ], true);
  205. return app('json')->success($this->services->saveGiveLevel((int)$id, (int)$level_id) ? 400218 : 400219);
  206. }
  207. /**
  208. * 赠送付费会员时长
  209. * @param int $uid
  210. * @return mixed
  211. * */
  212. public function give_level_time($id)
  213. {
  214. if (!$id) return app('json')->fail(100100);
  215. return app('json')->success($this->services->giveLevelTime((int)$id));
  216. }
  217. /*
  218. * 执行赠送付费会员时长
  219. * @param int $uid
  220. * @return mixed
  221. * */
  222. public function save_give_level_time($id)
  223. {
  224. if (!$id) return app('json')->fail(100100);
  225. list($days) = $this->request->postMore([
  226. ['days', 0],
  227. ], true);
  228. return app('json')->success($this->services->saveGiveLevelTime((int)$id, (int)$days) ? 400218 : 400219);
  229. }
  230. /**
  231. * 清除会员等级
  232. * @param int $uid
  233. * @return json
  234. */
  235. public function del_level($id)
  236. {
  237. if (!$id) return app('json')->fail(100100);
  238. return app('json')->success($this->services->cleanUpLevel((int)$id) ? 400185 : 400186);
  239. }
  240. /**
  241. * 设置会员分组
  242. * @param $id
  243. * @return mixed
  244. * @throws \FormBuilder\Exception\FormBuilderException
  245. * @throws \think\db\exception\DataNotFoundException
  246. * @throws \think\db\exception\DbException
  247. * @throws \think\db\exception\ModelNotFoundException
  248. */
  249. public function set_group()
  250. {
  251. list($uids) = $this->request->postMore([
  252. ['uids', []],
  253. ], true);
  254. if (!$uids) return app('json')->fail(100100);
  255. return app('json')->success($this->services->setGroup($uids));
  256. }
  257. /**
  258. * 保存会员分组
  259. * @param $id
  260. * @return mixed
  261. */
  262. public function save_set_group()
  263. {
  264. list($group_id, $uids) = $this->request->postMore([
  265. ['group_id', 0],
  266. ['uids', ''],
  267. ], true);
  268. if (!$uids) return app('json')->fail(100100);
  269. if (!$group_id) return app('json')->fail(400316);
  270. $uids = explode(',', $uids);
  271. return app('json')->success($this->services->saveSetGroup($uids, (int)$group_id) ? 100014 : 100015);
  272. }
  273. /**
  274. * 设置用户标签
  275. * @return mixed
  276. * @throws \FormBuilder\Exception\FormBuilderException
  277. * @throws \think\db\exception\DataNotFoundException
  278. * @throws \think\db\exception\DbException
  279. * @throws \think\db\exception\ModelNotFoundException
  280. */
  281. public function set_label()
  282. {
  283. list($uids) = $this->request->postMore([
  284. ['uids', []],
  285. ], true);
  286. $uid = implode(',', $uids);
  287. if (!$uid) return app('json')->fail(100100);
  288. return app('json')->success($this->services->setLabel($uids));
  289. }
  290. /**
  291. * 保存用户标签
  292. * @return mixed
  293. */
  294. public function save_set_label()
  295. {
  296. list($lables, $uids) = $this->request->postMore([
  297. ['label_id', []],
  298. ['uids', ''],
  299. ], true);
  300. if (!$uids) return app('json')->fail(100100);
  301. if (!$lables) return app('json')->fail(400317);
  302. $uids = explode(',', $uids);
  303. return app('json')->success($this->services->saveSetLabel($uids, $lables) ? 100014 : 100015);
  304. }
  305. /**
  306. * 编辑其他
  307. * @param $id
  308. * @return mixed
  309. * @throws \FormBuilder\Exception\FormBuilderException
  310. */
  311. public function edit_other($id)
  312. {
  313. if (!$id) return app('json')->fail(100026);
  314. return app('json')->success($this->services->editOther((int)$id));
  315. }
  316. /**
  317. * 执行编辑其他
  318. * @param int $id
  319. * @return mixed
  320. */
  321. public function update_other($id)
  322. {
  323. $data = $this->request->postMore([
  324. ['money_status', 0],
  325. ['money', 0],
  326. ['integration_status', 0],
  327. ['integration', 0],
  328. ]);
  329. if (!$id) return app('json')->fail(100100);
  330. $data['adminId'] = $this->adminId;
  331. $data['money'] = (string)$data['money'];
  332. $data['integration'] = (string)$data['integration'];
  333. $data['is_other'] = true;
  334. return app('json')->success($this->services->updateInfo($id, $data) ? 100001 : 100007);
  335. }
  336. /**
  337. * 修改user表状态
  338. *
  339. * @return array
  340. */
  341. public function set_status($status, $id)
  342. {
  343. // if ($status == '' || $id == 0) return app('json')->fail('参数错误');
  344. // UserModel::where(['uid' => $id])->update(['status' => $status]);
  345. return app('json')->success(100014);
  346. }
  347. /**
  348. * 编辑会员信息
  349. * @param $id
  350. * @return mixed|\think\response\Json|void
  351. */
  352. public function edit($id)
  353. {
  354. if (!$id) return app('json')->fail(100100);
  355. return app('json')->success($this->services->edit($id));
  356. }
  357. public function update($id)
  358. {
  359. $data = $this->request->postMore([
  360. ['money_status', 0],
  361. ['is_promoter', 0],
  362. ['real_name', ''],
  363. ['card_id', ''],
  364. ['birthday', ''],
  365. ['mark', ''],
  366. ['money', 0],
  367. ['integration_status', 0],
  368. ['integration', 0],
  369. ['status', 0],
  370. ['level', 0],
  371. ['phone', 0],
  372. ['addres', ''],
  373. ['label_id', []],
  374. ['group_id', 0],
  375. ['pwd', ''],
  376. ['true_pwd'],
  377. ['spread_open', 1]
  378. ]);
  379. if (!$id) return app('json')->fail(100100);
  380. if ($data['phone']) {
  381. if (!preg_match("/^1[3456789]\d{9}$/", $data['phone'])) return app('json')->fail(400252);
  382. }
  383. if ($data['card_id']) {
  384. if (!check_card($data['card_id'])) return app('json')->fail(400315);
  385. }
  386. if ($data['pwd']) {
  387. if (!$data['true_pwd']) {
  388. return app('json')->fail(400263);
  389. }
  390. if ($data['pwd'] != $data['true_pwd']) {
  391. return app('json')->fail(400264);
  392. }
  393. $data['pwd'] = md5($data['pwd']);
  394. } else {
  395. unset($data['pwd']);
  396. }
  397. unset($data['true_pwd']);
  398. $data['adminId'] = $this->adminId;
  399. $data['money'] = (string)$data['money'];
  400. $data['integration'] = (string)$data['integration'];
  401. return app('json')->success($this->services->updateInfo($id, $data) ? 100001 : 100007);
  402. }
  403. /**
  404. * 获取单个用户信息
  405. * @param $id 用户id
  406. * @return mixed
  407. */
  408. public function oneUserInfo($id)
  409. {
  410. $data = $this->request->getMore([
  411. ['type', ''],
  412. ]);
  413. $id = (int)$id;
  414. if ($data['type'] == '') return app('json')->fail(100100);
  415. return app('json')->success($this->services->oneUserInfo($id, $data['type']));
  416. }
  417. /**
  418. * 同步微信粉丝用户
  419. * @return mixed
  420. */
  421. public function syncWechatUsers()
  422. {
  423. $this->services->syncWechatUsers();
  424. return app('json')->success(400318);
  425. }
  426. }