2025-02-28 06:28:54 +00:00

66 lines
1.8 KiB
JavaScript

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