yunshangxie-admin/src/permission.js

58 lines
1.5 KiB
JavaScript

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'];
NProgress.configure({ showSpinner: false });
router.beforeEach(async (to, from, next) => {
NProgress.start();
document.title = '智慧云商协-'+to.meta.title;
const token = localStorage.getItem('associationToken');
if(store.state.user.association!=''){
if(typeof (store.state.user.association)=='object'){
var association=store.state.user.association;
}else{
var association=JSON.parse(store.state.user.association);
}
}
if (token) {
if (to.path === '/login') {
next('/dashboard/base');
return;
}
await store.dispatch('user/getUserInfo');
await store.dispatch('permission/initRoutes', store.getters['user/roles']);
const roles = store.getters['user/roles'];
if (roles && roles.length > 0) {
if(roles[0]==='ALL_ROUTERS'){
next();
}else{
if (roles.indexOf(to.name) !== -1) {
next();
} else {
next('/dashboard/base');
}
}
} else {
try {
next({ ...to , replace: true });
} catch (error) {
await store.commit('user/removeToken');
next(`/login`);
}
}
} else {
if (whiteListRouters.indexOf(to.path) !== -1) {
next();
} else {
next(`/login`);
}
NProgress.done();
}
});
router.afterEach(() => {
NProgress.done();
});