Index.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. 'general' => ['new', 'purple'],
  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', 'index/index');
  63. if ($this->auth->isLogin()) {
  64. $this->success(__("You've logged in, do not login again"), $url);
  65. }
  66. if ($this->request->isPost()) {
  67. $username = $this->request->post('username');
  68. $password = $this->request->post('password');
  69. $keeplogin = $this->request->post('keeplogin');
  70. $token = $this->request->post('__token__');
  71. $rule = [
  72. 'username' => 'require|length:3,30',
  73. 'password' => 'require|length:3,30',
  74. '__token__' => 'require|token',
  75. ];
  76. $data = [
  77. 'username' => $username,
  78. 'password' => $password,
  79. '__token__' => $token,
  80. ];
  81. if (Config::get('fastadmin.login_captcha')) {
  82. $rule['captcha'] = 'require|captcha';
  83. $data['captcha'] = $this->request->post('captcha');
  84. }
  85. $validate = new Validate($rule, [], ['username' => __('Username'), 'password' => __('Password'), 'captcha' => __('Captcha')]);
  86. $result = $validate->check($data);
  87. if (!$result) {
  88. $this->error($validate->getError(), $url, ['token' => $this->request->token()]);
  89. }
  90. AdminLog::setTitle(__('Login'));
  91. $result = $this->auth->login($username, $password, $keeplogin ? 86400 : 0);
  92. if ($result === true) {
  93. Hook::listen("admin_login_after", $this->request);
  94. $this->success(__('Login successful'), $url, ['url' => $url, 'id' => $this->auth->id, 'username' => $username, 'avatar' => $this->auth->avatar]);
  95. } else {
  96. $msg = $this->auth->getError();
  97. $msg = $msg ? $msg : __('Username or password is incorrect');
  98. $this->error($msg, $url, ['token' => $this->request->token()]);
  99. }
  100. }
  101. // 根据客户端的cookie,判断是否可以自动登录
  102. if ($this->auth->autologin()) {
  103. Session::delete("referer");
  104. $this->redirect($url);
  105. }
  106. $background = Config::get('fastadmin.login_background');
  107. $background = $background ? (stripos($background, 'http') === 0 ? $background : config('site.cdnurl') . $background) : '';
  108. $this->view->assign('background', $background);
  109. $this->view->assign('title', __('Login'));
  110. Hook::listen("admin_login_init", $this->request);
  111. return $this->view->fetch();
  112. }
  113. /**
  114. * 退出登录
  115. */
  116. public function logout()
  117. {
  118. if ($this->request->isPost()) {
  119. $this->auth->logout();
  120. Hook::listen("admin_logout_after", $this->request);
  121. $this->success(__('Logout successful'), 'index/login');
  122. }
  123. $html = "<form id='logout_submit' name='logout_submit' action='' method='post'>" . token() . "<input type='submit' value='ok' style='display:none;'></form>";
  124. $html .= "<script>document.forms['logout_submit'].submit();</script>";
  125. return $html;
  126. }
  127. }