Normalize legacy home redirects

This commit is contained in:
2026-06-09 18:45:02 +08:00
parent 39aa708c4f
commit f8bdc3e952
6 changed files with 49 additions and 33 deletions
+10 -15
View File
@@ -8,16 +8,10 @@ import { startProgress, stopProgress } from '@vben/utils';
import { accessRoutes, coreRouteNames } from '#/router/routes';
import { useAuthStore } from '#/store';
import { useAppContextStore } from '#/store/app-context';
import { normalizeHomePath } from '#/utils/legacy-home';
import { generateAccess } from './access';
function normalizeHomePath(path?: null | string) {
if (!path || path === '/page-render/main_home') {
return preferences.app.defaultHomePath;
}
return path;
}
/**
* 通用守卫配置
* @param router
@@ -118,9 +112,8 @@ function setupAccessGuard(router: Router) {
// 基本路由,这些路由不需要进入权限拦截
if (coreRouteNames.includes(to.name as string)) {
if (to.path === LOGIN_PATH && accessStore.accessToken) {
return decodeURIComponent(
(to.query?.redirect as string) ||
normalizeHomePath(userStore.userInfo?.homePath),
return normalizeHomePath(
(to.query?.redirect as string) || userStore.userInfo?.homePath,
);
}
return true;
@@ -211,13 +204,15 @@ function setupAccessGuard(router: Router) {
}
// 重定向逻辑
const redirectPath = (from.query.redirect ??
(to.path === preferences.app.defaultHomePath
? normalizeHomePath(userInfo.homePath)
: to.fullPath)) as string;
const redirectPath = normalizeHomePath(
(from.query.redirect ??
(to.path === preferences.app.defaultHomePath
? userInfo.homePath
: to.fullPath)) as string,
);
return {
...router.resolve(decodeURIComponent(redirectPath)),
...router.resolve(redirectPath),
replace: true,
};
});
+1 -8
View File
@@ -4,7 +4,6 @@ import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { LOGIN_PATH } from '@vben/constants';
import { preferences } from '@vben/preferences';
import { resetAllStores, useAccessStore, useUserStore } from '@vben/stores';
import { defineStore } from 'pinia';
@@ -12,13 +11,7 @@ import { defineStore } from 'pinia';
import { getAccessCodesApi, loginApi, logoutApi } from '#/api/core/auth';
import { getUserInfoApi } from '#/api/core/user';
import { $t } from '#/locales';
function normalizeHomePath(path?: null | string) {
if (!path || path === '/page-render/main_home') {
return preferences.app.defaultHomePath;
}
return path;
}
import { normalizeHomePath } from '#/utils/legacy-home';
export const useAuthStore = defineStore('auth', () => {
const accessStore = useAccessStore();
+33
View File
@@ -0,0 +1,33 @@
import { preferences } from '@vben/preferences';
const LEGACY_HOME_PATTERNS = [
/^\/app\/[^/]+\/(?:form-render|page-render)\//,
/^\/(?:form-render|page-render)\//,
/^\/online-dev(?:\/|$)/,
/^\/online-development(?:\/|$)/,
];
function decodePath(path: string) {
try {
return decodeURIComponent(path);
} catch {
return path;
}
}
function isLegacyHomePath(path: string) {
return LEGACY_HOME_PATTERNS.some((pattern) => pattern.test(path));
}
function normalizeHomePath(path?: null | string) {
if (!path) {
return preferences.app.defaultHomePath;
}
const decodedPath = decodePath(path);
return isLegacyHomePath(decodedPath)
? preferences.app.defaultHomePath
: decodedPath;
}
export { normalizeHomePath };
@@ -10,8 +10,7 @@ import { $t } from '@vben/locales';
import { getPreferencesConfigApi } from '#/api/core/ui-config';
import { useAuthStore } from '#/store';
const LEGACY_HOME_PATH = '/page-render/main_home';
import { normalizeHomePath } from '#/utils/legacy-home';
defineOptions({ name: 'Login' });
@@ -61,10 +60,6 @@ const getRedirectState = () => {
return redirect ? JSON.stringify({ redirect }) : undefined;
};
function normalizeRedirectPath(path: string) {
return path === LEGACY_HOME_PATH ? '/' : path;
}
type OAuthProvider =
| 'dingtalk'
| 'feishu'
@@ -115,7 +110,7 @@ async function handleLogin(params: Record<string, any>) {
if (redirect) {
// 有 redirect 参数时,登录成功后跳转到指定页面
await authStore.authLogin(params, async () => {
const targetPath = normalizeRedirectPath(decodeURIComponent(redirect));
const targetPath = normalizeHomePath(redirect);
await router.push(targetPath);
});
} else {