User.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\SystemBase;
  4. use app\admin\model\SystemUser;
  5. use think\admin\Controller;
  6. use think\admin\helper\QueryHelper;
  7. use think\admin\service\AdminService;
  8. /**
  9. * 系统用户管理
  10. * Class User
  11. * @package app\admin\controller
  12. */
  13. class User extends Controller
  14. {
  15. /**
  16. * 绑定数据表
  17. * @var string
  18. */
  19. private $table = 'SystemUser';
  20. /**
  21. * 超级用户名称
  22. * @var string
  23. */
  24. protected $superName;
  25. /**
  26. * 控制器初始化
  27. */
  28. protected function initialize()
  29. {
  30. // 超级用户名称
  31. $this->superName = AdminService::instance()->getSuperName();
  32. }
  33. /**
  34. * 系统用户管理
  35. * @auth true
  36. * @menu true
  37. * @throws \think\db\exception\DataNotFoundException
  38. * @throws \think\db\exception\DbException
  39. * @throws \think\db\exception\ModelNotFoundException
  40. */
  41. public function index()
  42. {
  43. $this->type = input('type', 'index');
  44. $this->_query(SystemUser::class)->layTable(function () {
  45. $this->title = '系统用户管理';
  46. $this->bases = (new SystemBase)->items('身份权限');
  47. }, function (QueryHelper $query) {
  48. // 加载对应数据列表
  49. if ($this->type === 'index') {
  50. $query->where(['is_deleted' => 0, 'status' => 1]);
  51. } elseif ($this->type = 'recycle') {
  52. $query->where(['is_deleted' => 0, 'status' => 0]);
  53. }
  54. // 数据列表搜索过滤
  55. $query->equal('status,usertype')->dateBetween('login_at,create_at');
  56. $query->like('username,nickname,contact_phone#phone,contact_mail#mail');
  57. });
  58. }
  59. /**
  60. * 数据列表处理
  61. * @param array $data
  62. */
  63. protected function _page_filter(array &$data)
  64. {
  65. (new SystemBase)->items('身份权限', $data, 'usertype', 'userinfo');
  66. }
  67. /**
  68. * 添加系统用户
  69. * @auth true
  70. * @throws \think\db\exception\DataNotFoundException
  71. * @throws \think\db\exception\DbException
  72. * @throws \think\db\exception\ModelNotFoundException
  73. */
  74. public function add()
  75. {
  76. $this->_applyFormToken();
  77. $this->_form($this->table, 'form');
  78. }
  79. /**
  80. * 编辑系统用户
  81. * @auth true
  82. * @throws \think\db\exception\DataNotFoundException
  83. * @throws \think\db\exception\DbException
  84. * @throws \think\db\exception\ModelNotFoundException
  85. */
  86. public function edit()
  87. {
  88. $this->_applyFormToken();
  89. $this->_form($this->table, 'form');
  90. }
  91. /**
  92. * 修改用户密码
  93. * @auth true
  94. * @throws \think\db\exception\DataNotFoundException
  95. * @throws \think\db\exception\DbException
  96. * @throws \think\db\exception\ModelNotFoundException
  97. */
  98. public function pass()
  99. {
  100. $this->_applyFormToken();
  101. if ($this->request->isGet()) {
  102. $this->verify = false;
  103. $this->_form($this->table, 'pass');
  104. } else {
  105. $data = $this->_vali([
  106. 'id.require' => '用户ID不能为空!',
  107. 'password.require' => '登录密码不能为空!',
  108. 'repassword.require' => '重复密码不能为空!',
  109. 'repassword.confirm:password' => '两次输入的密码不一致!',
  110. ]);
  111. if (data_save($this->table, ['id' => $data['id'], 'password' => md5($data['password'])])) {
  112. sysoplog('系统用户管理', "修改用户[{$data['id']}]密码成功");
  113. $this->success('密码修改成功,请使用新密码登录!', '');
  114. } else {
  115. $this->error('密码修改失败,请稍候再试!');
  116. }
  117. }
  118. }
  119. /**
  120. * 表单数据处理
  121. * @param array $data
  122. * @throws \think\db\exception\DataNotFoundException
  123. * @throws \think\db\exception\DbException
  124. * @throws \think\db\exception\ModelNotFoundException
  125. */
  126. protected function _form_filter(array &$data)
  127. {
  128. if ($this->request->isPost()) {
  129. // 账号权限绑定处理
  130. $data['authorize'] = arr2str($data['authorize'] ?? []);
  131. if (isset($data['id']) && $data['id'] > 0) {
  132. unset($data['username']);
  133. } else {
  134. // 检查登录账号是否出现重复
  135. if (empty($data['username'])) $this->error('登录账号不能为空!');
  136. $where = ['username' => $data['username'], 'is_deleted' => 0];
  137. if ($this->app->db->name($this->table)->where($where)->count() > 0) {
  138. $this->error("账号已经存在,请使用其它账号!");
  139. }
  140. // 新添加的用户密码与账号相同
  141. $data['password'] = md5($data['username']);
  142. }
  143. } else {
  144. // 用户身份数据
  145. $this->bases = (new SystemBase)->items('身份权限');
  146. // 权限绑定处理
  147. $data['authorize'] = str2arr($data['authorize'] ?? '');
  148. // 用户权限管理
  149. $query = $this->app->db->name('SystemAuth')->where(['status' => 1]);
  150. $this->authorizes = $query->order('sort desc,id desc')->select()->toArray();
  151. }
  152. }
  153. /**
  154. * 修改用户状态
  155. * @auth true
  156. * @throws \think\db\exception\DbException
  157. */
  158. public function state()
  159. {
  160. $this->_checkInput();
  161. $this->_save($this->table, $this->_vali([
  162. 'status.in:0,1' => '状态值范围异常!',
  163. 'status.require' => '状态值不能为空!',
  164. ]));
  165. }
  166. /**
  167. * 删除系统用户
  168. * @auth true
  169. * @throws \think\db\exception\DbException
  170. */
  171. public function remove()
  172. {
  173. $this->_checkInput();
  174. $this->_delete($this->table);
  175. }
  176. /**
  177. * 检查输入变量
  178. */
  179. private function _checkInput()
  180. {
  181. if (in_array('10000', str2arr(input('id', '')))) {
  182. $this->error('系统超级账号禁止删除!');
  183. }
  184. }
  185. /**
  186. * 表单结果处理
  187. * @param bool $result
  188. */
  189. protected function _add_form_result(bool $result)
  190. {
  191. if ($result) {
  192. $id = $this->app->db->name($this->table)->getLastInsID();
  193. sysoplog('系统用户管理', "添加系统用户[{$id}]成功");
  194. }
  195. }
  196. /**
  197. * 表单结果处理
  198. * @param boolean $result
  199. */
  200. protected function _edit_form_result(bool $result)
  201. {
  202. if ($result) {
  203. $id = input('id') ?: 0;
  204. sysoplog('系统用户管理', "修改系统用户[{$id}]成功");
  205. if ($id == AdminService::instance()->getUserId()) {
  206. $this->success('用户资料修改成功!', 'javascript:location.reload()');
  207. } else {
  208. $this->success('用户资料修改成功!');
  209. }
  210. }
  211. }
  212. /**
  213. * 状态结果处理
  214. * @param boolean $result
  215. */
  216. protected function _state_save_result(bool $result)
  217. {
  218. if ($result) {
  219. [$id, $state] = [input('id'), input('status')];
  220. sysoplog('系统用户管理', ($state ? '激活' : '禁用') . "系统用户[{$id}]成功");
  221. }
  222. }
  223. /**
  224. * 删除结果处理
  225. * @param boolean $result
  226. */
  227. protected function _remove_delete_result(bool $result)
  228. {
  229. if ($result) {
  230. $id = input('id') ?: 0;
  231. sysoplog('系统用户管理', "删除系统用户[{$id}]成功");
  232. }
  233. }
  234. }