Index.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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\model\SystemUser;
  18. use think\admin\service\AdminService;
  19. use think\admin\service\MenuService;
  20. /**
  21. * 后台界面入口
  22. * Class Index
  23. * @package app\admin\controller
  24. */
  25. class Index extends Controller
  26. {
  27. /**
  28. * 显示后台首页
  29. * @throws \ReflectionException
  30. * @throws \think\db\exception\DataNotFoundException
  31. * @throws \think\db\exception\DbException
  32. * @throws \think\db\exception\ModelNotFoundException
  33. */
  34. public function index()
  35. {
  36. /*! 根据运行模式刷新权限 */
  37. AdminService::apply($this->app->isDebug());
  38. /*! 读取当前用户权限菜单树 */
  39. $this->menus = MenuService::getTree();
  40. /*! 判断当前用户的登录状态 */
  41. $this->login = AdminService::isLogin();
  42. /*! 菜单为空且未登录跳转到登录页 */
  43. if (empty($this->menus) && empty($this->login)) {
  44. $this->redirect(sysuri('admin/login/index'));
  45. } else {
  46. $this->title = '系统管理后台';
  47. $this->super = AdminService::isSuper();
  48. $this->theme = AdminService::getUserTheme();
  49. $this->fetch();
  50. }
  51. }
  52. /**
  53. * 后台主题切换
  54. * @login true
  55. * @return void
  56. * @throws \think\db\exception\DataNotFoundException
  57. * @throws \think\db\exception\DbException
  58. * @throws \think\db\exception\ModelNotFoundException
  59. */
  60. public function theme()
  61. {
  62. if ($this->request->isGet()) {
  63. $this->theme = AdminService::getUserTheme();
  64. $this->themes = Config::themes;
  65. $this->fetch();
  66. } else {
  67. $data = $this->_vali(['site_theme.require' => '主题名称不能为空!']);
  68. if (AdminService::setUserTheme($data['site_theme'])) {
  69. $this->success('主题配置保存成功!');
  70. } else {
  71. $this->error('主题配置保存失败!');
  72. }
  73. }
  74. }
  75. /**
  76. * 修改用户资料
  77. * @login true
  78. * @param mixed $id 用户ID
  79. */
  80. public function info($id = 0)
  81. {
  82. $this->_applyFormToken();
  83. if (AdminService::getUserId() === intval($id)) {
  84. SystemUser::mForm('admin@user/form', 'id', [], ['id' => $id]);
  85. } else {
  86. $this->error('只能修改自己的资料!');
  87. }
  88. }
  89. /**
  90. * 资料修改表单处理
  91. * @param array $data
  92. */
  93. protected function _info_form_filter(array &$data)
  94. {
  95. if ($this->request->isPost()) {
  96. unset($data['username'], $data['authorize']);
  97. }
  98. }
  99. /**
  100. * 资料修改结果处理
  101. * @param bool $status
  102. */
  103. protected function _info_form_result(bool $status)
  104. {
  105. if ($status) {
  106. $this->success('用户资料修改成功!', 'javascript:location.reload()');
  107. }
  108. }
  109. /**
  110. * 修改当前用户密码
  111. * @login true
  112. * @param mixed $id
  113. * @throws \think\db\exception\DataNotFoundException
  114. * @throws \think\db\exception\DbException
  115. * @throws \think\db\exception\ModelNotFoundException
  116. */
  117. public function pass($id = 0)
  118. {
  119. $this->_applyFormToken();
  120. if (AdminService::getUserId() !== intval($id)) {
  121. $this->error('只能修改当前用户的密码!');
  122. }
  123. if ($this->app->request->isGet()) {
  124. $this->verify = true;
  125. SystemUser::mForm('admin@user/pass', 'id', [], ['id' => $id]);
  126. } else {
  127. $data = $this->_vali([
  128. 'password.require' => '登录密码不能为空!',
  129. 'repassword.require' => '重复密码不能为空!',
  130. 'oldpassword.require' => '旧的密码不能为空!',
  131. 'password.confirm:repassword' => '两次输入的密码不一致!',
  132. ]);
  133. $user = SystemUser::mk()->find($id);
  134. if (empty($user)) $this->error('用户不存在!');
  135. if (md5($data['oldpassword']) !== $user['password']) {
  136. $this->error('旧密码验证失败,请重新输入!');
  137. }
  138. if ($user->save(['password' => md5($data['password'])])) {
  139. sysoplog('系统用户管理', "修改用户[{$user['id']}]密码成功");
  140. $this->success('密码修改成功,下次请使用新密码登录!', '');
  141. } else {
  142. $this->error('密码修改失败,请稍候再试!');
  143. }
  144. }
  145. }
  146. }