2025-01-20 08:09:54 +00:00
|
|
|
/**
|
|
|
|
* @Description: 路由权限
|
|
|
|
* @Author: zenghua.wang
|
|
|
|
* @Date: 2022-01-26 22:04:31
|
|
|
|
* @LastEditors: zenghua.wang
|
|
|
|
* @LastEditTime: 2024-05-22 13:36:31
|
|
|
|
*/
|
|
|
|
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-01-25 02:44:29 +00:00
|
|
|
const { VITE_APP_TITLE } = import.meta.env;
|
2025-01-20 08:09:54 +00:00
|
|
|
const whiteList = ['/login'];
|
|
|
|
|
|
|
|
router.beforeEach(async (to, from, next) => {
|
|
|
|
NProgress.start();
|
|
|
|
if (typeof to.meta.title === 'string') {
|
2025-01-25 02:44:29 +00:00
|
|
|
document.title = VITE_APP_TITLE + ' | ' + to.meta.title;
|
2025-01-20 08:09:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const userStore = useUserStore();
|
2025-02-13 07:06:39 +00:00
|
|
|
const hasToken = userStore.hasToken();
|
2025-02-17 06:43:55 +00:00
|
|
|
console.log('main', hasToken);
|
2025-01-20 08:09:54 +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);
|
|
|
|
accessRoutes.forEach((item) => router.addRoute(item));
|
2025-02-28 06:28:54 +00:00
|
|
|
return next({ ...to, replace: true });
|
2025-01-20 08:09:54 +00:00
|
|
|
} else {
|
2025-02-28 06:28:54 +00:00
|
|
|
if (from.path.includes('/sub') && (!to.path.includes('/sub') || to.path.includes('/platform'))) {
|
|
|
|
NProgress.done();
|
2025-01-25 02:44:29 +00:00
|
|
|
window.location.reload();
|
|
|
|
return;
|
|
|
|
}
|
2025-01-20 08:09:54 +00:00
|
|
|
next();
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
next(`/login?redirect=${to.path}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (whiteList.indexOf(to.path) !== -1) {
|
|
|
|
next();
|
|
|
|
} else {
|
|
|
|
next(`/login?redirect=${to.path}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2025-01-25 02:44:29 +00:00
|
|
|
router.afterEach(() => {
|
2025-01-20 08:09:54 +00:00
|
|
|
NProgress.done();
|
|
|
|
});
|