Index.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\AdminLog;
  4. use app\common\controller\Backend;
  5. use think\Config;
  6. use think\Hook;
  7. use think\Validate;
  8. /**
  9. * 后台首页
  10. * @internal
  11. */
  12. class Index extends Backend
  13. {
  14. protected $noNeedLogin = ['login'];
  15. protected $noNeedRight = ['index', 'logout'];
  16. protected $layout = '';
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. }
  21. /**
  22. * 后台首页
  23. */
  24. public function index()
  25. {
  26. //左侧菜单
  27. $menulist = $this->auth->getSidebar([
  28. // 'dashboard' => 'hot',
  29. // 'addon' => ['new', 'red', 'badge'],
  30. // 'auth/rule' => __('Menu'),
  31. // 'general' => ['new', 'purple'],
  32. ], $this->view->site['fixedpage']);
  33. $action = $this->request->request('action');
  34. if ($this->request->isPost())
  35. {
  36. if ($action == 'refreshmenu')
  37. {
  38. $this->success('', null, ['menulist' => $menulist]);
  39. }
  40. }
  41. $this->view->assign('menulist', $menulist);
  42. $this->view->assign('title', __('Home'));
  43. return $this->view->fetch();
  44. }
  45. /**
  46. * 管理员登录
  47. */
  48. public function login()
  49. {
  50. $url = $this->request->get('url', 'index/index');
  51. if ($this->auth->isLogin())
  52. {
  53. $this->success(__("You've logged in, do not login again"), $url);
  54. }
  55. if ($this->request->isPost())
  56. {
  57. $username = $this->request->post('username');
  58. $password = $this->request->post('password');
  59. $keeplogin = $this->request->post('keeplogin');
  60. $token = $this->request->post('__token__');
  61. $rule = [
  62. 'username' => 'require|length:3,30',
  63. 'password' => 'require|length:3,30',
  64. '__token__' => 'token',
  65. ];
  66. $data = [
  67. 'username' => $username,
  68. 'password' => $password,
  69. '__token__' => $token,
  70. ];
  71. if (Config::get('fastadmin.login_captcha'))
  72. {
  73. $rule['captcha'] = 'require|captcha';
  74. $data['captcha'] = $this->request->post('captcha');
  75. }
  76. $validate = new Validate($rule, [], ['username' => __('Username'), 'password' => __('Password'), 'captcha' => __('Captcha')]);
  77. $result = $validate->check($data);
  78. if (!$result)
  79. {
  80. $this->error($validate->getError(), $url, ['token' => $this->request->token()]);
  81. }
  82. AdminLog::setTitle(__('Login'));
  83. $result = $this->auth->login($username, $password, $keeplogin ? 86400 : 0);
  84. if ($result === true)
  85. {
  86. Hook::listen("admin_login_after", $this->request);
  87. $this->success(__('Login successful'), $url, ['url' => $url, 'id' => $this->auth->id, 'username' => $username, 'avatar' => $this->auth->avatar]);
  88. }
  89. else
  90. {
  91. $msg = $this->auth->getError();
  92. $msg = $msg ? $msg : __('Username or password is incorrect');
  93. $this->error($msg, $url, ['token' => $this->request->token()]);
  94. }
  95. }
  96. // 根据客户端的cookie,判断是否可以自动登录
  97. if ($this->auth->autologin())
  98. {
  99. $this->redirect($url);
  100. }
  101. $background = Config::get('fastadmin.login_background');
  102. $background = stripos($background, 'http')===0 ? $background : config('site.cdnurl') . $background;
  103. $this->view->assign('background', $background);
  104. $this->view->assign('title', __('Login'));
  105. Hook::listen("admin_login_init", $this->request);
  106. return $this->view->fetch();
  107. }
  108. /**
  109. * 注销登录
  110. */
  111. public function logout()
  112. {
  113. $this->auth->logout();
  114. Hook::listen("admin_logout_after", $this->request);
  115. $this->success(__('Logout successful'), 'index/login');
  116. }
  117. }