Index.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace app\admin\controller;
  3. use think\admin\Controller;
  4. use think\admin\service\AdminService;
  5. use think\admin\service\MenuService;
  6. /**
  7. * 后台界面入口
  8. * Class Index
  9. * @package app\admin\controller
  10. */
  11. class Index extends Controller
  12. {
  13. /**
  14. * 显示后台首页
  15. * @throws \ReflectionException
  16. * @throws \think\db\exception\DataNotFoundException
  17. * @throws \think\db\exception\DbException
  18. * @throws \think\db\exception\ModelNotFoundException
  19. */
  20. public function index()
  21. {
  22. /*! 根据运行模式刷新权限 */
  23. AdminService::instance()->apply($this->app->isDebug());
  24. /*! 读取当前用户权限菜单树 */
  25. $this->menus = MenuService::instance()->getTree();
  26. /*! 判断当前用户的登录状态 */
  27. $this->login = AdminService::instance()->isLogin();
  28. /*! 菜单为空且未登录跳转到登录页 */
  29. if (empty($this->menus) && empty($this->login)) {
  30. $this->redirect(sysuri('admin/login/index'));
  31. } else {
  32. $this->title = '系统管理后台';
  33. $this->isSuper = AdminService::instance()->isSuper();
  34. $this->fetch();
  35. }
  36. }
  37. /**
  38. * 修改用户资料
  39. * @login true
  40. * @param mixed $id 用户ID
  41. * @throws \think\db\exception\DataNotFoundException
  42. * @throws \think\db\exception\DbException
  43. * @throws \think\db\exception\ModelNotFoundException
  44. */
  45. public function info($id = 0)
  46. {
  47. $this->_applyFormToken();
  48. if (AdminService::instance()->getUserId() === intval($id)) {
  49. $this->_form('SystemUser', 'admin@user/form', 'id', [], ['id' => $id]);
  50. } else {
  51. $this->error('只能修改自己的资料!');
  52. }
  53. }
  54. /**
  55. * 资料修改后处理
  56. * @param bool $status
  57. */
  58. protected function _info_form_result(bool $status)
  59. {
  60. if ($status) {
  61. $this->success('用户资料修改成功!', 'javascript:location.reload()');
  62. }
  63. }
  64. /**
  65. * 修改当前用户密码
  66. * @login true
  67. * @param mixed $id
  68. * @throws \think\db\exception\DataNotFoundException
  69. * @throws \think\db\exception\DbException
  70. * @throws \think\db\exception\ModelNotFoundException
  71. */
  72. public function pass($id = 0)
  73. {
  74. $this->_applyFormToken();
  75. if (AdminService::instance()->getUserId() !== intval($id)) {
  76. $this->error('只能修改当前用户的密码!');
  77. }
  78. if ($this->app->request->isGet()) {
  79. $this->verify = true;
  80. $this->_form('SystemUser', 'admin@user/pass', 'id', [], ['id' => $id]);
  81. } else {
  82. $data = $this->_vali([
  83. 'password.require' => '登录密码不能为空!',
  84. 'repassword.require' => '重复密码不能为空!',
  85. 'oldpassword.require' => '旧的密码不能为空!',
  86. 'password.confirm:repassword' => '两次输入的密码不一致!',
  87. ]);
  88. $user = $this->app->db->name('SystemUser')->where(['id' => $id])->find();
  89. if (md5($data['oldpassword']) !== $user['password']) {
  90. $this->error('旧密码验证失败,请重新输入!');
  91. }
  92. if (data_save('SystemUser', ['id' => $user['id'], 'password' => md5($data['password'])])) {
  93. sysoplog('系统用户管理', "修改用户[{$user['id']}]密码成功");
  94. $this->success('密码修改成功,下次请使用新密码登录!', '');
  95. } else {
  96. $this->error('密码修改失败,请稍候再试!');
  97. }
  98. }
  99. }
  100. }