Index.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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' => 'side',
  31. 'general' => ['new', 'purple'],
  32. ], $this->view->site['fixedpage']);
  33. $this->view->assign('menulist', $menulist);
  34. $this->view->assign('title', __('Home'));
  35. return $this->view->fetch();
  36. }
  37. /**
  38. * 管理员登录
  39. */
  40. public function login()
  41. {
  42. $url = $this->request->get('url', 'index/index');
  43. if ($this->auth->isLogin())
  44. {
  45. $this->success(__("You've logged in, do not login again"), $url);
  46. }
  47. if ($this->request->isPost())
  48. {
  49. $username = $this->request->post('username');
  50. $password = $this->request->post('password');
  51. $keeplogin = $this->request->post('keeplogin');
  52. $token = $this->request->post('__token__');
  53. $rule = [
  54. 'username' => 'require|length:3,30',
  55. 'password' => 'require|length:3,30',
  56. '__token__' => 'token',
  57. ];
  58. $data = [
  59. 'username' => $username,
  60. 'password' => $password,
  61. '__token__' => $token,
  62. ];
  63. if (Config::get('fastadmin.login_captcha'))
  64. {
  65. $rule['captcha'] = 'require|captcha';
  66. $data['captcha'] = $this->request->post('captcha');
  67. }
  68. $validate = new Validate($rule, [], ['username' => __('Username'), 'password' => __('Password'), 'captcha' => __('Captcha')]);
  69. $result = $validate->check($data);
  70. if (!$result)
  71. {
  72. $this->error($validate->getError(), $url, ['token' => $this->request->token()]);
  73. }
  74. AdminLog::setTitle(__('Login'));
  75. $result = $this->auth->login($username, $password, $keeplogin ? 86400 : 0);
  76. if ($result === true)
  77. {
  78. $this->success(__('Login successful'), $url, ['url' => $url, 'id' => $this->auth->id, 'username' => $username, 'avatar' => $this->auth->avatar]);
  79. }
  80. else
  81. {
  82. $this->error(__('Username or password is incorrect'), $url, ['token' => $this->request->token()]);
  83. }
  84. }
  85. // 根据客户端的cookie,判断是否可以自动登录
  86. if ($this->auth->autologin())
  87. {
  88. $this->redirect($url);
  89. }
  90. $background = cdnurl(Config::get('fastadmin.login_background'));
  91. $this->view->assign('background', $background);
  92. $this->view->assign('title', __('Login'));
  93. Hook::listen("login_init", $this->request);
  94. return $this->view->fetch();
  95. }
  96. /**
  97. * 注销登录
  98. */
  99. public function logout()
  100. {
  101. $this->auth->logout();
  102. $this->success(__('Logout successful'), 'index/login');
  103. }
  104. }