Index.php 5.2 KB

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