Index.php 5.3 KB

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