@@ -10,6 +10,7 @@ import { store } from "./store";
1010import { Provider } from "react-redux" ;
1111import theme from "./theme" ;
1212import { errorConfigStore } from "@/utils/errorConfigStore.ts" ;
13+ import { setCachedHomePageUrl , getCachedHomePageUrl } from "@/utils/systemParam" ;
1314import "@/i18n" ;
1415
1516function showLoadingUI ( ) {
@@ -43,41 +44,81 @@ function showLoadingUI() {
4344 ` ;
4445}
4546
47+ /**
48+ * 从 localStorage 读取 JWT token
49+ */
50+ function getAuthToken ( ) : string | null {
51+ const session = localStorage . getItem ( 'session' ) ;
52+ if ( session ) {
53+ try {
54+ return JSON . parse ( session ) . token || null ;
55+ } catch {
56+ return null ;
57+ }
58+ }
59+ return null ;
60+ }
61+
4662/**
4763 * 自定义首页URL重定向
4864 * 在任何渲染之前检查系统参数 sys.home.page.url,若已配置则立即跳转,确保无闪烁。
49- * 使用原始 fetch 避免触发 antd message 等尚未初始化的 UI 组件 。
65+ * 使用原始 fetch 但携带 JWT token,避免已登录用户仍收到 401 。
5066 */
51- async function checkHomePageRedirect ( ) : Promise < boolean > {
67+ async function checkHomePageRedirect ( ) : Promise < { redirected : boolean ; authNeeded : boolean } > {
5268 if ( window . location . pathname !== '/' ) {
53- return false ;
69+ return { redirected : false , authNeeded : false } ;
5470 }
71+
72+ const headers : Record < string , string > = { 'Content-Type' : 'application/json' } ;
73+ const token = getAuthToken ( ) ;
74+ if ( token ) {
75+ headers [ 'Authorization' ] = `Bearer ${ token } ` ;
76+ }
77+
5578 try {
5679 const response = await fetch ( '/api/sys-param/sys.home.page.url' , {
5780 method : 'GET' ,
5881 credentials : 'include' ,
59- headers : { 'Content-Type' : 'application/json' } ,
82+ headers,
6083 } ) ;
6184 if ( response . ok ) {
6285 const result = await response . json ( ) ;
6386 const url = result ?. data ?. paramValue ?. trim ( ) ;
6487 if ( url ) {
88+ setCachedHomePageUrl ( url ) ;
6589 window . location . replace ( url ) ;
66- return true ;
90+ return { redirected : true , authNeeded : false } ;
91+ }
92+ // 参数存在但值为空 → 管理员已清除,清掉缓存
93+ setCachedHomePageUrl ( null ) ;
94+ } else if ( response . status === 401 ) {
95+ // 未登录,尝试从缓存读取
96+ const cachedUrl = getCachedHomePageUrl ( ) ;
97+ if ( cachedUrl ) {
98+ window . location . replace ( cachedUrl ) ;
99+ return { redirected : true , authNeeded : false } ;
67100 }
101+ // 未登录且无缓存,需要弹出登录框
102+ return { redirected : false , authNeeded : true } ;
68103 }
69104 } catch {
70- // 忽略错误,继续正常启动
105+ // 网络错误等,尝试从缓存读取
106+ const cachedUrl = getCachedHomePageUrl ( ) ;
107+ if ( cachedUrl ) {
108+ window . location . replace ( cachedUrl ) ;
109+ return { redirected : true , authNeeded : false } ;
110+ }
71111 }
72- return false ;
112+ return { redirected : false , authNeeded : false } ;
73113}
74114
75115async function bootstrap ( ) {
76116 const container = document . getElementById ( "root" ) ;
77117 if ( ! container ) return ;
78118
79119 // 在任何 UI 渲染之前检查自定义首页重定向
80- if ( await checkHomePageRedirect ( ) ) {
120+ const { redirected, authNeeded } = await checkHomePageRedirect ( ) ;
121+ if ( redirected ) {
81122 return ;
82123 }
83124
@@ -106,6 +147,13 @@ async function bootstrap() {
106147 </ Provider >
107148 </ StrictMode >
108149 ) ;
150+
151+ // 未登录且无缓存时,等 React 挂载后弹出登录框
152+ if ( authNeeded ) {
153+ setTimeout ( ( ) => {
154+ window . dispatchEvent ( new CustomEvent ( 'show-login' ) ) ;
155+ } , 500 ) ;
156+ }
109157}
110158
111159bootstrap ( ) ;
0 commit comments