User.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  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\ModifyPid;
  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 modifypid()
  253. {
  254. if ($this->request->isPost()) {
  255. $post = $this->request->post();
  256. try {
  257. validate(ModifyPid::class)->check($post);
  258. } catch (ValidateException $e) {
  259. $this->error($e->getMessage());
  260. }
  261. $pid = $post['pid'];
  262. $uid = $post['id'];
  263. $user = $this->model->findOrEmpty(['id' => $uid]);
  264. empty($user) && $this->error('用户不存在');
  265. $userLogic = new UserLogic();
  266. $result = $userLogic->modifypid($uid, $pid);
  267. if ($result !== true) {
  268. $this->error($result);
  269. }
  270. $this->success('成功');
  271. }
  272. $user = $this->model->findOrEmpty(['id' => $this->request['id']]);
  273. $this->assign('info', $user);
  274. return $this->fetch();
  275. }
  276. /**
  277. * 等级设置
  278. * @return mixed
  279. */
  280. public function levelset()
  281. {
  282. if ($this->request->isPost()) {
  283. $post = $this->request->post();
  284. $level = $post['level'];
  285. if ($level > 4) {
  286. $this->error('最高等级4级');
  287. }
  288. $user = $this->model->findOrEmpty(['id' => $post['id']]);
  289. empty($user) && $this->error('用户不存在');
  290. $level_type = 1;
  291. if ($level > $user['level']) {
  292. $level_type = 2;
  293. }
  294. $this->model->startTrans();
  295. try {
  296. $this->model->where('id', $post['id'])->save(['level' => $level, 'level_type' => $level_type]);
  297. $this->model->commit();
  298. } catch (\Exception $e) {
  299. $this->model->rollback();
  300. $this->error('失败' . $e->getMessage());
  301. }
  302. $this->success('成功');
  303. }
  304. $user = $this->model->findOrEmpty(['id' => $this->request['id']]);
  305. // $user_info = Db::name('user', $id)->find();
  306. $this->assign('info', $user);
  307. return $this->fetch();
  308. }
  309. /**
  310. * 末尾奖励
  311. * @return mixed
  312. */
  313. public function moweiscore()
  314. {
  315. if ($this->request->isPost()) {
  316. $post = $this->request->post();
  317. $user = $this->model->findOrEmpty(['id' => $post['uid']]);
  318. $money = $post['money'];
  319. empty($user) && $this->error('用户不存在');
  320. $this->model->startTrans();
  321. try {
  322. edit_user_score(8, $post['uid'], $money);
  323. $this->model->commit();
  324. } catch (\Exception $e) {
  325. $this->model->rollback();
  326. $this->error('失败' . $e->getMessage());
  327. }
  328. $this->success('成功');
  329. }
  330. return $this->fetch();
  331. }
  332. /**
  333. * 余额明细
  334. * @param MoneyLog $model
  335. * @return mixed|\think\response\Json
  336. * @throws \think\db\exception\DataNotFoundException
  337. * @throws \think\db\exception\DbException
  338. * @throws \think\db\exception\ModelNotFoundException
  339. */
  340. public function moneyLog(UserMoneyModel $model)
  341. {
  342. if ($this->request->isAjax()) {
  343. if (input('selectFields')) {
  344. return $this->selectList();
  345. }
  346. list($page, $limit, $where) = $this->buildTableParames();
  347. $where[] = ['uid', '=', $this->request->param('id', '')];
  348. $count = $model
  349. ->where($where)
  350. ->count();
  351. $type_conf = config('type.money');
  352. $list = $model
  353. ->where($where)
  354. ->withAttr('type', function ($value, $data) use ($type_conf) {
  355. return $type_conf[$value];
  356. })
  357. ->withAttr('money', function ($value, $data) {
  358. if ($data['state'] == 2)
  359. $value = '-' . $value;
  360. return $value;
  361. })
  362. ->page($page, $limit)
  363. ->order($this->sort)
  364. ->select();
  365. $data = [
  366. 'code' => 0,
  367. 'msg' => '',
  368. 'count' => $count,
  369. 'data' => $list,
  370. ];
  371. return json($data);
  372. }
  373. return $this->fetch();
  374. }
  375. /**
  376. * 查看上级
  377. * @param Request $request
  378. * @param UpgradeLog $upgradeLog
  379. * @return mixed
  380. * @throws \think\db\exception\DataNotFoundException
  381. * @throws \think\db\exception\DbException
  382. * @throws \think\db\exception\ModelNotFoundException
  383. */
  384. public function lookpidlevel(Request $request)
  385. {
  386. if ($this->request->isAjax()) {
  387. // if (($pid = $this->request->param('pid')) !== false && $pid)
  388. // $where[] = ['pid', '=', $this->request->param('pid', '')];
  389. $id = $this->request->param('id');
  390. $path = Db::name('user')->where('id', $id)->value('path');
  391. $arr = explode(',', $path);
  392. $ids = $arr;
  393. $ids = implode(',', $ids);
  394. $order = 'field(id,' . $ids . ')';
  395. // return User::whereIn('id',$ids)->order(Db::raw($order))->select();
  396. $where = array();
  397. $where[] = ['uid', 'in', $arr];
  398. sr_log($where);
  399. $count = $this->model
  400. ->withJoin('userData', 'INNER')
  401. ->where($where)
  402. ->count();
  403. $list = $this->model
  404. ->withJoin('userData', 'INNER')
  405. ->where($where)
  406. ->order(Db::raw($order))
  407. ->select();
  408. $data = [
  409. 'code' => 0,
  410. 'msg' => '成功',
  411. 'count' => $count,
  412. 'data' => $list,
  413. ];
  414. return json($data);
  415. }
  416. return $this->fetch();
  417. }
  418. /**
  419. * 元宝明细
  420. * @param CoinLog $model
  421. * @return mixed|\think\response\Json
  422. * @throws \think\db\exception\DataNotFoundException
  423. * @throws \think\db\exception\DbException
  424. * @throws \think\db\exception\ModelNotFoundException
  425. */
  426. public function coinLog(CoinLog $model)
  427. {
  428. if ($this->request->isAjax()) {
  429. if (input('selectFields')) {
  430. return $this->selectList();
  431. }
  432. list($page, $limit, $where) = $this->buildTableParames();
  433. $where[] = ['uid', '=', $this->request->param('id', '')];
  434. $count = $model
  435. ->where($where)
  436. ->count();
  437. $type_conf = config('type.coin');
  438. $list = $model
  439. ->where($where)
  440. ->withAttr('type', function ($value, $data) use ($type_conf) {
  441. return $type_conf[$value];
  442. })
  443. ->withAttr('coin', function ($value, $data) {
  444. if ($data['state'] == 2)
  445. $value = '-' . $value;
  446. return $value;
  447. })
  448. ->page($page, $limit)
  449. ->order($this->sort)
  450. ->select();
  451. $data = [
  452. 'code' => 0,
  453. 'msg' => '',
  454. 'count' => $count,
  455. 'data' => $list,
  456. ];
  457. return json($data);
  458. }
  459. return $this->fetch();
  460. }
  461. /**
  462. * 积分明细
  463. * @param ScoreLog $model
  464. * @return mixed|\think\response\Json
  465. * @throws \think\db\exception\DataNotFoundException
  466. * @throws \think\db\exception\DbException
  467. * @throws \think\db\exception\ModelNotFoundException
  468. */
  469. public function scoreLog(ScoreLogModel $model)
  470. {
  471. if ($this->request->isAjax()) {
  472. if (input('selectFields')) {
  473. return $this->selectList();
  474. }
  475. list($page, $limit, $where) = $this->buildTableParames();
  476. $where[] = ['uid', '=', $this->request->param('id', '')];
  477. $count = $model
  478. ->where($where)
  479. ->count();
  480. $type_conf = config('type.score');
  481. $list = $model
  482. ->where($where)
  483. ->withAttr('type', function ($value, $data) use ($type_conf) {
  484. return $type_conf[$value];
  485. })
  486. ->withAttr('score', function ($value, $data) {
  487. if ($data['state'] == 2)
  488. $value = '-' . $value;
  489. return $value;
  490. })
  491. ->page($page, $limit)
  492. ->order($this->sort)
  493. ->select();
  494. $data = [
  495. 'code' => 0,
  496. 'msg' => '',
  497. 'count' => $count,
  498. 'data' => $list,
  499. ];
  500. return json($data);
  501. }
  502. return $this->fetch();
  503. }
  504. /**
  505. * 积分明细
  506. * @param ActiveLog $model
  507. * @return mixed|\think\response\Json
  508. * @throws \think\db\exception\DataNotFoundException
  509. * @throws \think\db\exception\DbException
  510. * @throws \think\db\exception\ModelNotFoundException
  511. */
  512. public function activeLog(ActiveLog $model)
  513. {
  514. if ($this->request->isAjax()) {
  515. if (input('selectFields')) {
  516. return $this->selectList();
  517. }
  518. list($page, $limit, $where) = $this->buildTableParames();
  519. $where[] = ['uid', '=', $this->request->param('id', '')];
  520. $count = $model
  521. ->where($where)
  522. ->count();
  523. $type_conf = config('type.active');
  524. $list = $model
  525. ->where($where)
  526. ->withAttr('type', function ($value, $data) use ($type_conf) {
  527. return $type_conf[$value];
  528. })
  529. ->withAttr('active', function ($value, $data) {
  530. if ($data['state'] == 2)
  531. $value = '-' . $value;
  532. return $value;
  533. })
  534. ->page($page, $limit)
  535. ->order($this->sort)
  536. ->select();
  537. $data = [
  538. 'code' => 0,
  539. 'msg' => '',
  540. 'count' => $count,
  541. 'data' => $list,
  542. ];
  543. return json($data);
  544. }
  545. return $this->fetch();
  546. }
  547. /**
  548. * @NodeAnotation(title="导出")
  549. */
  550. public function export()
  551. {
  552. list($page, $limit, $where) = $this->buildTableParames();
  553. $tableName = $this->model->getName();
  554. $tableName = CommonTool::humpToLine(lcfirst($tableName));
  555. $prefix = config('database.connections.mysql.prefix');
  556. $dbList = Db::query("show full columns from {$prefix}{$tableName}");
  557. $header = [];
  558. foreach ($dbList as $vo) {
  559. $comment = !empty($vo['Comment']) ? $vo['Comment'] : $vo['Field'];
  560. if (!in_array($vo['Field'], $this->noExportFields)) {
  561. $header[] = [$comment, $vo['Field']];
  562. }
  563. }
  564. $list = $this->model
  565. ->where($where)
  566. ->withJoin('userData', 'INNER')
  567. ->where($where)
  568. ->limit(100000)
  569. ->order('id', 'desc')
  570. ->select()
  571. ->toArray();
  572. $fileName = time();
  573. return Excel::exportData($list, $header, $fileName, 'xlsx');
  574. }
  575. public function teamincome(Request $request)
  576. {
  577. if ($this->request->isAjax()) {
  578. // if (($pid = $this->request->param('pid')) !== false && $pid)
  579. // $where[] = ['pid', '=', $this->request->param('pid', '')];
  580. $id = $this->request->param('id');
  581. // $path = Db::name('user')->where('id', $id)->value('path');
  582. $where = array();
  583. $where[] = ['team_id', '=', $id];
  584. $where[] = ['type', '=', 5];
  585. $count = Db::name('userteam_log')
  586. // ->withJoin('user', 'INNER')
  587. ->where($where)
  588. ->count();
  589. $list = Db::name('userteam_log')
  590. // ->withJoin('user', 'INNER')
  591. ->where($where)
  592. ->order($this->sort)
  593. ->select();
  594. $data = [
  595. 'code' => 0,
  596. 'msg' => '成功',
  597. 'count' => $count,
  598. 'data' => $list,
  599. ];
  600. return json($data);
  601. }
  602. return $this->fetch();
  603. }
  604. }