Index.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. AdminLog::setTitle(__('Login'));
  77. $result = $this->auth->login($username, $password, $keeplogin ? 86400 : 0);
  78. if ($result === true) {
  79. Hook::listen("admin_login_after", $this->request);
  80. $this->success(__('Login successful'), $url, ['url' => $url, 'id' => $this->auth->id, 'username' => $username, 'avatar' => $this->auth->avatar]);
  81. } else {
  82. $msg = $this->auth->getError();
  83. $msg = $msg ? $msg : __('Username or password is incorrect');
  84. $this->error($msg, $url, ['token' => $this->request->token()]);
  85. }
  86. }
  87. // 根据客户端的cookie,判断是否可以自动登录
  88. if ($this->auth->autologin()) {
  89. Session::delete("referer");
  90. $this->redirect($url);
  91. }
  92. $background = Config::get('fastadmin.login_background');
  93. $background = $background ? (stripos($background, 'http') === 0 ? $background : config('site.cdnurl') . $background) : '';
  94. $this->view->assign('background', $background);
  95. $this->view->assign('title', __('Login'));
  96. Hook::listen("admin_login_init", $this->request);
  97. return $this->view->fetch();
  98. }
  99. /**
  100. * 退出登录
  101. */
  102. public function logout()
  103. {
  104. if ($this->request->isPost()) {
  105. $this->auth->logout();
  106. Hook::listen("admin_logout_after", $this->request);
  107. $this->success(__('Logout successful'), 'index/login');
  108. }
  109. $html = "<form id='logout_submit' name='logout_submit' action='' method='post'>" . token() . "<input type='submit' value='ok' style='display:none;'></form>";
  110. $html .= "<script>document.forms['logout_submit'].submit();</script>";
  111. return $html;
  112. }
  113. }