User.php 13 KB

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