User.php 6.0 KB

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