User.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. * @throws \think\db\exception\DataNotFoundException
  77. * @throws \think\db\exception\DbException
  78. * @throws \think\db\exception\ModelNotFoundException
  79. */
  80. public function pass()
  81. {
  82. $this->_applyFormToken();
  83. if ($this->request->isGet()) {
  84. $this->verify = false;
  85. SystemUser::mForm('pass');
  86. } else {
  87. $data = $this->_vali([
  88. 'id.require' => '用户ID不能为空!',
  89. 'password.require' => '登录密码不能为空!',
  90. 'repassword.require' => '重复密码不能为空!',
  91. 'repassword.confirm:password' => '两次输入的密码不一致!',
  92. ]);
  93. $user = SystemUser::mk()->find($data['id']);
  94. if (!empty($user) && $user->save(['password' => md5($data['password'])])) {
  95. sysoplog('系统用户管理', "修改用户[{$data['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(array &$data)
  110. {
  111. if ($this->request->isPost()) {
  112. // 账号权限绑定处理
  113. $data['authorize'] = arr2str($data['authorize'] ?? []);
  114. if (isset($data['id']) && $data['id'] > 0) {
  115. unset($data['username']);
  116. } else {
  117. // 检查账号是否重复
  118. if (empty($data['username'])) {
  119. $this->error('登录账号不能为空!');
  120. }
  121. $map = ['username' => $data['username'], 'is_deleted' => 0];
  122. if (SystemUser::mk()->where($map)->count() > 0) {
  123. $this->error("账号已经存在,请使用其它账号!");
  124. }
  125. // 新添加的用户密码与账号相同
  126. $data['password'] = md5($data['username']);
  127. }
  128. } else {
  129. // 权限绑定处理
  130. $data['authorize'] = str2arr($data['authorize'] ?? '');
  131. // 用户身份数据
  132. $this->bases = SystemBase::items('身份权限');
  133. // 用户权限管理
  134. $this->superName = AdminService::instance()->getSuperName();
  135. $this->authorizes = SystemAuth::items();
  136. }
  137. }
  138. /**
  139. * 修改用户状态
  140. * @auth true
  141. */
  142. public function state()
  143. {
  144. $this->_checkInput();
  145. SystemUser::mSave($this->_vali([
  146. 'status.in:0,1' => '状态值范围异常!',
  147. 'status.require' => '状态值不能为空!',
  148. ]));
  149. }
  150. /**
  151. * 删除系统用户
  152. * @auth true
  153. */
  154. public function remove()
  155. {
  156. $this->_checkInput();
  157. SystemUser::mDelete();
  158. }
  159. /**
  160. * 检查输入变量
  161. */
  162. private function _checkInput()
  163. {
  164. if (in_array('10000', str2arr(input('id', '')))) {
  165. $this->error('系统超级账号禁止删除!');
  166. }
  167. }
  168. }