WholeProcessPlatform/frontend/src/permission.ts

136 lines
3.9 KiB
TypeScript
Raw Normal View History

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', '/register']; //login
// ✅ 新增:递归标准化路由路径,确保所有 path 以 '/' 开头
function normalizeRoutes(routes: any[]): any[] {
return routes.map(route => {
// 创建副本以避免直接修改原始数据(可选,视具体需求而定)
const normalizedRoute = { ...route };
// 修正当前路由的 path
if (normalizedRoute.path && !normalizedRoute.path.startsWith('/')) {
normalizedRoute.path = `/${normalizedRoute.path}`;
}
// 递归修正子路由
if (normalizedRoute.children && normalizedRoute.children.length > 0) {
normalizedRoute.children = normalizeRoutes(normalizedRoute.children);
}
return normalizedRoute;
});
}
2026-03-25 10:02:19 +08:00
2026-03-27 14:50:35 +08:00
// 查找第一个可用路由
2026-04-22 17:53:20 +08:00
function findFirstAvailableRoute(routes: any[]): string | undefined {
2026-03-27 14:50:35 +08:00
for (const route of routes) {
if (route.meta?.hidden) continue;
2026-04-21 14:42:10 +08:00
2026-03-27 14:50:35 +08:00
if (route.children?.length > 0) {
const child = route.children[0];
// 优先使用 opturl 或 path
const targetPath = child.opturl || child.path;
return targetPath?.startsWith('/') ? targetPath : `/${targetPath}`;
}
2026-04-21 14:42:10 +08:00
2026-03-27 14:50:35 +08:00
const targetPath = route.opturl || route.path;
return targetPath;
}
return '/404';
}
2026-03-25 10:02:19 +08:00
router.beforeEach(async (to, from, next) => {
NProgress.start();
const userStore = useUserStoreHook();
2026-04-21 14:42:10 +08:00
2026-03-25 10:02:19 +08:00
if (userStore.Token) {
// 登录成功,跳转到首页
if (to.path === '/login') {
//login
2026-03-25 10:02:19 +08:00
next({ path: '/' });
NProgress.done();
return;
} else if (
(to.path === '/401' || to.path === '/404') &&
from.matched.length === 0 &&
!from.name
) {
// 拦截直接 URL 访问错误页面,仅允许内部重定向触发
next({ path: '/' });
NProgress.done();
return;
2026-03-25 10:02:19 +08:00
} else {
const hasGetUserInfo = userStore.roles.length > 0;
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();
let accessRoutes: RouteRecordRaw[] =
await permissionStore.generateRoutes(roles);
// ✅ 关键修复:在添加路由前,标准化所有路径
accessRoutes = normalizeRoutes(accessRoutes);
2026-04-21 14:42:10 +08:00
2026-03-25 10:02:19 +08:00
accessRoutes.forEach((route: any) => {
router.addRoute(route);
});
2026-04-21 14:42:10 +08:00
2026-03-27 14:50:35 +08:00
// 关键:如果是根路径,加载完路由后跳转到第一个可用路由
if (to.path === '/') {
const firstRoute = findFirstAvailableRoute(accessRoutes);
if (firstRoute) {
next(firstRoute);
NProgress.done();
return;
}
}
2026-04-21 14:42:10 +08:00
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();
2026-05-05 22:38:24 +08:00
next(`/login`);
2026-03-25 10:02:19 +08:00
NProgress.done();
}
}
}
} else {
// 未登录可以访问白名单页面
if (whiteList.indexOf(to.path) !== -1) {
next();
} else {
2026-05-05 22:38:24 +08:00
next(`/login`);
2026-03-25 10:02:19 +08:00
NProgress.done();
}
}
});
router.afterEach(() => {
NProgress.done();
});