User.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. <?php
  2. namespace app\admin\controller\user;
  3. use app\admin\logic\MoneyLogLogic;
  4. use app\admin\logic\ScoreLogLogic;
  5. use app\admin\logic\UserLogic;
  6. use app\common\model\User as UserModel;
  7. use app\admin\traits\Curd;
  8. use app\common\controller\AdminController;
  9. use app\common\model\UserMoneyModel;
  10. use app\Request;
  11. use app\validate\admin\user\user\ModifyMoney;
  12. use app\validate\admin\user\user\ModifyScore;
  13. use app\validate\admin\user\user\ModifyPid;
  14. use app\validate\admin\user\user\PhoneSet;
  15. use EasyAdmin\tool\CommonTool;
  16. use jianyan\excel\Excel;
  17. use think\App;
  18. use think\exception\ValidateException;
  19. use think\facade\Cache;
  20. use think\facade\Db;
  21. use app\admin\model\dao\User as UserDao;
  22. class User extends AdminController
  23. {
  24. public function __construct(App $app)
  25. {
  26. parent::__construct($app);
  27. $this->model = new UserModel();
  28. }
  29. use Curd;
  30. /**
  31. * 会员列表
  32. * @return mixed|\think\response\Json
  33. * @throws \think\db\exception\DataNotFoundException
  34. * @throws \think\db\exception\DbException
  35. * @throws \think\db\exception\ModelNotFoundException
  36. */
  37. public function index()
  38. {
  39. if ($this->request->isAjax()) {
  40. if (input('selectFields')) {
  41. return $this->selectList();
  42. }
  43. list($page, $limit, $where) = $this->buildTableParames();
  44. Cache::set("USER_EXPORT", ['page' => $page, 'limit' => $limit, 'where' => $where]);
  45. if (($pid = $this->request->param('pid')) !== false && $pid)
  46. $where[] = ['pid', '=', $this->request->param('pid', '')];
  47. $userLogic = new UserLogic();
  48. $data = $userLogic->getList($page, $limit, $where, $this->sort, $this->user_map);
  49. return $data;
  50. }
  51. return $this->fetch();
  52. }
  53. /**
  54. * 禁用用户
  55. * @param $id
  56. */
  57. public function forbid($id)
  58. {
  59. list($result, $msg) = UserLogic::forbid($id);
  60. if ($result !== true) {
  61. $this->error($msg);
  62. }
  63. $this->success($msg);
  64. }
  65. /**
  66. * 启用用户
  67. * @param $id
  68. */
  69. public function enable($id)
  70. {
  71. list($result, $msg) = UserLogic::enable($id);
  72. if ($result !== true) {
  73. $this->error($msg);
  74. }
  75. $this->success($msg);
  76. }
  77. /**
  78. * 会员详情
  79. * @param Request $request
  80. * @return mixed
  81. */
  82. public function details(Request $request)
  83. {
  84. $id = $request->param('id');
  85. $info = UserLogic::getUserDetail($id);
  86. $this->assign('info', $info);
  87. return $this->fetch();
  88. }
  89. /**
  90. * 添加用户(功能不存在)
  91. * @return mixed
  92. */
  93. /*public function add()
  94. {
  95. if ($this->request->isAjax()) {
  96. $post = $this->request->post();
  97. $mobile = $this->model->where('mobile', $post['mobile'])->value('id');
  98. $mobile && $this->error('该手机号码已被注册');
  99. $invite = $this->model->where('code|px_code', $post['code'])->value('id');
  100. !$invite && $this->error('邀请人不存在');
  101. $this->model->startTrans();
  102. try {
  103. $insert['mobile'] = $post['mobile'];
  104. $insert['reg_ip'] = $this->request->ip();
  105. $insert['avatar'] = 'http://images.yxj.hongyun63.com/user/default_avatar.jpg';
  106. $insert['code'] = create_invite_code();
  107. $insert['px_code'] = create_invite_code();
  108. $insert['user_type'] = 2;
  109. $rz_money = 1.5 + rand(0, 30) / 100;
  110. $this->model->save($insert);
  111. $uid = $this->model->id;
  112. $user_data = new UserData();
  113. $user_data->save(['uid' => $uid, 'rz_money' => $rz_money]); // 保存用户关联信息
  114. $this->bindRelation($post['code'], $uid); // 绑定关系
  115. $this->model->commit();
  116. } catch (\Exception $e) {
  117. $this->model->rollback();
  118. $this->error('添加用户失败');
  119. }
  120. $this->success('添加成功');
  121. }
  122. return $this->fetch();
  123. }*/
  124. /**
  125. * 邀请页面
  126. * @return mixed
  127. */
  128. public function invite()
  129. {
  130. $admin = session('admin');
  131. !$admin['user_id'] && $this->error('没有该权限');
  132. $user = UserDao::getUserOrEmptyById($admin['user_id']);
  133. empty($user) && $this->error('用户信息不存在');
  134. $this->assign('user_id', encode($admin['user_id']));
  135. return $this->fetch();
  136. }
  137. /**
  138. * 兜底
  139. * @return mixed
  140. */
  141. public function doudi()
  142. {
  143. return 22;
  144. if ($this->request->isAjax()) {
  145. $post = $this->request->post();
  146. $user = $this->model->findOrEmpty(['id' => $post['uid']]);
  147. empty($user) && $this->error('用户不存在');
  148. $post['active'] <= 0 && $this->error('参数错误');
  149. $path = trim_string($user['path'] . ',' . $user['id']);
  150. $path_explode = explode(',', $path);
  151. $insert = [];
  152. foreach ($path_explode as $value) {
  153. $insert[] = [
  154. 'active' => $post['active'],
  155. 'uid' => $value,
  156. 'ip' => $this->request->ip(),
  157. 'user_admin' => session('?admin.username') ? session('admin.username') : '',
  158. 'from_uid' => $post['uid'],
  159. 'failure_at' => date('Y-m-d H:i:s', time() + 86400 * 30),
  160. ];
  161. }
  162. $this->model->startTrans();
  163. try {
  164. $this->model->whereIn('id', $path)->save(['active_set' => ['inc', $post['active']], 'total_number' => ['inc', $post['active']], 'total_number_real' => ['inc', $post['active']], 'total_active' => ['inc', $post['active']]]);
  165. ActiveSet::insertAll($insert);
  166. $this->model->commit();
  167. } catch (\Exception $e) {
  168. $this->model->rollback();
  169. $this->error('失败');
  170. }
  171. $this->success('成功');
  172. }
  173. return $this->fetch();
  174. }
  175. /**
  176. * 增加余额(功能已屏蔽)
  177. * @return mixed
  178. */
  179. /*public function editmoney()
  180. {
  181. if ($this->request->isPost()) {
  182. $post = $this->request->post();
  183. $user = $this->model->findOrEmpty(['id' => $post['uid']]);
  184. $money = $post['money'];
  185. $type = $post['type'];
  186. empty($user) && $this->error('用户不存在');
  187. $this->model->startTrans();
  188. try {
  189. if ($type == 'more') {
  190. edit_user_money(7, $post['uid'], $money);
  191. } else {
  192. edit_user_money(8, $post['uid'], $money);
  193. }
  194. $this->model->commit();
  195. } catch (\Exception $e) {
  196. $this->model->rollback();
  197. $this->error('失败' . $e->getMessage());
  198. }
  199. $this->success('成功');
  200. }
  201. return $this->fetch();
  202. }*/
  203. /**
  204. * 修改手机号码
  205. * @return mixed
  206. */
  207. public function phoneset()
  208. {
  209. if ($this->request->isPost()) {
  210. $post = $this->request->post();
  211. try {
  212. validate(PhoneSet::class)->check($post);
  213. } catch (ValidateException $e) {
  214. $this->error($e->getMessage());
  215. }
  216. $userLogic = new UserLogic();
  217. $result = $userLogic->modifyPhone($post['id'], $post['phone']);
  218. if ($result !== true) {
  219. $this->error($result);
  220. }
  221. $this->success('成功');
  222. }
  223. $user = UserDao::getUserOrEmptyById($this->request['id']);
  224. $this->assign('info', $user);
  225. return $this->fetch('phoneset');
  226. }
  227. /*
  228. * 修改所属上级
  229. * @return mixed
  230. */
  231. public function modifypid()
  232. {
  233. if ($this->request->isPost()) {
  234. $post = $this->request->post();
  235. try {
  236. validate(ModifyPid::class)->check($post);
  237. } catch (ValidateException $e) {
  238. $this->error($e->getMessage());
  239. }
  240. $userLogic = new UserLogic();
  241. $result = $userLogic->modifypid($post['id'], $post['pid']);
  242. if ($result !== true) {
  243. $this->error($result);
  244. }
  245. $this->success('成功');
  246. }
  247. $user = UserDao::getUserOrEmptyById($this->request['id']);
  248. $this->assign('info', $user);
  249. return $this->fetch();
  250. }
  251. /**
  252. * 等级设置
  253. * @return mixed
  254. */
  255. public function levelset()
  256. {
  257. if ($this->request->isPost()) {
  258. $post = $this->request->post();
  259. $result = UserLogic::levelset($post);
  260. if ($result !== true) {
  261. $this->error($result);
  262. }
  263. $this->success('成功');
  264. }
  265. $user = UserDao::getUserOrEmptyById($this->request['id']);
  266. $this->assign('info', $user);
  267. return $this->fetch();
  268. }
  269. /**
  270. * 回收卡设置
  271. * @return mixed
  272. */
  273. public function recyclecountset()
  274. {
  275. if ($this->request->isPost()) {
  276. $post = $this->request->post();
  277. $result = UserLogic::recycleCountSet($post);
  278. if ($result !== true) {
  279. $this->error($result);
  280. }
  281. $this->success('成功');
  282. }
  283. $user = UserDao::getUserOrEmptyById($this->request['id']);
  284. $this->assign('info', $user);
  285. return $this->fetch();
  286. }
  287. /**
  288. * 末尾奖励(功能已注释)
  289. * @return mixed
  290. */
  291. /*public function moweiscore()
  292. {
  293. if ($this->request->isPost()) {
  294. $post = $this->request->post();
  295. $user = $this->model->findOrEmpty(['id' => $post['uid']]);
  296. $money = $post['money'];
  297. empty($user) && $this->error('用户不存在');
  298. $this->model->startTrans();
  299. try {
  300. edit_user_score(8, $post['uid'], $money);
  301. $this->model->commit();
  302. } catch (\Exception $e) {
  303. $this->model->rollback();
  304. $this->error('失败' . $e->getMessage());
  305. }
  306. $this->success('成功');
  307. }
  308. return $this->fetch();
  309. }*/
  310. /**
  311. * 余额明细
  312. * @param UserMoneyModel $model
  313. * @return mixed|\think\response\Json
  314. * @throws \think\db\exception\DataNotFoundException
  315. * @throws \think\db\exception\DbException
  316. * @throws \think\db\exception\ModelNotFoundException
  317. */
  318. public function moneyLog()
  319. {
  320. if ($this->request->isAjax()) {
  321. if (input('selectFields')) {
  322. return $this->selectList();
  323. }
  324. list($page, $limit, $where) = $this->buildTableParames();
  325. $where[] = ['uid', '=', $this->request->param('id', '')];
  326. list($count, $list) = MoneyLogLogic::getMoneyLog($page, $limit, $where, $this->sort);
  327. $data = [
  328. 'code' => 0,
  329. 'msg' => '',
  330. 'count' => $count,
  331. 'data' => $list,
  332. ];
  333. return json($data);
  334. }
  335. return $this->fetch();
  336. }
  337. /**
  338. * 查看上级
  339. * @return mixed|\think\response\Json
  340. */
  341. public function lookpidlevel()
  342. {
  343. if ($this->request->isAjax()) {
  344. $id = $this->request->param('id');
  345. list($count, $list) = UserLogic::lookpidlevel($id);
  346. $data = [
  347. 'code' => 0,
  348. 'msg' => '成功',
  349. 'count' => $count,
  350. 'data' => $list,
  351. ];
  352. return json($data);
  353. }
  354. return $this->fetch();
  355. }
  356. /**
  357. * 元宝明细(功能已失效)
  358. * @param CoinLog $model
  359. * @return mixed|\think\response\Json
  360. * @throws \think\db\exception\DataNotFoundException
  361. * @throws \think\db\exception\DbException
  362. * @throws \think\db\exception\ModelNotFoundException
  363. */
  364. /*public function coinLog(CoinLog $model)
  365. {
  366. if ($this->request->isAjax()) {
  367. if (input('selectFields')) {
  368. return $this->selectList();
  369. }
  370. list($page, $limit, $where) = $this->buildTableParames();
  371. $where[] = ['uid', '=', $this->request->param('id', '')];
  372. $count = $model
  373. ->where($where)
  374. ->count();
  375. $type_conf = config('type.coin');
  376. $list = $model
  377. ->where($where)
  378. ->withAttr('type', function ($value, $data) use ($type_conf) {
  379. return $type_conf[$value];
  380. })
  381. ->withAttr('coin', function ($value, $data) {
  382. if ($data['state'] == 2)
  383. $value = '-' . $value;
  384. return $value;
  385. })
  386. ->page($page, $limit)
  387. ->order($this->sort)
  388. ->select();
  389. $data = [
  390. 'code' => 0,
  391. 'msg' => '',
  392. 'count' => $count,
  393. 'data' => $list,
  394. ];
  395. return json($data);
  396. }
  397. return $this->fetch();
  398. }*/
  399. /**
  400. * 积分明细
  401. * @return mixed|\think\response\Json
  402. */
  403. public function scoreLog()
  404. {
  405. if ($this->request->isAjax()) {
  406. if (input('selectFields')) {
  407. return $this->selectList();
  408. }
  409. list($page, $limit, $where) = $this->buildTableParames();
  410. $where[] = ['uid', '=', $this->request->param('id', '')];
  411. list($count, $list) = ScoreLogLogic::scoreLog($page, $limit, $where, $this->sort);
  412. $data = [
  413. 'code' => 0,
  414. 'msg' => '',
  415. 'count' => $count,
  416. 'data' => $list,
  417. ];
  418. return json($data);
  419. }
  420. return $this->fetch();
  421. }
  422. /**
  423. * 活跃明细(功能已失效)
  424. * @param ActiveLog $model
  425. * @return mixed|\think\response\Json
  426. * @throws \think\db\exception\DataNotFoundException
  427. * @throws \think\db\exception\DbException
  428. * @throws \think\db\exception\ModelNotFoundException
  429. */
  430. /*public function activeLog(ActiveLogModel $model)
  431. {
  432. if ($this->request->isAjax()) {
  433. if (input('selectFields')) {
  434. return $this->selectList();
  435. }
  436. list($page, $limit, $where) = $this->buildTableParames();
  437. $where[] = ['uid', '=', $this->request->param('id', '')];
  438. $count = $model
  439. ->where($where)
  440. ->count();
  441. $type_conf = config('type.active');
  442. $list = $model
  443. ->where($where)
  444. ->withAttr('type', function ($value, $data) use ($type_conf) {
  445. return $type_conf[$value];
  446. })
  447. ->withAttr('active', function ($value, $data) {
  448. if ($data['state'] == 2)
  449. $value = '-' . $value;
  450. return $value;
  451. })
  452. ->page($page, $limit)
  453. ->order($this->sort)
  454. ->select();
  455. $data = [
  456. 'code' => 0,
  457. 'msg' => '',
  458. 'count' => $count,
  459. 'data' => $list,
  460. ];
  461. return json($data);
  462. }
  463. return $this->fetch();
  464. }*/
  465. /**
  466. * @NodeAnotation(title="导出")
  467. */
  468. public function export()
  469. {
  470. list($page, $limit, $where) = $this->buildTableParames();
  471. if (empty($where)) {
  472. $search = Cache::get("USER_EXPORT");
  473. $page = $search['page'];
  474. $limit = $search['limit'];
  475. $where = $search['where'];
  476. }
  477. $header = getExportHeader($this->model->getName(), $this->noExportFields);
  478. $list = UserLogic::getExportList($where, $page, $limit);
  479. $fileName = time();
  480. return Excel::exportData($list, $header, $fileName, 'xlsx');
  481. }
  482. /**
  483. * 团队充值(已失效)
  484. * @return mixed
  485. */
  486. /*public function teamincome(Request $request)
  487. {
  488. if ($this->request->isAjax()) {
  489. // if (($pid = $this->request->param('pid')) !== false && $pid)
  490. // $where[] = ['pid', '=', $this->request->param('pid', '')];
  491. $id = $this->request->param('id');
  492. // $path = Db::name('user')->where('id', $id)->value('path');
  493. $where = array();
  494. $where[] = ['team_id', '=', $id];
  495. $where[] = ['type', '=', 5];
  496. $count = Db::name('userteam_log')
  497. // ->withJoin('user', 'INNER')
  498. ->where($where)
  499. ->count();
  500. $list = Db::name('userteam_log')
  501. // ->withJoin('user', 'INNER')
  502. ->where($where)
  503. ->order($this->sort)
  504. ->select();
  505. $data = [
  506. 'code' => 0,
  507. 'msg' => '成功',
  508. 'count' => $count,
  509. 'data' => $list,
  510. ];
  511. return json($data);
  512. }
  513. return $this->fetch();
  514. }*/
  515. /*
  516. * 修改积分
  517. * @return mixed
  518. */
  519. public function modifyscore()
  520. {
  521. if ($this->request->isPost()) {
  522. $post = $this->request->post();
  523. try {
  524. validate(ModifyScore::class)->check($post);
  525. } catch (ValidateException $e) {
  526. $this->error($e->getMessage());
  527. }
  528. $userLogic = new UserLogic();
  529. $result = $userLogic->ModifyScore($post);
  530. if ($result !== true) {
  531. $this->error($result);
  532. }
  533. $this->success('成功');
  534. }
  535. $user = UserDao::getUserOrEmptyById($this->request['id']);
  536. $user['type_map'] = ScoreLogLogic::getTypeMap();
  537. $user['state_map'] = ScoreLogLogic::getStateMap();
  538. $this->assign('info', $user);
  539. return $this->fetch();
  540. }
  541. /*
  542. * 修改余额
  543. * @return mixed
  544. */
  545. public function modifymoney()
  546. {
  547. if ($this->request->isPost()) {
  548. $post = $this->request->post();
  549. try {
  550. validate(ModifyMoney::class)->check($post);
  551. } catch (ValidateException $e) {
  552. $this->error($e->getMessage());
  553. }
  554. $userLogic = new UserLogic();
  555. $result = $userLogic->ModifyMoney($post);
  556. if ($result !== true) {
  557. $this->error($result);
  558. }
  559. $this->success('成功');
  560. }
  561. $user = UserDao::getUserOrEmptyById($this->request['id']);
  562. $user['type_map'] = MoneyLogLogic::getTypeMap();
  563. $user['state_map'] = MoneyLogLogic::getStateMap();
  564. $this->assign('info', $user);
  565. return $this->fetch();
  566. }
  567. /**
  568. * 注销用户
  569. * @param $id
  570. */
  571. public function deluser($id)
  572. {
  573. if ($this->request->isAjax()) {
  574. $userLogic = new UserLogic();
  575. $flag = $userLogic->delUser($id);
  576. $flag === true ? $this->success('删除成功') : $this->success('删除失败');
  577. }
  578. }
  579. }