2026-03-25 10:02:19 +08:00
|
|
|
import router from '@/router';
|
|
|
|
|
import { RouteRecordRaw } from 'vue-router';
|
|
|
|
|
import { useUserStoreHook } from '@/store/modules/user';
|
|
|
|
|
import { usePermissionStoreHook } from '@/store/modules/permission';
|
|
|
|
|
|
|
|
|
|
import NProgress from 'nprogress';
|
|
|
|
|
import 'nprogress/nprogress.css';
|
2026-03-27 14:50:35 +08:00
|
|
|
NProgress.configure({ showSpinner: false });
|
2026-03-25 10:02:19 +08:00
|
|
|
|
|
|
|
|
const permissionStore = usePermissionStoreHook();
|
|
|
|
|
|
|
|
|
|
// 白名单路由
|
|
|
|
|
const whiteList = ['/login'];
|
|
|
|
|
|
2026-03-27 14:50:35 +08:00
|
|
|
// 查找第一个可用路由
|
|
|
|
|
function findFirstAvailableRoute(routes: RouteRecordRaw[]): string | undefined {
|
|
|
|
|
for (const route of routes) {
|
|
|
|
|
if (route.meta?.hidden) continue;
|
|
|
|
|
|
|
|
|
|
if (route.children?.length > 0) {
|
|
|
|
|
const child = route.children[0];
|
|
|
|
|
// 优先使用 opturl 或 path
|
|
|
|
|
const targetPath = child.opturl || child.path;
|
|
|
|
|
return targetPath?.startsWith('/') ? targetPath : `/${targetPath}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const targetPath = route.opturl || route.path;
|
|
|
|
|
return targetPath;
|
|
|
|
|
}
|
|
|
|
|
return '/404';
|
2026-03-25 17:32:09 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-25 10:02:19 +08:00
|
|
|
router.beforeEach(async (to, from, next) => {
|
|
|
|
|
NProgress.start();
|
|
|
|
|
const userStore = useUserStoreHook();
|
2026-03-27 14:50:35 +08:00
|
|
|
|
2026-03-25 10:02:19 +08:00
|
|
|
if (userStore.Token) {
|
|
|
|
|
// 登录成功,跳转到首页
|
|
|
|
|
if (to.path === '/login') {
|
|
|
|
|
next({ path: '/' });
|
|
|
|
|
NProgress.done();
|
|
|
|
|
} else {
|
|
|
|
|
const hasGetUserInfo = userStore.roles.length > 0;
|
2026-03-27 14:50:35 +08:00
|
|
|
|
2026-03-25 10:02:19 +08:00
|
|
|
if (hasGetUserInfo) {
|
2026-03-27 14:50:35 +08:00
|
|
|
// 已获取用户信息,检查路由匹配
|
2026-03-25 10:02:19 +08:00
|
|
|
if (to.matched.length === 0) {
|
2026-03-27 14:50:35 +08:00
|
|
|
// 路由未匹配,可能是访问根路径
|
|
|
|
|
if (to.path === '/') {
|
|
|
|
|
const firstRoute = findFirstAvailableRoute(permissionStore.routes);
|
|
|
|
|
if (firstRoute) {
|
|
|
|
|
next(firstRoute);
|
|
|
|
|
NProgress.done();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-03-25 10:02:19 +08:00
|
|
|
from.name ? next({ name: from.name as any }) : next('/401');
|
|
|
|
|
} else {
|
|
|
|
|
next();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
try {
|
|
|
|
|
const { roles } = await userStore.getInfo();
|
2026-03-27 14:50:35 +08:00
|
|
|
const accessRoutes: RouteRecordRaw[] = await permissionStore.generateRoutes(roles);
|
|
|
|
|
|
2026-03-25 10:02:19 +08:00
|
|
|
accessRoutes.forEach((route: any) => {
|
|
|
|
|
router.addRoute(route);
|
|
|
|
|
});
|
2026-03-27 14:50:35 +08:00
|
|
|
|
|
|
|
|
// 关键:如果是根路径,加载完路由后跳转到第一个可用路由
|
|
|
|
|
if (to.path === '/') {
|
|
|
|
|
const firstRoute = findFirstAvailableRoute(accessRoutes);
|
|
|
|
|
if (firstRoute) {
|
|
|
|
|
next(firstRoute);
|
|
|
|
|
NProgress.done();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-25 10:02:19 +08:00
|
|
|
next({ ...to, replace: true });
|
|
|
|
|
} catch (error) {
|
2026-03-27 14:50:35 +08:00
|
|
|
console.log(error);
|
2026-03-25 10:02:19 +08:00
|
|
|
await userStore.resetToken();
|
|
|
|
|
next(`/login?redirect=${to.path}`);
|
|
|
|
|
NProgress.done();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// 未登录可以访问白名单页面
|
|
|
|
|
if (whiteList.indexOf(to.path) !== -1) {
|
|
|
|
|
next();
|
|
|
|
|
} else {
|
|
|
|
|
next(`/login?redirect=${to.path}`);
|
|
|
|
|
NProgress.done();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
router.afterEach(() => {
|
|
|
|
|
NProgress.done();
|
2026-03-27 14:50:35 +08:00
|
|
|
});
|