User.php 21 KB

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