fix: ref写法优化 路由跳转逻辑优化 部分样式优化

This commit is contained in:
ZhuRui
2026-07-29 09:32:14 +08:00
parent a4dab891cf
commit 8439f64c40
11 changed files with 145 additions and 69 deletions
+25 -3
View File
@@ -1,6 +1,7 @@
import router from '@/router';
import { auth } from '@/hooks/useAuth';
import { useTabsStore } from '@/stores/tabsStore';
import { useMenuStore } from '@/stores/menuStore';
const appTitle = import.meta.env.VITE_APP_TITLE || 'CPMS 运营平台';
@@ -9,6 +10,11 @@ function updateDocumentTitle(title?: string) {
document.title = title ? `${title} - ${appTitle}` : appTitle;
}
function getRedirectPath(queryValue: unknown): string | undefined {
if (Array.isArray(queryValue)) return queryValue.find((item) => typeof item === 'string');
return typeof queryValue === 'string' ? queryValue : undefined;
}
/**
* 全局路由守卫
* 核心流程:
@@ -26,8 +32,21 @@ router.beforeEach(async (to, _from, next) => {
// ── /login 特殊处理 ──
if (to.path === '/login') {
if (auth.isLoggedIn()) {
const { homePath } = await import('@/stores/menuStore').then((m) => m.useMenuStore());
next(homePath.value);
const { loadMenu, loaded, homePath, isRoutePathAvailable } =
await import('@/stores/menuStore').then((m) => m.useMenuStore());
if (!loaded.value) {
const { loadPermissions } = await import('@/stores/permissionStore').then((m) =>
m.usePermissionStore(),
);
await Promise.all([loadMenu(), loadPermissions()]);
}
const redirectPath = getRedirectPath(to.query.redirect);
const targetPath =
redirectPath && isRoutePathAvailable(redirectPath)
? redirectPath
: homePath.value || '/404';
next(targetPath);
} else {
next();
}
@@ -36,7 +55,7 @@ router.beforeEach(async (to, _from, next) => {
// ── 未登录 → 跳转登录页 ──
if (!auth.isLoggedIn()) {
next('/login');
next({ path: '/login', query: { redirect: to.fullPath } });
return;
}
@@ -68,6 +87,9 @@ router.afterEach((to) => {
const componentName = (to.meta.componentName as string) || (to.name as string);
if (!componentName) return;
const { isRoutePathAvailable } = useMenuStore();
if (!isRoutePathAvailable(to.path)) return;
const { addTab } = useTabsStore();
addTab({
path: to.path,