User.php 20 KB

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