User.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 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')->equal('status');
  40. $query->dateBetween('login_at,create_at')->where(['is_deleted' => '0'])->order('id desc')->page();
  41. }
  42. /**
  43. * 添加系统用户
  44. * @auth true
  45. * @throws \think\db\exception\DataNotFoundException
  46. * @throws \think\db\exception\DbException
  47. * @throws \think\db\exception\ModelNotFoundException
  48. */
  49. public function add()
  50. {
  51. $this->_applyFormToken();
  52. $this->_form($this->table, 'form');
  53. }
  54. /**
  55. * 编辑系统用户
  56. * @auth true
  57. * @throws \think\db\exception\DataNotFoundException
  58. * @throws \think\db\exception\DbException
  59. * @throws \think\db\exception\ModelNotFoundException
  60. */
  61. public function edit()
  62. {
  63. $this->_applyFormToken();
  64. $this->_form($this->table, 'form');
  65. }
  66. /**
  67. * 修改用户密码
  68. * @auth true
  69. * @throws \think\db\exception\DataNotFoundException
  70. * @throws \think\db\exception\DbException
  71. * @throws \think\db\exception\ModelNotFoundException
  72. */
  73. public function pass()
  74. {
  75. $this->_applyFormToken();
  76. if ($this->request->isGet()) {
  77. $this->verify = false;
  78. $this->_form($this->table, 'pass');
  79. } else {
  80. $post = $this->request->post();
  81. if ($post['password'] !== $post['repassword']) {
  82. $this->error('两次输入的密码不一致!');
  83. }
  84. if (data_save($this->table, ['id' => $post['id'], 'password' => md5($post['password'])], 'id')) {
  85. $this->success('密码修改成功,下次请使用新密码登录!', '');
  86. } else {
  87. $this->error('密码修改失败,请稍候再试!');
  88. }
  89. }
  90. }
  91. /**
  92. * 表单数据处理
  93. * @param array $data
  94. * @throws \think\db\exception\DataNotFoundException
  95. * @throws \think\db\exception\DbException
  96. * @throws \think\db\exception\ModelNotFoundException
  97. */
  98. protected function _form_filter(&$data)
  99. {
  100. if ($this->request->isPost()) {
  101. // 用户权限处理
  102. $data['authorize'] = (isset($data['authorize']) && is_array($data['authorize'])) ? join(',', $data['authorize']) : '';
  103. // 用户账号重复检查
  104. if (isset($data['id']))
  105. unset($data['username']);
  106. elseif ($this->app->db->name($this->table)->where(['username' => $data['username'], 'is_deleted' => '0'])->count() > 0) {
  107. $this->error("账号{$data['username']}已经存在,请使用其它账号!");
  108. }
  109. } else {
  110. $data['authorize'] = explode(',', isset($data['authorize']) ? $data['authorize'] : '');
  111. $this->authorizes = $this->app->db->name('SystemAuth')->where(['status' => '1'])->order('sort desc,id desc')->select();
  112. }
  113. }
  114. /**
  115. * 修改用户状态
  116. * @auth true
  117. * @throws \think\db\exception\DbException
  118. */
  119. public function state()
  120. {
  121. if (in_array('10000', explode(',', $this->request->post('id')))) {
  122. $this->error('系统超级账号禁止操作!');
  123. }
  124. $this->_applyFormToken();
  125. $this->_save($this->table, ['status' => intval(input('status'))]);
  126. }
  127. /**
  128. * 删除系统用户
  129. * @auth true
  130. * @throws \think\db\exception\DbException
  131. */
  132. public function remove()
  133. {
  134. if (in_array('10000', explode(',', $this->request->post('id')))) {
  135. $this->error('系统超级账号禁止删除!');
  136. }
  137. $this->_applyFormToken();
  138. $this->_delete($this->table);
  139. }
  140. }