User.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. use think\admin\service\AdminService;
  18. /**
  19. * 系统用户管理
  20. * Class User
  21. * @package app\admin\controller
  22. */
  23. class User extends Controller
  24. {
  25. /**
  26. * 绑定数据表
  27. * @var string
  28. */
  29. private $table = 'SystemUser';
  30. /**
  31. * 超级用户名称
  32. * @var string
  33. */
  34. protected $superName;
  35. /**
  36. * 控制器初始化
  37. */
  38. protected function initialize()
  39. {
  40. // 超级用户名称
  41. $this->superName = AdminService::instance()->getSuperName();
  42. }
  43. /**
  44. * 系统用户管理
  45. * @auth true
  46. * @menu true
  47. * @throws \think\db\exception\DataNotFoundException
  48. * @throws \think\db\exception\DbException
  49. * @throws \think\db\exception\ModelNotFoundException
  50. */
  51. public function index()
  52. {
  53. $this->title = '系统用户管理';
  54. $query = $this->_query($this->table);
  55. $query->equal('status')->dateBetween('login_at,create_at');
  56. $query->like('username,contact_phone#phone,contact_mail#mail');
  57. // 加载对应数据列表
  58. $this->type = input('type', 'all');
  59. if ($this->type === 'all') {
  60. $query->where(['is_deleted' => 0, 'status' => 1]);
  61. } elseif ($this->type = 'recycle') {
  62. $query->where(['is_deleted' => 0, 'status' => 0]);
  63. }
  64. // 列表排序并显示
  65. $query->order('sort desc,id desc')->page();
  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'])], 'id')) {
  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. if (isset($data['id']) && $data['id'] > 0) {
  130. unset($data['username']);
  131. } else {
  132. // 检查登录账号是否出现重复
  133. if (empty($data['username'])) $this->error('登录账号不能为空!');
  134. $where = ['username' => $data['username'], 'is_deleted' => 0];
  135. if ($this->app->db->name($this->table)->where($where)->count() > 0) {
  136. $this->error("账号已经存在,请使用其它账号!");
  137. }
  138. // 新添加的用户密码与账号相同
  139. $data['password'] = md5($data['username']);
  140. }
  141. // 账号权限绑定处理
  142. $data['authorize'] = arr2str($data['authorize'] ?? []);
  143. } else {
  144. $data['authorize'] = str2arr($data['authorize'] ?? '');
  145. $query = $this->app->db->name('SystemAuth')->where(['status' => 1]);
  146. $this->authorizes = $query->order('sort desc,id desc')->select()->toArray();
  147. }
  148. }
  149. /**
  150. * 修改用户状态
  151. * @auth true
  152. * @throws \think\db\exception\DbException
  153. */
  154. public function state()
  155. {
  156. $this->_checkInput();
  157. $this->_applyFormToken();
  158. $this->_save($this->table, $this->_vali([
  159. 'status.in:0,1' => '状态值范围异常!',
  160. 'status.require' => '状态值不能为空!',
  161. ]));
  162. }
  163. /**
  164. * 删除系统用户
  165. * @auth true
  166. * @throws \think\db\exception\DbException
  167. */
  168. public function remove()
  169. {
  170. $this->_checkInput();
  171. $this->_applyFormToken();
  172. $this->_delete($this->table);
  173. }
  174. /**
  175. * 检查输入变量
  176. */
  177. private function _checkInput()
  178. {
  179. if (in_array('10000', str2arr(input('id', '')))) {
  180. $this->error('系统超级账号禁止删除!');
  181. }
  182. }
  183. /**
  184. * 表单结果处理
  185. * @param bool $result
  186. */
  187. protected function _add_form_result(bool $result)
  188. {
  189. if ($result) {
  190. $id = $this->app->db->name($this->table)->getLastInsID();
  191. sysoplog('系统用户管理', "添加系统用户[{$id}]成功");
  192. }
  193. }
  194. /**
  195. * 表单结果处理
  196. * @param boolean $result
  197. */
  198. protected function _edit_form_result(bool $result)
  199. {
  200. if ($result) {
  201. $id = input('id') ?: 0;
  202. sysoplog('系统用户管理', "修改系统用户[{$id}]成功");
  203. $this->success('用户资料修改成功!', 'javascript:location.reload()');
  204. }
  205. }
  206. /**
  207. * 状态结果处理
  208. * @param boolean $result
  209. */
  210. protected function _state_save_result(bool $result)
  211. {
  212. if ($result) {
  213. [$id, $state] = [input('id'), input('status')];
  214. sysoplog('系统用户管理', ($state ? '激活' : '禁用') . "系统用户[{$id}]成功");
  215. }
  216. }
  217. /**
  218. * 删除结果处理
  219. * @param boolean $result
  220. */
  221. protected function _remove_delete_result(bool $result)
  222. {
  223. if ($result) {
  224. $id = input('id') ?: 0;
  225. sysoplog('系统用户管理', "删除系统用户[{$id}]成功");
  226. }
  227. }
  228. }