User.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2022 广州楚才信息科技有限公司 [ 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::items('身份权限');
  45. }, function (QueryHelper $query) {
  46. // 加载对应数据列表
  47. $query->where(['is_deleted' => 0, 'status' => intval($this->type === 'index')]);
  48. // 关联用户身份资料
  49. $query->with(['userinfo' => function (Relation $relation) {
  50. $relation->field('code,name,content');
  51. }]);
  52. // 数据列表搜索过滤
  53. $query->equal('status,usertype')->dateBetween('login_at,create_at');
  54. $query->like('username|nickname#username,contact_phone#phone,contact_mail#mail');
  55. });
  56. }
  57. /**
  58. * 添加系统用户
  59. * @auth true
  60. */
  61. public function add()
  62. {
  63. SystemUser::mForm('form');
  64. }
  65. /**
  66. * 编辑系统用户
  67. * @auth true
  68. */
  69. public function edit()
  70. {
  71. SystemUser::mForm('form');
  72. }
  73. /**
  74. * 修改用户密码
  75. * @auth true
  76. */
  77. public function pass()
  78. {
  79. $this->_applyFormToken();
  80. if ($this->request->isGet()) {
  81. $this->verify = false;
  82. SystemUser::mForm('pass');
  83. } else {
  84. $data = $this->_vali([
  85. 'id.require' => '用户ID不能为空!',
  86. 'password.require' => '登录密码不能为空!',
  87. 'repassword.require' => '重复密码不能为空!',
  88. 'repassword.confirm:password' => '两次输入的密码不一致!',
  89. ]);
  90. $user = SystemUser::mk()->findOrEmpty($data['id']);
  91. if ($user->isExists() && $user->save(['password' => md5($data['password'])])) {
  92. sysoplog('系统用户管理', "修改用户[{$data['id']}]密码成功");
  93. $this->success('密码修改成功,请使用新密码登录!', '');
  94. } else {
  95. $this->error('密码修改失败,请稍候再试!');
  96. }
  97. }
  98. }
  99. /**
  100. * 表单数据处理
  101. * @param array $data
  102. * @throws \think\db\exception\DataNotFoundException
  103. * @throws \think\db\exception\DbException
  104. * @throws \think\db\exception\ModelNotFoundException
  105. */
  106. protected function _form_filter(array &$data)
  107. {
  108. if ($this->request->isPost()) {
  109. // 账号权限绑定处理
  110. $data['authorize'] = arr2str($data['authorize'] ?? []);
  111. if (isset($data['id']) && $data['id'] > 0) {
  112. unset($data['username']);
  113. } else {
  114. // 检查账号是否重复
  115. if (empty($data['username'])) {
  116. $this->error('登录账号不能为空!');
  117. }
  118. $map = ['username' => $data['username'], 'is_deleted' => 0];
  119. if (SystemUser::mk()->where($map)->count() > 0) {
  120. $this->error("账号已经存在,请使用其它账号!");
  121. }
  122. // 新添加的用户密码与账号相同
  123. $data['password'] = md5($data['username']);
  124. }
  125. } else {
  126. // 权限绑定处理
  127. $data['authorize'] = str2arr($data['authorize'] ?? '');
  128. // 用户身份数据
  129. $this->bases = SystemBase::items('身份权限');
  130. // 用户权限管理
  131. $this->superName = AdminService::getSuperName();
  132. $this->authorizes = SystemAuth::items();
  133. }
  134. }
  135. /**
  136. * 修改用户状态
  137. * @auth true
  138. */
  139. public function state()
  140. {
  141. $this->_checkInput();
  142. SystemUser::mSave($this->_vali([
  143. 'status.in:0,1' => '状态值范围异常!',
  144. 'status.require' => '状态值不能为空!',
  145. ]));
  146. }
  147. /**
  148. * 删除系统用户
  149. * @auth true
  150. */
  151. public function remove()
  152. {
  153. $this->_checkInput();
  154. SystemUser::mDelete();
  155. }
  156. /**
  157. * 检查输入变量
  158. */
  159. private function _checkInput()
  160. {
  161. if (in_array('10000', str2arr(input('id', '')))) {
  162. $this->error('系统超级账号禁止删除!');
  163. }
  164. }
  165. }