User.php 19 KB

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