User.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://demo.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 app\admin\service\NodeService;
  16. use library\Controller;
  17. use library\tools\Data;
  18. use think\Db;
  19. /**
  20. * 系统用户管理
  21. * Class User
  22. * @package app\admin\controller
  23. */
  24. class User extends Controller
  25. {
  26. /**
  27. * 指定当前数据表
  28. * @var string
  29. */
  30. public $table = 'SystemUser';
  31. /**
  32. * 系统用户管理
  33. * @auth true
  34. * @menu true
  35. * @throws \think\Exception
  36. * @throws \think\db\exception\DataNotFoundException
  37. * @throws \think\db\exception\ModelNotFoundException
  38. * @throws \think\exception\DbException
  39. * @throws \think\exception\PDOException
  40. */
  41. public function index()
  42. {
  43. $this->title = '系统用户管理';
  44. $query = $this->_query($this->table)->like('username,phone,mail')->equal('status');
  45. $query->dateBetween('login_at,create_at')->where(['is_deleted' => '0'])->order('id desc')->page();
  46. }
  47. /**
  48. * 添加系统用户
  49. * @auth true
  50. * @throws \think\Exception
  51. * @throws \think\db\exception\DataNotFoundException
  52. * @throws \think\db\exception\ModelNotFoundException
  53. * @throws \think\exception\DbException
  54. * @throws \think\exception\PDOException
  55. */
  56. public function add()
  57. {
  58. $this->applyCsrfToken();
  59. $this->_form($this->table, 'form');
  60. }
  61. /**
  62. * 编辑系统用户
  63. * @auth true
  64. * @throws \think\Exception
  65. * @throws \think\db\exception\DataNotFoundException
  66. * @throws \think\db\exception\ModelNotFoundException
  67. * @throws \think\exception\DbException
  68. * @throws \think\exception\PDOException
  69. */
  70. public function edit()
  71. {
  72. $this->applyCsrfToken();
  73. $this->_form($this->table, 'form');
  74. }
  75. /**
  76. * 修改用户密码
  77. * @auth true
  78. * @throws \think\Exception
  79. * @throws \think\exception\PDOException
  80. */
  81. public function pass()
  82. {
  83. $this->applyCsrfToken();
  84. if ($this->request->isGet()) {
  85. $this->verify = false;
  86. $this->_form($this->table, 'pass');
  87. } else {
  88. $post = $this->request->post();
  89. if ($post['password'] !== $post['repassword']) {
  90. $this->error('两次输入的密码不一致!');
  91. }
  92. $result = NodeService::checkpwd($post['password']);
  93. if (empty($result['code'])) $this->error($result['msg']);
  94. if (Data::save($this->table, ['id' => $post['id'], 'password' => md5($post['password'])], 'id')) {
  95. $this->success('密码修改成功,下次请使用新密码登录!', '');
  96. } else {
  97. $this->error('密码修改失败,请稍候再试!');
  98. }
  99. }
  100. }
  101. /**
  102. * 表单数据处理
  103. * @param array $data
  104. * @throws \think\db\exception\DataNotFoundException
  105. * @throws \think\db\exception\ModelNotFoundException
  106. * @throws \think\exception\DbException
  107. */
  108. public function _form_filter(&$data)
  109. {
  110. if ($this->request->isPost()) {
  111. // 刷新系统授权
  112. NodeService::applyUserAuth();
  113. // 用户权限处理
  114. $data['authorize'] = (isset($data['authorize']) && is_array($data['authorize'])) ? join(',', $data['authorize']) : '';
  115. // 用户账号重复检查
  116. if (isset($data['id'])) unset($data['username']);
  117. elseif (Db::name($this->table)->where(['username' => $data['username'], 'is_deleted' => '0'])->count() > 0) {
  118. $this->error("账号{$data['username']}已经存在,请使用其它账号!");
  119. }
  120. } else {
  121. $data['authorize'] = explode(',', isset($data['authorize']) ? $data['authorize'] : '');
  122. $this->authorizes = Db::name('SystemAuth')->where(['status' => '1'])->order('sort desc,id desc')->select();
  123. }
  124. }
  125. /**
  126. * 禁用系统用户
  127. * @auth true
  128. * @throws \think\Exception
  129. * @throws \think\exception\PDOException
  130. */
  131. public function forbid()
  132. {
  133. if (in_array('10000', explode(',', $this->request->post('id')))) {
  134. $this->error('系统超级账号禁止操作!');
  135. }
  136. $this->applyCsrfToken();
  137. $this->_save($this->table, ['status' => '0']);
  138. }
  139. /**
  140. * 启用系统用户
  141. * @auth true
  142. * @throws \think\Exception
  143. * @throws \think\exception\PDOException
  144. */
  145. public function resume()
  146. {
  147. $this->applyCsrfToken();
  148. $this->_save($this->table, ['status' => '1']);
  149. }
  150. /**
  151. * 删除系统用户
  152. * @auth true
  153. * @throws \think\Exception
  154. * @throws \think\exception\PDOException
  155. */
  156. public function remove()
  157. {
  158. if (in_array('10000', explode(',', $this->request->post('id')))) {
  159. $this->error('系统超级账号禁止删除!');
  160. }
  161. $this->applyCsrfToken();
  162. $this->_delete($this->table);
  163. }
  164. }