Index.php 7.7 KB

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