yunshangxie-admin/src/permission.js

67 lines
1.6 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';
NProgress.configure({ showSpinner: false });
const whiteListRouters = store.getters['permission/whiteListRouters'];
router.beforeEach(async (to, from, next) => {
NProgress.start();
document.title = '智慧云商协-'+to.meta.title;
const token = localStorage.getItem('associationToken');
console.log(token);
console.log(to.path);
2024-05-11 18:14:00 +08:00
console.log(to.path);
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') {
next();
return;
}
const roles = store.getters['user/roles'];
console.log(roles);
if (roles && roles.length > 0) {
next();
} else {
try {
await store.dispatch('user/getUserInfo');
await store.dispatch('permission/initRoutes', store.getters['user/roles']);
next({ ...to });
} catch (error) {
await store.commit('user/removeToken');
2024-05-11 18:14:00 +08:00
next(`/login?id=`+association.id?association.id:1);
2024-04-18 13:44:38 +08:00
NProgress.done();
}
}
} else {
/* white list router */
if (whiteListRouters.indexOf(to.path) !== -1) {
next();
} else {
2024-05-11 18:14:00 +08:00
console.log(association);
if(typeof (association)!='undefined'){
2024-05-16 18:15:30 +08:00
next(`/login`);
2024-05-11 18:14:00 +08:00
}else{
2024-05-16 18:15:30 +08:00
next(`/login`);
2024-05-11 18:14:00 +08:00
}
2024-04-18 13:44:38 +08:00
}
NProgress.done();
}
});
router.afterEach(() => {
NProgress.done();
});