User.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. <?php
  2. namespace app\admin\controller\user;
  3. use app\admin\logic\GreenScoreLogLogic;
  4. use app\admin\logic\MoneyLogLogic;
  5. use app\admin\logic\ScoreLogLogic;
  6. use app\admin\logic\UserLogic;
  7. use app\common\model\User as UserModel;
  8. use app\admin\traits\Curd;
  9. use app\common\controller\AdminController;
  10. use app\common\model\UserMoneyModel;
  11. use app\Request;
  12. use app\validate\admin\user\user\ModifyGreenScore;
  13. use app\validate\admin\user\user\ModifyMoney;
  14. use app\validate\admin\user\user\ModifyScore;
  15. use app\validate\admin\user\user\ModifyPid;
  16. use app\validate\admin\user\user\PhoneSet;
  17. use jianyan\excel\Excel;
  18. use think\App;
  19. use think\exception\ValidateException;
  20. use think\facade\Cache;
  21. use app\admin\model\dao\User as UserDao;
  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. Cache::set("USER_EXPORT", ['page' => $page, 'limit' => $limit, 'where' => $where]);
  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. list($result, $msg) = UserLogic::forbid($id);
  60. if ($result !== true) {
  61. $this->error($msg);
  62. }
  63. $this->success($msg);
  64. }
  65. /**
  66. * 启用用户
  67. * @param $id
  68. */
  69. public function enable($id)
  70. {
  71. list($result, $msg) = UserLogic::enable($id);
  72. if ($result !== true) {
  73. $this->error($msg);
  74. }
  75. $this->success($msg);
  76. }
  77. /**
  78. * 会员详情
  79. * @param Request $request
  80. * @return mixed
  81. */
  82. public function details(Request $request)
  83. {
  84. $id = $request->param('id');
  85. $info = UserLogic::getUserDetail($id);
  86. $this->assign('info', $info);
  87. return $this->fetch();
  88. }
  89. /**
  90. * 邀请页面
  91. * @return mixed
  92. */
  93. public function invite()
  94. {
  95. $admin = session('admin');
  96. !$admin['user_id'] && $this->error('没有该权限');
  97. $user = UserDao::getUserOrEmptyById($admin['user_id']);
  98. empty($user) && $this->error('用户信息不存在');
  99. $this->assign('user_id', encode($admin['user_id']));
  100. return $this->fetch();
  101. }
  102. /**
  103. * 兜底
  104. * @return mixed
  105. */
  106. public function doudi()
  107. {
  108. return 22;
  109. if ($this->request->isAjax()) {
  110. $post = $this->request->post();
  111. $user = $this->model->findOrEmpty(['id' => $post['uid']]);
  112. empty($user) && $this->error('用户不存在');
  113. $post['active'] <= 0 && $this->error('参数错误');
  114. $path = trim_string($user['path'] . ',' . $user['id']);
  115. $path_explode = explode(',', $path);
  116. $insert = [];
  117. foreach ($path_explode as $value) {
  118. $insert[] = [
  119. 'active' => $post['active'],
  120. 'uid' => $value,
  121. 'ip' => $this->request->ip(),
  122. 'user_admin' => session('?admin.username') ? session('admin.username') : '',
  123. 'from_uid' => $post['uid'],
  124. 'failure_at' => date('Y-m-d H:i:s', time() + 86400 * 30),
  125. ];
  126. }
  127. $this->model->startTrans();
  128. try {
  129. $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']]]);
  130. ActiveSet::insertAll($insert);
  131. $this->model->commit();
  132. } catch (\Exception $e) {
  133. $this->model->rollback();
  134. $this->error('失败');
  135. }
  136. $this->success('成功');
  137. }
  138. return $this->fetch();
  139. }
  140. /**
  141. * 修改手机号码
  142. * @return mixed
  143. */
  144. public function phoneset()
  145. {
  146. if ($this->request->isPost()) {
  147. $post = $this->request->post();
  148. try {
  149. validate(PhoneSet::class)->check($post);
  150. } catch (ValidateException $e) {
  151. $this->error($e->getMessage());
  152. }
  153. $userLogic = new UserLogic();
  154. $result = $userLogic->modifyPhone($post['id'], $post['phone']);
  155. if ($result !== true) {
  156. $this->error($result);
  157. }
  158. $this->success('成功');
  159. }
  160. $user = UserDao::getUserOrEmptyById($this->request['id']);
  161. $this->assign('info', $user);
  162. return $this->fetch('phoneset');
  163. }
  164. /*
  165. * 修改所属上级
  166. * @return mixed
  167. */
  168. public function modifypid()
  169. {
  170. if ($this->request->isPost()) {
  171. $post = $this->request->post();
  172. try {
  173. validate(ModifyPid::class)->check($post);
  174. } catch (ValidateException $e) {
  175. $this->error($e->getMessage());
  176. }
  177. $userLogic = new UserLogic();
  178. $result = $userLogic->modifypid($post['id'], $post['pid']);
  179. if ($result !== true) {
  180. $this->error($result);
  181. }
  182. $this->success('成功');
  183. }
  184. $user = UserDao::getUserOrEmptyById($this->request['id']);
  185. $this->assign('info', $user);
  186. return $this->fetch();
  187. }
  188. /**
  189. * 等级设置
  190. * @return mixed
  191. */
  192. public function levelset()
  193. {
  194. if ($this->request->isPost()) {
  195. $post = $this->request->post();
  196. $result = UserLogic::levelset($post);
  197. if ($result !== true) {
  198. $this->error($result);
  199. }
  200. $this->success('成功');
  201. }
  202. $user = UserDao::getUserOrEmptyById($this->request['id']);
  203. $this->assign('info', $user);
  204. return $this->fetch();
  205. }
  206. /**
  207. * 回收卡设置
  208. * @return mixed
  209. */
  210. public function recyclecountset()
  211. {
  212. if ($this->request->isPost()) {
  213. $post = $this->request->post();
  214. $result = UserLogic::recycleCountSet($post);
  215. if ($result !== true) {
  216. $this->error($result);
  217. }
  218. $this->success('成功');
  219. }
  220. $user = UserDao::getUserOrEmptyById($this->request['id']);
  221. $this->assign('info', $user);
  222. return $this->fetch();
  223. }
  224. /**
  225. * 余额明细
  226. * @param UserMoneyModel $model
  227. * @return mixed|\think\response\Json
  228. * @throws \think\db\exception\DataNotFoundException
  229. * @throws \think\db\exception\DbException
  230. * @throws \think\db\exception\ModelNotFoundException
  231. */
  232. public function moneyLog()
  233. {
  234. if ($this->request->isAjax()) {
  235. if (input('selectFields')) {
  236. return $this->selectList();
  237. }
  238. list($page, $limit, $where) = $this->buildTableParames();
  239. $where[] = ['uid', '=', $this->request->param('id', '')];
  240. list($count, $list) = MoneyLogLogic::getMoneyLog($page, $limit, $where, $this->sort);
  241. $data = [
  242. 'code' => 0,
  243. 'msg' => '',
  244. 'count' => $count,
  245. 'data' => $list,
  246. ];
  247. return json($data);
  248. }
  249. return $this->fetch();
  250. }
  251. /**
  252. * 查看上级
  253. * @return mixed|\think\response\Json
  254. */
  255. public function lookpidlevel()
  256. {
  257. if ($this->request->isAjax()) {
  258. $id = $this->request->param('id');
  259. list($count, $list) = UserLogic::lookpidlevel($id);
  260. $data = [
  261. 'code' => 0,
  262. 'msg' => '成功',
  263. 'count' => $count,
  264. 'data' => $list,
  265. ];
  266. return json($data);
  267. }
  268. return $this->fetch();
  269. }
  270. /**
  271. * 积分明细
  272. * @return mixed|\think\response\Json
  273. */
  274. public function scoreLog()
  275. {
  276. if ($this->request->isAjax()) {
  277. if (input('selectFields')) {
  278. return $this->selectList();
  279. }
  280. list($page, $limit, $where) = $this->buildTableParames();
  281. $where[] = ['uid', '=', $this->request->param('id', '')];
  282. list($count, $list) = ScoreLogLogic::scoreLog($page, $limit, $where, $this->sort);
  283. $data = [
  284. 'code' => 0,
  285. 'msg' => '',
  286. 'count' => $count,
  287. 'data' => $list,
  288. ];
  289. return json($data);
  290. }
  291. return $this->fetch();
  292. }
  293. /**
  294. * 活跃明细(功能已失效)
  295. * @param ActiveLog $model
  296. * @return mixed|\think\response\Json
  297. * @throws \think\db\exception\DataNotFoundException
  298. * @throws \think\db\exception\DbException
  299. * @throws \think\db\exception\ModelNotFoundException
  300. */
  301. /*public function activeLog(ActiveLogModel $model)
  302. {
  303. if ($this->request->isAjax()) {
  304. if (input('selectFields')) {
  305. return $this->selectList();
  306. }
  307. list($page, $limit, $where) = $this->buildTableParames();
  308. $where[] = ['uid', '=', $this->request->param('id', '')];
  309. $count = $model
  310. ->where($where)
  311. ->count();
  312. $type_conf = config('type.active');
  313. $list = $model
  314. ->where($where)
  315. ->withAttr('type', function ($value, $data) use ($type_conf) {
  316. return $type_conf[$value];
  317. })
  318. ->withAttr('active', function ($value, $data) {
  319. if ($data['state'] == 2)
  320. $value = '-' . $value;
  321. return $value;
  322. })
  323. ->page($page, $limit)
  324. ->order($this->sort)
  325. ->select();
  326. $data = [
  327. 'code' => 0,
  328. 'msg' => '',
  329. 'count' => $count,
  330. 'data' => $list,
  331. ];
  332. return json($data);
  333. }
  334. return $this->fetch();
  335. }*/
  336. /**
  337. * @NodeAnotation(title="导出")
  338. */
  339. public function export()
  340. {
  341. list($page, $limit, $where) = $this->buildTableParames();
  342. if (empty($where)) {
  343. $search = Cache::get("USER_EXPORT");
  344. $page = $search['page'];
  345. $limit = $search['limit'];
  346. $where = $search['where'];
  347. }
  348. $header = getExportHeader($this->model->getName(), $this->noExportFields);
  349. $list = UserLogic::getExportList($where, $page, $limit);
  350. $fileName = time();
  351. return Excel::exportData($list, $header, $fileName, 'xlsx');
  352. }
  353. /*
  354. * 修改积分
  355. * @return mixed
  356. */
  357. public function modifyscore()
  358. {
  359. if ($this->request->isPost()) {
  360. $post = $this->request->post();
  361. try {
  362. validate(ModifyScore::class)->check($post);
  363. } catch (ValidateException $e) {
  364. $this->error($e->getMessage());
  365. }
  366. $userLogic = new UserLogic();
  367. $result = $userLogic->ModifyScore($post);
  368. if ($result !== true) {
  369. $this->error($result);
  370. }
  371. $this->success('成功');
  372. }
  373. $user = UserDao::getUserOrEmptyById($this->request['id']);
  374. $user['type_map'] = ScoreLogLogic::getTypeMap();
  375. $user['state_map'] = ScoreLogLogic::getStateMap();
  376. $this->assign('info', $user);
  377. return $this->fetch();
  378. }
  379. /*
  380. * 修改余额
  381. * @return mixed
  382. */
  383. public function modifymoney()
  384. {
  385. if ($this->request->isPost()) {
  386. $post = $this->request->post();
  387. try {
  388. validate(ModifyMoney::class)->check($post);
  389. } catch (ValidateException $e) {
  390. $this->error($e->getMessage());
  391. }
  392. $userLogic = new UserLogic();
  393. $result = $userLogic->ModifyMoney($post);
  394. if ($result !== true) {
  395. $this->error($result);
  396. }
  397. $this->success('成功');
  398. }
  399. $user = UserDao::getUserOrEmptyById($this->request['id']);
  400. $user['type_map'] = MoneyLogLogic::getTypeMap();
  401. $user['state_map'] = MoneyLogLogic::getStateMap();
  402. $this->assign('info', $user);
  403. return $this->fetch();
  404. }
  405. /*
  406. * 修改余额
  407. * @return mixed
  408. */
  409. public function modifygreenscore()
  410. {
  411. if ($this->request->isPost()) {
  412. $post = $this->request->post();
  413. try {
  414. validate(ModifyGreenScore::class)->check($post);
  415. } catch (ValidateException $e) {
  416. $this->error($e->getMessage());
  417. }
  418. $userLogic = new UserLogic();
  419. $result = $userLogic->ModifyGreenScore($post);
  420. if ($result !== true) {
  421. $this->error($result);
  422. }
  423. $this->success('成功');
  424. }
  425. $user = UserDao::getUserOrEmptyById($this->request['id']);
  426. $user['type_map'] = GreenScoreLogLogic::getTypeMap();
  427. $user['state_map'] = GreenScoreLogLogic::getStateMap();
  428. $this->assign('info', $user);
  429. return $this->fetch();
  430. }
  431. /**
  432. * 注销用户
  433. * @param $id
  434. */
  435. public function deluser($id)
  436. {
  437. if ($this->request->isAjax()) {
  438. $userLogic = new UserLogic();
  439. $flag = $userLogic->delUser($id);
  440. $flag === true ? $this->success('删除成功') : $this->success('删除失败');
  441. }
  442. }
  443. }