From ff32a8111da573eccd383df5cc56723bd54f4e99 Mon Sep 17 00:00:00 2001 From: sunlei Date: Sun, 17 May 2026 13:57:22 +0800 Subject: [PATCH] =?UTF-8?q?fix(admin):=20=E5=85=BC=E5=AE=B9=E8=B7=A8?= =?UTF-8?q?=E5=BA=94=E7=94=A8=E7=99=BB=E5=BD=95=E5=9B=9E=E8=B7=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/web-antdv-next/src/router/guard.ts | 15 +++++++++++---- apps/web-antdv-next/src/store/auth.ts | 17 ++++++++++++++--- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/apps/web-antdv-next/src/router/guard.ts b/apps/web-antdv-next/src/router/guard.ts index c5ef15e..2beb371 100644 --- a/apps/web-antdv-next/src/router/guard.ts +++ b/apps/web-antdv-next/src/router/guard.ts @@ -26,6 +26,13 @@ function redirectToExternalUrl(url: string) { return true; } +function getRedirectQuery(queryRedirect?: string) { + if (queryRedirect) return queryRedirect; + + // 兼容旧链接 /auth/login?redirect=... 在 hash 路由下被放到 location.search 的情况。 + return new URLSearchParams(window.location.search).get('redirect') || ''; +} + /** * 通用守卫配置 * @param router @@ -68,7 +75,7 @@ function setupAccessGuard(router: Router) { if (coreRouteNames.includes(to.name as string)) { if (to.path === LOGIN_PATH && accessStore.accessToken) { const redirectPath = - decodeRedirect(to.query?.redirect as string) || + decodeRedirect(getRedirectQuery(to.query?.redirect as string)) || userStore.userInfo?.homePath || preferences.app.defaultHomePath; @@ -125,10 +132,10 @@ function setupAccessGuard(router: Router) { accessStore.setAccessRoutes(accessibleRoutes); accessStore.setIsAccessChecked(true); let redirectPath: string; - if (from.query.redirect) { + const fromRedirect = getRedirectQuery(from.query.redirect as string); + if (fromRedirect) { redirectPath = - decodeRedirect(from.query.redirect as string) || - preferences.app.defaultHomePath; + decodeRedirect(fromRedirect) || preferences.app.defaultHomePath; } else if (to.fullPath === preferences.app.defaultHomePath) { redirectPath = preferences.app.defaultHomePath; } else if (userInfo.homePath && to.fullPath === userInfo.homePath) { diff --git a/apps/web-antdv-next/src/store/auth.ts b/apps/web-antdv-next/src/store/auth.ts index 0c8a0b6..90e2711 100644 --- a/apps/web-antdv-next/src/store/auth.ts +++ b/apps/web-antdv-next/src/store/auth.ts @@ -30,10 +30,21 @@ export const useAuthStore = defineStore('auth', () => { } } + function getRedirectQuery() { + const routeRedirect = router.currentRoute.value.query?.redirect as + | string + | undefined; + + if (routeRedirect) return routeRedirect; + + // 兼容旧链接 /auth/login?redirect=... 在 hash 路由下被放到 location.search 的情况。 + return new URLSearchParams(window.location.search).get('redirect') as + | null + | string; + } + async function goToRedirect(fallbackPath: string) { - const redirect = decodeRedirect( - router.currentRoute.value.query?.redirect as string | undefined, - ); + const redirect = decodeRedirect(getRedirectQuery() || undefined); const target = redirect || fallbackPath; if (/^https?:\/\//i.test(target)) {