User.php 7.6 KB

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