User.php 23 KB

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