User.php 19 KB

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