User.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // |
  4. // +----------------------------------------------------------------------
  5. // |
  6. // +----------------------------------------------------------------------
  7. // |
  8. // +----------------------------------------------------------------------
  9. // |
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | 会员管理
  13. // +----------------------------------------------------------------------
  14. namespace app\admin\controller\user;
  15. use app\admin\model\user\User as UserModel;
  16. use app\admin\model\user\UserGroup;
  17. use app\common\controller\Backend;
  18. use app\common\library\Auth;
  19. use think\exception\ValidateException;
  20. class User extends Backend
  21. {
  22. protected $searchFields = 'id,username,nickname';
  23. //初始化
  24. protected function initialize()
  25. {
  26. parent::initialize();
  27. $this->modelClass = new UserModel;
  28. $this->assign('groupCache', UserGroup::column('name', 'id'));
  29. }
  30. /**
  31. * 会员列表
  32. */
  33. public function index()
  34. {
  35. $this->relationSearch = true;
  36. if ($this->request->isAjax()) {
  37. //如果发送的来源是Selectpage,则转发到Selectpage
  38. if ($this->request->request('keyField')) {
  39. return $this->selectpage();
  40. }
  41. [$page, $limit, $where, $sort, $order] = $this->buildTableParames();
  42. $list = $this->modelClass
  43. ->withJoin(['group' => ['id', 'name']])
  44. ->where($where)
  45. ->page($page, $limit)
  46. ->select();
  47. $total = $this->modelClass
  48. ->withJoin(['group'])
  49. ->where($where)
  50. ->order($sort, $order)
  51. ->count();
  52. $result = ["code" => 0, "count" => $total, "data" => $list];
  53. return json($result);
  54. }
  55. return $this->fetch();
  56. }
  57. /**
  58. * 会员增加
  59. */
  60. public function add()
  61. {
  62. if ($this->request->isPost()) {
  63. $params = $this->request->post("row/a");
  64. try {
  65. $this->validate($params, 'app\admin\validate\user\User');
  66. } catch (ValidateException $e) {
  67. $this->error($e->getMessage());
  68. }
  69. if (Auth::instance()->register($params['username'], $params['password'], $params['email'], $params['mobile'], $params)) {
  70. $this->success("添加会员成功!", url("index"));
  71. } else {
  72. $this->error(Auth::instance()->getError() ?: '添加会员失败!');
  73. }
  74. } else {
  75. return $this->fetch();
  76. }
  77. }
  78. /**
  79. * 会员编辑
  80. */
  81. public function edit()
  82. {
  83. if ($this->request->isPost()) {
  84. $userid = $this->request->param('id/d', 0);
  85. $params = $this->request->post("row/a");
  86. try {
  87. $this->validate($params, 'app\admin\validate\user\User.edit');
  88. } catch (ValidateException $e) {
  89. $this->error($e->getMessage());
  90. }
  91. //获取用户信息
  92. $row = UserModel::find($userid);
  93. if (empty($row)) {
  94. $this->error('该会员不存在!');
  95. }
  96. $row->save($params);
  97. $this->success("更新成功!", url("index"));
  98. } else {
  99. $userid = $this->request->param('id/d', 0);
  100. $data = $this->modelClass->find($userid);
  101. if (empty($data)) {
  102. $this->error("该会员不存在!");
  103. }
  104. $this->assign("data", $data);
  105. return $this->fetch();
  106. }
  107. }
  108. /**
  109. * 会员删除
  110. */
  111. public function del()
  112. {
  113. $ids = $this->request->param('id/a', null);
  114. if (empty($ids)) {
  115. $this->error('请选择需要删除的会员!');
  116. }
  117. if (!is_array($ids)) {
  118. $ids = [0 => $ids];
  119. }
  120. foreach ($ids as $uid) {
  121. Auth::instance()->delete($uid);
  122. }
  123. $this->success("删除成功!");
  124. }
  125. }