Index.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\library\SystemLogin;
  4. use app\admin\model\AdminLog;
  5. use app\common\controller\Backend;
  6. use think\Config;
  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', 'cockpitlogin'];
  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. // 皮肤由系统风格强制,忽略 cookie 覆盖
  35. if ($key === 'adminskin') {
  36. continue;
  37. }
  38. config('fastadmin.' . $key, $cookieValue);
  39. }
  40. }
  41. // 按登录系统再次套用风格(覆盖 cookie)
  42. $theme = SystemLogin::applyTheme($this->auth);
  43. $this->view->assign($theme);
  44. //左侧菜单(不显示 hot/new 等角标)
  45. list($menulist, $navlist, $fixedmenu, $referermenu) = $this->auth->getSidebar([
  46. ], $this->view->site['fixedpage']);
  47. $action = $this->request->request('action');
  48. if ($this->request->isPost()) {
  49. if ($action == 'refreshmenu') {
  50. $this->success('', null, ['menulist' => $menulist, 'navlist' => $navlist]);
  51. }
  52. }
  53. $this->assignconfig('cookie', ['prefix' => config('cookie.prefix')]);
  54. $this->view->assign('menulist', $menulist);
  55. $this->view->assign('navlist', $navlist);
  56. $this->view->assign('fixedmenu', $fixedmenu);
  57. $this->view->assign('referermenu', $referermenu);
  58. $this->view->assign('title', __('Home'));
  59. return $this->view->fetch();
  60. }
  61. /**
  62. * 登录页 login.html:按 mproc.login_page_system 锁定可登录账号与风格
  63. */
  64. public function login()
  65. {
  66. SystemLogin::loadConfig();
  67. $systemKey = SystemLogin::normalizeKey((string)Config::get('mproc.login_page_system'));
  68. return $this->doSystemLogin($systemKey);
  69. }
  70. /**
  71. * 兼容旧链接
  72. */
  73. public function cockpitlogin()
  74. {
  75. $this->redirect('index/login', $this->request->get(), 302);
  76. }
  77. /**
  78. * @param string $systemKey collab|cockpit
  79. */
  80. protected function doSystemLogin(string $systemKey)
  81. {
  82. SystemLogin::loadConfig();
  83. $systemKey = SystemLogin::normalizeKey($systemKey);
  84. $url = $this->request->get('url', '', 'url_clean');
  85. $url = $url ?: 'index/index';
  86. $sysName = (string)SystemLogin::option($systemKey, 'name', '');
  87. if ($sysName === '') {
  88. $sysName = $systemKey === 'cockpit' ? '生产经营驾驶舱系统' : '生产协同系统';
  89. }
  90. if ($this->auth->isLogin()) {
  91. if (!SystemLogin::belongsTo($this->auth, (int)$this->auth->id, $systemKey)) {
  92. $this->auth->logout();
  93. Session::delete('login_system');
  94. } else {
  95. Session::set('login_system', $systemKey);
  96. $this->success(__("You've logged in, do not login again"), $url);
  97. }
  98. }
  99. //保持会话有效时长,单位:小时(与 config fastadmin.keeplogin_hours 一致)
  100. $keeyloginhours = (int)Config::get('fastadmin.keeplogin_hours');
  101. if ($keeyloginhours <= 0) {
  102. $keeyloginhours = 72;
  103. }
  104. if ($this->request->isPost()) {
  105. $username = $this->request->post('username');
  106. $password = $this->request->post('password', '', null);
  107. $token = $this->request->post('__token__');
  108. $rule = [
  109. 'username' => 'require|length:3,30',
  110. 'password' => 'require|length:4,30',
  111. '__token__' => 'require|token',
  112. ];
  113. $data = [
  114. 'username' => $username,
  115. 'password' => $password,
  116. '__token__' => $token,
  117. ];
  118. if (Config::get('fastadmin.login_captcha')) {
  119. $rule['captcha'] = 'require|captcha';
  120. $data['captcha'] = $this->request->post('captcha');
  121. }
  122. $validate = new Validate($rule, [], ['username' => __('Username'), 'password' => __('Password'), 'captcha' => __('Captcha')]);
  123. $result = $validate->check($data);
  124. if (!$result) {
  125. $this->error($validate->getError(), $url, ['token' => $this->request->token()]);
  126. }
  127. AdminLog::setTitle(__('Login') . '-' . $sysName);
  128. // 默认保持登录 72 小时(登录页无「记住我」勾选时也生效,避免频繁掉线)
  129. $keeptime = $keeyloginhours * 3600;
  130. $result = $this->auth->login($username, $password, $keeptime);
  131. if ($result === true) {
  132. if (!SystemLogin::belongsTo($this->auth, (int)$this->auth->id, $systemKey)) {
  133. $this->auth->logout();
  134. Session::delete('login_system');
  135. $this->error('登录账号不存在', $url, ['token' => $this->request->token()]);
  136. }
  137. Session::set('login_system', $systemKey);
  138. Hook::listen("admin_login_after", $this->request);
  139. $this->success(__('Login successful'), $url, ['url' => $url, 'id' => $this->auth->id, 'username' => $username, 'avatar' => resolve_admin_avatar($this->auth->avatar)]);
  140. } else {
  141. $msg = $this->auth->getError();
  142. $msg = $msg ? $msg : __('Username or password is incorrect');
  143. $this->error($msg, $url, ['token' => $this->request->token()]);
  144. }
  145. }
  146. // 根据客户端的cookie,判断是否可以自动登录
  147. if ($this->auth->autologin()) {
  148. if (!SystemLogin::belongsTo($this->auth, (int)$this->auth->id, $systemKey)) {
  149. $this->auth->logout();
  150. Session::delete('login_system');
  151. } else {
  152. Session::set('login_system', $systemKey);
  153. Session::delete("referer");
  154. $this->redirect($url);
  155. }
  156. }
  157. $background = Config::get('fastadmin.login_background');
  158. $background = $background ? (stripos($background, 'http') === 0 ? $background : config('site.cdnurl') . $background) : '';
  159. $this->view->assign('keeyloginhours', $keeyloginhours);
  160. $this->view->assign('background', $background);
  161. $this->view->assign('login_system', $systemKey);
  162. $this->view->assign('title', $sysName . ' - ' . __('Login'));
  163. Hook::listen("admin_login_init", $this->request);
  164. return $this->view->fetch('login');
  165. }
  166. /**
  167. * 退出登录
  168. */
  169. public function logout()
  170. {
  171. if ($this->request->isPost()) {
  172. $this->auth->logout();
  173. Session::delete('login_system');
  174. Hook::listen("admin_logout_after", $this->request);
  175. $this->redirect('index/login');
  176. }
  177. $html = "<form id='logout_submit' name='logout_submit' action='' method='post'>" . token() . "<input type='submit' value='ok' style='display:none;'></form>";
  178. $html .= "<script>document.forms['logout_submit'].submit();</script>";
  179. return $html;
  180. }
  181. }