User.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2020 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: https://thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
  12. // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
  13. // +----------------------------------------------------------------------
  14. namespace app\admin\controller;
  15. use think\admin\Controller;
  16. /**
  17. * 系统用户管理
  18. * Class User
  19. * @package app\admin\controller
  20. */
  21. class User extends Controller
  22. {
  23. /**
  24. * 绑定数据表
  25. * @var string
  26. */
  27. public $table = 'SystemUser';
  28. /**
  29. * 系统用户管理
  30. * @auth true
  31. * @menu true
  32. * @throws \think\db\exception\DataNotFoundException
  33. * @throws \think\db\exception\DbException
  34. * @throws \think\db\exception\ModelNotFoundException
  35. */
  36. public function index()
  37. {
  38. $this->title = '系统用户管理';
  39. $query = $this->_query($this->table)->like('username,phone,mail');
  40. $query->equal('status')->dateBetween('login_at,create_at');
  41. // 加载对应数据列表
  42. $this->type = input('type', 'all');
  43. if ($this->type === 'all') {
  44. $query->where(['is_deleted' => 0, 'status' => 1]);
  45. } elseif ($this->type = 'recycle') {
  46. $query->where(['is_deleted' => 0, 'status' => 0]);
  47. }
  48. // 列表排序并显示
  49. $query->order('sort desc,id desc')->page();
  50. }
  51. /**
  52. * 添加系统用户
  53. * @auth true
  54. * @throws \think\db\exception\DataNotFoundException
  55. * @throws \think\db\exception\DbException
  56. * @throws \think\db\exception\ModelNotFoundException
  57. */
  58. public function add()
  59. {
  60. $this->_applyFormToken();
  61. $this->_form($this->table, 'form');
  62. }
  63. /**
  64. * 编辑系统用户
  65. * @auth true
  66. * @throws \think\db\exception\DataNotFoundException
  67. * @throws \think\db\exception\DbException
  68. * @throws \think\db\exception\ModelNotFoundException
  69. */
  70. public function edit()
  71. {
  72. $this->_applyFormToken();
  73. $this->_form($this->table, 'form');
  74. }
  75. /**
  76. * 修改用户密码
  77. * @auth true
  78. * @throws \think\db\exception\DataNotFoundException
  79. * @throws \think\db\exception\DbException
  80. * @throws \think\db\exception\ModelNotFoundException
  81. */
  82. public function pass()
  83. {
  84. $this->_applyFormToken();
  85. if ($this->request->isGet()) {
  86. $this->verify = false;
  87. $this->_form($this->table, 'pass');
  88. } else {
  89. $data = $this->_vali([
  90. 'id.require' => '用户ID不能为空!',
  91. 'password.require' => '登录密码不能为空!',
  92. 'repassword.require' => '重复密码不能为空!',
  93. 'repassword.confirm:password' => '两次输入的密码不一致!',
  94. ]);
  95. if (data_save($this->table, ['id' => $data['id'], 'password' => md5($data['password'])], 'id')) {
  96. $this->success('密码修改成功,请使用新密码登录!', '');
  97. } else {
  98. $this->error('密码修改失败,请稍候再试!');
  99. }
  100. }
  101. }
  102. /**
  103. * 表单数据处理
  104. * @param array $data
  105. * @throws \think\db\exception\DataNotFoundException
  106. * @throws \think\db\exception\DbException
  107. * @throws \think\db\exception\ModelNotFoundException
  108. */
  109. protected function _form_filter(&$data)
  110. {
  111. if ($this->request->isPost()) {
  112. if (isset($data['id']) && $data['id'] > 0) {
  113. unset($data['username']);
  114. } else {
  115. // 检查登录账号是否出现重复
  116. if (empty($data['username'])) $this->error('登录账号不能为空!');
  117. $where = ['username' => $data['username'], 'is_deleted' => 0];
  118. if ($this->app->db->name($this->table)->where($where)->count() > 0) {
  119. $this->error("账号{$data['username']}已经存在,请使用其它账号!");
  120. }
  121. // 新添加的用户密码与账号相同
  122. $data['password'] = md5($data['username']);
  123. }
  124. // 账号权限绑定处理
  125. $data['authorize'] = (isset($data['authorize']) && is_array($data['authorize'])) ? join(',', $data['authorize']) : '';
  126. } else {
  127. $data['authorize'] = explode(',', $data['authorize'] ?? '');
  128. $this->authorizes = $this->app->db->name('SystemAuth')->where(['status' => 1])->order('sort desc,id desc')->select()->toArray();
  129. }
  130. }
  131. /**
  132. * 修改用户状态
  133. * @auth true
  134. * @throws \think\db\exception\DbException
  135. */
  136. public function state()
  137. {
  138. $this->_checkInput();
  139. $this->_applyFormToken();
  140. $this->_save($this->table, $this->_vali([
  141. 'status.in:0,1' => '状态值范围异常!',
  142. 'status.require' => '状态值不能为空!',
  143. ]));
  144. }
  145. /**
  146. * 删除系统用户
  147. * @auth true
  148. * @throws \think\db\exception\DbException
  149. */
  150. public function remove()
  151. {
  152. $this->_checkInput();
  153. $this->_applyFormToken();
  154. $this->_delete($this->table);
  155. }
  156. /**
  157. * 检查输入变量
  158. */
  159. private function _checkInput()
  160. {
  161. if (in_array('10000', explode(',', input('id', '')))) {
  162. $this->error('系统超级账号禁止删除!');
  163. }
  164. }
  165. }