Index.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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\Session;
  8. use think\Validate;
  9. /**
  10. * 后台首页
  11. * @internal
  12. */
  13. class Index extends Backend
  14. {
  15. protected $noNeedLogin = ['login'];
  16. protected $noNeedRight = ['index', 'logout'];
  17. protected $layout = '';
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. //移除HTML标签
  22. $this->request->filter('trim,strip_tags,htmlspecialchars');
  23. }
  24. /**
  25. * 后台首页
  26. */
  27. public function index()
  28. {
  29. $cookieArr = ['adminskin' => "/^skin\-([a-z\-]+)\$/i", 'multiplenav' => "/^(0|1)\$/", 'multipletab' => "/^(0|1)\$/", 'show_submenu' => "/^(0|1)\$/"];
  30. foreach ($cookieArr as $key => $regex) {
  31. $cookieValue = $this->request->cookie($key);
  32. if (!is_null($cookieValue) && preg_match($regex, $cookieValue)) {
  33. config('fastadmin.' . $key, $cookieValue);
  34. }
  35. }
  36. //左侧菜单
  37. list($menulist, $navlist, $fixedmenu, $referermenu) = $this->auth->getSidebar([
  38. 'dashboard' => 'hot',
  39. 'addon' => ['new', 'red', 'badge'],
  40. 'auth/rule' => __('Menu'),
  41. ], $this->view->site['fixedpage']);
  42. $action = $this->request->request('action');
  43. if ($this->request->isPost()) {
  44. if ($action == 'refreshmenu') {
  45. $this->success('', null, ['menulist' => $menulist, 'navlist' => $navlist]);
  46. }
  47. }
  48. $this->assignconfig('cookie', ['prefix' => config('cookie.prefix')]);
  49. $this->view->assign('menulist', $menulist);
  50. $this->view->assign('navlist', $navlist);
  51. $this->view->assign('fixedmenu', $fixedmenu);
  52. $this->view->assign('referermenu', $referermenu);
  53. $this->view->assign('title', __('Home'));
  54. return $this->view->fetch();
  55. }
  56. /**
  57. * 管理员登录
  58. */
  59. public function login()
  60. {
  61. $url = $this->request->get('url', '', 'url_clean');
  62. $url = $url ?: 'index/index';
  63. if ($this->auth->isLogin()) {
  64. $this->success(__("You've logged in, do not login again"), $url);
  65. }
  66. //保持会话有效时长,单位:小时
  67. $keeyloginhours = 24;
  68. if ($this->request->isPost()) {
  69. $username = $this->request->post('username');
  70. $password = $this->request->post('password', '', null);
  71. $keeplogin = $this->request->post('keeplogin');
  72. $token = $this->request->post('__token__');
  73. $rule = [
  74. 'username' => 'require|length:3,30',
  75. // 'password' => 'require|length:3,30',
  76. '__token__' => 'require|token',
  77. ];
  78. $data = [
  79. 'username' => $username,
  80. 'password' => $password,
  81. '__token__' => $token,
  82. ];
  83. if (Config::get('fastadmin.login_captcha')) {
  84. $rule['captcha'] = 'require|captcha';
  85. $data['captcha'] = $this->request->post('captcha');
  86. }
  87. $validate = new Validate($rule, [], ['username' => __('Username'), 'password' => __('Password'), 'captcha' => __('Captcha')]);
  88. $result = $validate->check($data);
  89. if (!$result) {
  90. $this->error($validate->getError(), $url, ['token' => $this->request->token()]);
  91. }
  92. AdminLog::setTitle(__('Login'));
  93. $result = $this->auth->login($username, $password, $keeplogin ? $keeyloginhours * 3600 : 0);
  94. if ($result === true) {
  95. Hook::listen("admin_login_after", $this->request);
  96. $this->success(__('Login successful'), $url, ['url' => $url, 'id' => $this->auth->id, 'username' => $username, 'avatar' => $this->auth->avatar]);
  97. } else {
  98. $msg = $this->auth->getError();
  99. $msg = $msg ? $msg : __('Username or password is incorrect');
  100. $this->error($msg, $url, ['token' => $this->request->token()]);
  101. }
  102. }
  103. // 根据客户端的cookie,判断是否可以自动登录
  104. if ($this->auth->autologin()) {
  105. Session::delete("referer");
  106. $this->redirect($url);
  107. }
  108. $background = Config::get('fastadmin.login_background');
  109. $background = $background ? (stripos($background, 'http') === 0 ? $background : config('site.cdnurl') . $background) : '';
  110. $this->view->assign('keeyloginhours', $keeyloginhours);
  111. $this->view->assign('background', $background);
  112. $this->view->assign('title', __('Login'));
  113. Hook::listen("admin_login_init", $this->request);
  114. return $this->view->fetch();
  115. }
  116. /**
  117. * 退出登录
  118. */
  119. public function logout()
  120. {
  121. if ($this->request->isPost()) {
  122. $this->auth->logout();
  123. Hook::listen("admin_logout_after", $this->request);
  124. $this->success(__('Logout successful'), 'index/login');
  125. }
  126. $html = "<form id='logout_submit' name='logout_submit' action='' method='post'>" . token() . "<input type='submit' value='ok' style='display:none;'></form>";
  127. $html .= "<script>document.forms['logout_submit'].submit();</script>";
  128. return $html;
  129. }
  130. }