fix(admin): 兼容跨应用登录回跳

This commit is contained in:
sunlei 2026-05-17 13:57:22 +08:00
parent 21d576b6fe
commit ff32a8111d
2 changed files with 25 additions and 7 deletions

View File

@ -26,6 +26,13 @@ function redirectToExternalUrl(url: string) {
return true; 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 * @param router
@ -68,7 +75,7 @@ function setupAccessGuard(router: Router) {
if (coreRouteNames.includes(to.name as string)) { if (coreRouteNames.includes(to.name as string)) {
if (to.path === LOGIN_PATH && accessStore.accessToken) { if (to.path === LOGIN_PATH && accessStore.accessToken) {
const redirectPath = const redirectPath =
decodeRedirect(to.query?.redirect as string) || decodeRedirect(getRedirectQuery(to.query?.redirect as string)) ||
userStore.userInfo?.homePath || userStore.userInfo?.homePath ||
preferences.app.defaultHomePath; preferences.app.defaultHomePath;
@ -125,10 +132,10 @@ function setupAccessGuard(router: Router) {
accessStore.setAccessRoutes(accessibleRoutes); accessStore.setAccessRoutes(accessibleRoutes);
accessStore.setIsAccessChecked(true); accessStore.setIsAccessChecked(true);
let redirectPath: string; let redirectPath: string;
if (from.query.redirect) { const fromRedirect = getRedirectQuery(from.query.redirect as string);
if (fromRedirect) {
redirectPath = redirectPath =
decodeRedirect(from.query.redirect as string) || decodeRedirect(fromRedirect) || preferences.app.defaultHomePath;
preferences.app.defaultHomePath;
} else if (to.fullPath === preferences.app.defaultHomePath) { } else if (to.fullPath === preferences.app.defaultHomePath) {
redirectPath = preferences.app.defaultHomePath; redirectPath = preferences.app.defaultHomePath;
} else if (userInfo.homePath && to.fullPath === userInfo.homePath) { } else if (userInfo.homePath && to.fullPath === userInfo.homePath) {

View File

@ -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) { async function goToRedirect(fallbackPath: string) {
const redirect = decodeRedirect( const redirect = decodeRedirect(getRedirectQuery() || undefined);
router.currentRoute.value.query?.redirect as string | undefined,
);
const target = redirect || fallbackPath; const target = redirect || fallbackPath;
if (/^https?:\/\//i.test(target)) { if (/^https?:\/\//i.test(target)) {