59 lines
1.8 KiB
JavaScript
59 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 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 whiteList = ['/login', '/auth-redirect']; // 设置白名单
|
||
|
|
||
|
router.beforeEach(async (to, from, next) => {
|
||
|
NProgress.start();
|
||
|
if (typeof to.meta.title === 'string') {
|
||
|
document.title = '政务服务 | ' + to.meta.title;
|
||
|
}
|
||
|
|
||
|
const userStore = useUserStore();
|
||
|
const hasToken = true;
|
||
|
|
||
|
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)); // 动态添加访问路由表
|
||
|
next({ ...to, replace: true }); // 这里相当于push到一个页面 不在进入路由拦截
|
||
|
} else {
|
||
|
next(); // 如果不传参数就会重新执行路由拦截,重新进到这里
|
||
|
}
|
||
|
} catch (error) {
|
||
|
next(`/login?redirect=${to.path}`);
|
||
|
}
|
||
|
}
|
||
|
} else {
|
||
|
if (whiteList.indexOf(to.path) !== -1) {
|
||
|
next();
|
||
|
} else {
|
||
|
next(`/login?redirect=${to.path}`);
|
||
|
}
|
||
|
}
|
||
|
});
|
||
|
|
||
|
router.afterEach(() => {
|
||
|
NProgress.done();
|
||
|
});
|