User.php 19 KB

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