diff --git a/apps/web-antdv-next/.env.development b/apps/web-antdv-next/.env.development index 3a91dad..3b8df81 100644 --- a/apps/web-antdv-next/.env.development +++ b/apps/web-antdv-next/.env.development @@ -2,6 +2,7 @@ VITE_PORT=5999 VITE_BASE=/ +VITE_ROUTER_HISTORY=hash # 接口地址 VITE_GLOB_API_URL=/api diff --git a/apps/web-antdv-next/src/router/guard.ts b/apps/web-antdv-next/src/router/guard.ts index 3290d7b..3ac15f2 100644 --- a/apps/web-antdv-next/src/router/guard.ts +++ b/apps/web-antdv-next/src/router/guard.ts @@ -28,7 +28,21 @@ function getRedirectQuery(queryRedirect?: string) { if (queryRedirect) return queryRedirect; // 兼容旧链接 /auth/login?redirect=... 在 hash 路由下被放到 location.search 的情况。 - return new URLSearchParams(window.location.search).get('redirect') || ''; + const searchRedirect = new URLSearchParams(window.location.search).get( + 'redirect', + ); + if (searchRedirect) return searchRedirect; + + // Web/Playground 生产配置会跳 /#/auth/login?redirect=...; + // Admin 本地 history 模式下该 query 只存在于 location.hash。 + const hashQueryIndex = window.location.hash.indexOf('?'); + if (hashQueryIndex === -1) return ''; + + return ( + new URLSearchParams(window.location.hash.slice(hashQueryIndex + 1)).get( + 'redirect', + ) || '' + ); } /** diff --git a/apps/web-antdv-next/src/store/auth.ts b/apps/web-antdv-next/src/store/auth.ts index 5925bec..f85c038 100644 --- a/apps/web-antdv-next/src/store/auth.ts +++ b/apps/web-antdv-next/src/store/auth.ts @@ -38,9 +38,19 @@ export const useAuthStore = defineStore('auth', () => { if (routeRedirect) return routeRedirect; // 兼容旧链接 /auth/login?redirect=... 在 hash 路由下被放到 location.search 的情况。 - return new URLSearchParams(window.location.search).get('redirect') as - | null - | string; + const searchRedirect = new URLSearchParams(window.location.search).get( + 'redirect', + ); + if (searchRedirect) return searchRedirect; + + // Web/Playground 生产配置会跳 /#/auth/login?redirect=...; + // Admin 本地 history 模式下该 query 只存在于 location.hash。 + const hashQueryIndex = window.location.hash.indexOf('?'); + if (hashQueryIndex === -1) return null; + + return new URLSearchParams( + window.location.hash.slice(hashQueryIndex + 1), + ).get('redirect'); } function buildExternalAuthRedirectUrl(target: string) {