2025-06-05 17:40:58 +08:00

59 lines
1.7 KiB
JavaScript

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();
});