Index.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. //移除HTML标签
  21. $this->request->filter('trim,strip_tags,htmlspecialchars');
  22. }
  23. /**
  24. * 后台首页
  25. */
  26. public function index()
  27. {
  28. //左侧菜单
  29. list($menulist, $navlist, $fixedmenu, $referermenu) = $this->auth->getSidebar([
  30. 'dashboard' => 'hot',
  31. 'addon' => ['new', 'red', 'badge'],
  32. 'auth/rule' => __('Menu'),
  33. 'general' => ['new', 'purple'],
  34. ], $this->view->site['fixedpage']);
  35. $action = $this->request->request('action');
  36. if ($this->request->isPost()) {
  37. if ($action == 'refreshmenu') {
  38. $this->success('', null, ['menulist' => $menulist, 'navlist' => $navlist]);
  39. }
  40. }
  41. $this->view->assign('menulist', $menulist);
  42. $this->view->assign('navlist', $navlist);
  43. $this->view->assign('fixedmenu', $fixedmenu);
  44. $this->view->assign('referermenu', $referermenu);
  45. $this->view->assign('title', __('Home'));
  46. return $this->view->fetch();
  47. }
  48. /**
  49. * 管理员登录
  50. */
  51. public function login()
  52. {
  53. $url = $this->request->get('url', 'index/index');
  54. if ($this->auth->isLogin()) {
  55. $this->success(__("You've logged in, do not login again"), $url);
  56. }
  57. if ($this->request->isPost()) {
  58. $username = $this->request->post('username');
  59. $password = $this->request->post('password');
  60. $keeplogin = $this->request->post('keeplogin');
  61. $token = $this->request->post('__token__');
  62. $rule = [
  63. 'username' => 'require|length:3,30',
  64. 'password' => 'require|length:3,30',
  65. '__token__' => 'require|token',
  66. ];
  67. $data = [
  68. 'username' => $username,
  69. 'password' => $password,
  70. '__token__' => $token,
  71. ];
  72. if (Config::get('fastadmin.login_captcha')) {
  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. $this->error($validate->getError(), $url, ['token' => $this->request->token()]);
  80. }
  81. AdminLog::setTitle(__('Login'));
  82. $result = $this->auth->login($username, $password, $keeplogin ? 86400 : 0);
  83. if ($result === true) {
  84. Hook::listen("admin_login_after", $this->request);
  85. $this->success(__('Login successful'), $url, ['url' => $url, 'id' => $this->auth->id, 'username' => $username, 'avatar' => $this->auth->avatar]);
  86. } else {
  87. $msg = $this->auth->getError();
  88. $msg = $msg ? $msg : __('Username or password is incorrect');
  89. $this->error($msg, $url, ['token' => $this->request->token()]);
  90. }
  91. }
  92. // 根据客户端的cookie,判断是否可以自动登录
  93. if ($this->auth->autologin()) {
  94. $this->redirect($url);
  95. }
  96. $background = Config::get('fastadmin.login_background');
  97. $background = stripos($background, 'http') === 0 ? $background : config('site.cdnurl') . $background;
  98. $this->view->assign('background', $background);
  99. $this->view->assign('title', __('Login'));
  100. Hook::listen("admin_login_init", $this->request);
  101. return $this->view->fetch();
  102. }
  103. /**
  104. * 注销登录
  105. */
  106. public function logout()
  107. {
  108. $this->auth->logout();
  109. Hook::listen("admin_logout_after", $this->request);
  110. $this->success(__('Logout successful'), 'index/login');
  111. }
  112. }