User.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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 moweiscore()
  274. {
  275. if ($this->request->isPost()) {
  276. $post = $this->request->post();
  277. $user = $this->model->findOrEmpty(['id' => $post['uid']]);
  278. $money = $post['money'];
  279. empty($user) && $this->error('用户不存在');
  280. $this->model->startTrans();
  281. try {
  282. edit_user_score(8, $post['uid'], $money);
  283. $this->model->commit();
  284. } catch (\Exception $e) {
  285. $this->model->rollback();
  286. $this->error('失败' . $e->getMessage());
  287. }
  288. $this->success('成功');
  289. }
  290. return $this->fetch();
  291. }*/
  292. /**
  293. * 余额明细
  294. * @param UserMoneyModel $model
  295. * @return mixed|\think\response\Json
  296. * @throws \think\db\exception\DataNotFoundException
  297. * @throws \think\db\exception\DbException
  298. * @throws \think\db\exception\ModelNotFoundException
  299. */
  300. public function moneyLog()
  301. {
  302. if ($this->request->isAjax()) {
  303. if (input('selectFields')) {
  304. return $this->selectList();
  305. }
  306. list($page, $limit, $where) = $this->buildTableParames();
  307. $where[] = ['uid', '=', $this->request->param('id', '')];
  308. list($count, $list) = MoneyLogLogic::getMoneyLog($page, $limit, $where, $this->sort);
  309. $data = [
  310. 'code' => 0,
  311. 'msg' => '',
  312. 'count' => $count,
  313. 'data' => $list,
  314. ];
  315. return json($data);
  316. }
  317. return $this->fetch();
  318. }
  319. /**
  320. * 查看上级
  321. * @return mixed|\think\response\Json
  322. */
  323. public function lookpidlevel()
  324. {
  325. if ($this->request->isAjax()) {
  326. $id = $this->request->param('id');
  327. list($count, $list) = UserLogic::lookpidlevel($id);
  328. $data = [
  329. 'code' => 0,
  330. 'msg' => '成功',
  331. 'count' => $count,
  332. 'data' => $list,
  333. ];
  334. return json($data);
  335. }
  336. return $this->fetch();
  337. }
  338. /**
  339. * 元宝明细(功能已失效)
  340. * @param CoinLog $model
  341. * @return mixed|\think\response\Json
  342. * @throws \think\db\exception\DataNotFoundException
  343. * @throws \think\db\exception\DbException
  344. * @throws \think\db\exception\ModelNotFoundException
  345. */
  346. /*public function coinLog(CoinLog $model)
  347. {
  348. if ($this->request->isAjax()) {
  349. if (input('selectFields')) {
  350. return $this->selectList();
  351. }
  352. list($page, $limit, $where) = $this->buildTableParames();
  353. $where[] = ['uid', '=', $this->request->param('id', '')];
  354. $count = $model
  355. ->where($where)
  356. ->count();
  357. $type_conf = config('type.coin');
  358. $list = $model
  359. ->where($where)
  360. ->withAttr('type', function ($value, $data) use ($type_conf) {
  361. return $type_conf[$value];
  362. })
  363. ->withAttr('coin', function ($value, $data) {
  364. if ($data['state'] == 2)
  365. $value = '-' . $value;
  366. return $value;
  367. })
  368. ->page($page, $limit)
  369. ->order($this->sort)
  370. ->select();
  371. $data = [
  372. 'code' => 0,
  373. 'msg' => '',
  374. 'count' => $count,
  375. 'data' => $list,
  376. ];
  377. return json($data);
  378. }
  379. return $this->fetch();
  380. }*/
  381. /**
  382. * 积分明细
  383. * @return mixed|\think\response\Json
  384. */
  385. public function scoreLog()
  386. {
  387. if ($this->request->isAjax()) {
  388. if (input('selectFields')) {
  389. return $this->selectList();
  390. }
  391. list($page, $limit, $where) = $this->buildTableParames();
  392. $where[] = ['uid', '=', $this->request->param('id', '')];
  393. list($count, $list) = ScoreLogLogic::scoreLog($page, $limit, $where, $this->sort);
  394. $data = [
  395. 'code' => 0,
  396. 'msg' => '',
  397. 'count' => $count,
  398. 'data' => $list,
  399. ];
  400. return json($data);
  401. }
  402. return $this->fetch();
  403. }
  404. /**
  405. * 活跃明细(功能已失效)
  406. * @param ActiveLog $model
  407. * @return mixed|\think\response\Json
  408. * @throws \think\db\exception\DataNotFoundException
  409. * @throws \think\db\exception\DbException
  410. * @throws \think\db\exception\ModelNotFoundException
  411. */
  412. /*public function activeLog(ActiveLogModel $model)
  413. {
  414. if ($this->request->isAjax()) {
  415. if (input('selectFields')) {
  416. return $this->selectList();
  417. }
  418. list($page, $limit, $where) = $this->buildTableParames();
  419. $where[] = ['uid', '=', $this->request->param('id', '')];
  420. $count = $model
  421. ->where($where)
  422. ->count();
  423. $type_conf = config('type.active');
  424. $list = $model
  425. ->where($where)
  426. ->withAttr('type', function ($value, $data) use ($type_conf) {
  427. return $type_conf[$value];
  428. })
  429. ->withAttr('active', function ($value, $data) {
  430. if ($data['state'] == 2)
  431. $value = '-' . $value;
  432. return $value;
  433. })
  434. ->page($page, $limit)
  435. ->order($this->sort)
  436. ->select();
  437. $data = [
  438. 'code' => 0,
  439. 'msg' => '',
  440. 'count' => $count,
  441. 'data' => $list,
  442. ];
  443. return json($data);
  444. }
  445. return $this->fetch();
  446. }*/
  447. /**
  448. * @NodeAnotation(title="导出")
  449. */
  450. public function export()
  451. {
  452. list($page, $limit, $where) = $this->buildTableParames();
  453. if (empty($where)) {
  454. $search = Cache::get("USER_EXPORT");
  455. $page = $search['page'];
  456. $limit = $search['limit'];
  457. $where = $search['where'];
  458. }
  459. $header = getExportHeader($this->model->getName(), $this->noExportFields);
  460. $list = UserLogic::getExportList($where, $page, $limit);
  461. $fileName = time();
  462. return Excel::exportData($list, $header, $fileName, 'xlsx');
  463. }
  464. /**
  465. * 团队充值(已失效)
  466. * @return mixed
  467. */
  468. /*public function teamincome(Request $request)
  469. {
  470. if ($this->request->isAjax()) {
  471. // if (($pid = $this->request->param('pid')) !== false && $pid)
  472. // $where[] = ['pid', '=', $this->request->param('pid', '')];
  473. $id = $this->request->param('id');
  474. // $path = Db::name('user')->where('id', $id)->value('path');
  475. $where = array();
  476. $where[] = ['team_id', '=', $id];
  477. $where[] = ['type', '=', 5];
  478. $count = Db::name('userteam_log')
  479. // ->withJoin('user', 'INNER')
  480. ->where($where)
  481. ->count();
  482. $list = Db::name('userteam_log')
  483. // ->withJoin('user', 'INNER')
  484. ->where($where)
  485. ->order($this->sort)
  486. ->select();
  487. $data = [
  488. 'code' => 0,
  489. 'msg' => '成功',
  490. 'count' => $count,
  491. 'data' => $list,
  492. ];
  493. return json($data);
  494. }
  495. return $this->fetch();
  496. }*/
  497. /*
  498. * 修改积分
  499. * @return mixed
  500. */
  501. public function modifyscore()
  502. {
  503. if ($this->request->isPost()) {
  504. $post = $this->request->post();
  505. try {
  506. validate(ModifyScore::class)->check($post);
  507. } catch (ValidateException $e) {
  508. $this->error($e->getMessage());
  509. }
  510. $userLogic = new UserLogic();
  511. $result = $userLogic->ModifyScore($post);
  512. if ($result !== true) {
  513. $this->error($result);
  514. }
  515. $this->success('成功');
  516. }
  517. $user = UserDao::getUserOrEmptyById($this->request['id']);
  518. $user['type_map'] = ScoreLogLogic::getTypeMap();
  519. $user['state_map'] = ScoreLogLogic::getStateMap();
  520. $this->assign('info', $user);
  521. return $this->fetch();
  522. }
  523. /*
  524. * 修改余额
  525. * @return mixed
  526. */
  527. public function modifymoney()
  528. {
  529. if ($this->request->isPost()) {
  530. $post = $this->request->post();
  531. try {
  532. validate(ModifyMoney::class)->check($post);
  533. } catch (ValidateException $e) {
  534. $this->error($e->getMessage());
  535. }
  536. $userLogic = new UserLogic();
  537. $result = $userLogic->ModifyMoney($post);
  538. if ($result !== true) {
  539. $this->error($result);
  540. }
  541. $this->success('成功');
  542. }
  543. $user = UserDao::getUserOrEmptyById($this->request['id']);
  544. $user['type_map'] = MoneyLogLogic::getTypeMap();
  545. $user['state_map'] = MoneyLogLogic::getStateMap();
  546. $this->assign('info', $user);
  547. return $this->fetch();
  548. }
  549. /**
  550. * 注销用户
  551. * @param $id
  552. */
  553. public function deluser($id)
  554. {
  555. if ($this->request->isAjax()) {
  556. $userLogic = new UserLogic();
  557. $flag = $userLogic->delUser($id);
  558. $flag === true ? $this->success('删除成功') : $this->success('删除失败');
  559. }
  560. }
  561. }