User.php 8.1 KB

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