66 lines
1.8 KiB
JavaScript
Raw Normal View History

2025-01-23 01:10:59 +00:00
/**
* @Description: 路由权限
* @Author: zenghua.wang
* @Date: 2022-01-26 22:04:31
* @LastEditors: zenghua.wang
* @LastEditTime: 2024-02-26 13:54:43
*/
2025-02-27 01:24:25 +00:00
import { qiankunWindow } from 'vite-plugin-qiankun/dist/helper';
2025-01-23 01:10:59 +00:00
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';
import router from '@/router';
import { useUserStore } from '@/store/modules/user';
import { usePermissionStore } from '@/store/modules/permission';
NProgress.configure({ showSpinner: false });
2025-02-27 01:24:25 +00:00
const { VITE_APP_MIAN_URL } = import.meta.env;
const whiteList = [];
2025-01-23 01:10:59 +00:00
router.beforeEach(async (to, from, next) => {
NProgress.start();
if (typeof to.meta.title === 'string') {
document.title = '政务服务 | ' + to.meta.title;
}
const userStore = useUserStore();
2025-02-17 06:43:55 +00:00
const hasToken = userStore.hasToken();
console.log('sub', hasToken);
2025-01-23 01:10:59 +00:00
if (hasToken) {
if (to.path === '/login') {
next({ path: '/' });
} else {
try {
const PermissionStore = usePermissionStore();
if (!PermissionStore.routes.length) {
const accessRoutes = await PermissionStore.generateRoutes(userStore.roles);
2025-02-27 01:24:25 +00:00
accessRoutes.forEach((item) => router.addRoute(item));
2025-02-28 06:28:54 +00:00
return next({ ...to, replace: true });
2025-01-23 01:10:59 +00:00
} else {
2025-02-28 06:28:54 +00:00
if (from.path.includes('/sub') && to.path.includes('/platform')) {
window.location.reload();
return;
}
2025-02-27 01:24:25 +00:00
next();
2025-01-23 01:10:59 +00:00
}
} catch (error) {
next(`/login?redirect=${to.path}`);
}
}
} else {
2025-02-27 01:24:25 +00:00
NProgress.done();
2025-01-23 01:10:59 +00:00
if (whiteList.indexOf(to.path) !== -1) {
next();
} else {
2025-02-27 01:24:25 +00:00
if (qiankunWindow.__POWERED_BY_QIANKUN__) {
window.location.href = VITE_APP_MIAN_URL;
return;
}
2025-01-23 01:10:59 +00:00
next(`/login?redirect=${to.path}`);
}
}
});
router.afterEach(() => {
NProgress.done();
});