yunshangxie-admin/src/permission.js

58 lines
1.5 KiB
JavaScript
Raw Normal View History

2024-04-18 13:44:38 +08:00
import NProgress from 'nprogress'; // progress bar
import 'nprogress/nprogress.css'; // progress bar style
import store from '@/store';
import router from '@/router';
const whiteListRouters = store.getters['permission/whiteListRouters'];
2024-06-12 09:26:27 +08:00
NProgress.configure({ showSpinner: false });
2024-04-18 13:44:38 +08:00
router.beforeEach(async (to, from, next) => {
NProgress.start();
document.title = '智慧云商协-'+to.meta.title;
const token = localStorage.getItem('associationToken');
2024-05-11 18:14:00 +08:00
if(store.state.user.association!=''){
2024-04-18 17:57:52 +08:00
if(typeof (store.state.user.association)=='object'){
var association=store.state.user.association;
}else{
var association=JSON.parse(store.state.user.association);
}
}
2024-04-18 13:44:38 +08:00
if (token) {
if (to.path === '/login') {
2024-06-12 09:26:27 +08:00
next('/dashboard/base');
2024-04-18 13:44:38 +08:00
return;
}
2024-06-12 09:26:27 +08:00
await store.dispatch('user/getUserInfo');
await store.dispatch('permission/initRoutes', store.getters['user/roles']);
2024-04-18 13:44:38 +08:00
const roles = store.getters['user/roles'];
if (roles && roles.length > 0) {
2024-06-12 09:26:27 +08:00
if(roles[0]==='ALL_ROUTERS'){
next();
}else{
if (roles.indexOf(to.name) !== -1) {
next();
} else {
next('/dashboard/base');
}
}
2024-04-18 13:44:38 +08:00
} else {
try {
2024-06-12 09:26:27 +08:00
next({ ...to , replace: true });
2024-04-18 13:44:38 +08:00
} catch (error) {
await store.commit('user/removeToken');
2024-06-12 09:26:27 +08:00
next(`/login`);
2024-04-18 13:44:38 +08:00
}
}
} else {
if (whiteListRouters.indexOf(to.path) !== -1) {
next();
} else {
2024-06-12 09:26:27 +08:00
next(`/login`);
2024-04-18 13:44:38 +08:00
}
NProgress.done();
}
});
router.afterEach(() => {
NProgress.done();
});