refine main.js role logic

This commit is contained in:
Pan 2017-05-16 17:31:48 +08:00
parent 0427359d18
commit d820ca95e1
3 changed files with 16 additions and 22 deletions

View file

@ -48,7 +48,7 @@ router.beforeEach((to, from, next) => {
next({ path: '/' });
} else {
if (to.meta && to.meta.role) { // 判断即将进入的页面是否需要权限
if (store.getters.uid) { // 判断当前用户是否已拉取完info信息
if (store.getters.roles.length !== 0) { // 判断当前用户是否已拉取完info信息
if (hasPermission(store.getters.roles, to.meta.role)) { // 判断权限
next(); // 有权限
} else {
@ -70,7 +70,20 @@ router.beforeEach((to, from, next) => {
});
}
} else { // 页面不需要权限 直接进入
next();
console.log(store.getters.roles)
if (store.getters.roles.length !== 0) {
next();
} else {
store.dispatch('GetInfo').then(() => {
permission.init({
roles: store.getters.roles,
router: router.options.routes
});
next();
}).catch(err => {
console.log(err);
});
}
}
}
} else {

View file

@ -30,7 +30,6 @@ const userMap = {
export default {
loginByEmail: config => {
console.log(config)
const { email } = JSON.parse(config.body);
return userMap[email.split('@')[0]];
},

View file

@ -12,9 +12,7 @@
<script>
import { Navbar, Sidebar, AppMain } from 'views/layout';
import store from 'store';
import router from 'router';
import permission from 'store/permission';
export default {
name: 'layout',
components: {
@ -26,22 +24,6 @@
sidebar() {
return this.$store.state.app.sidebar;
}
},
beforeRouteEnter: (to, from, next) => {
const roles = store.getters.roles;
if (roles.length !== 0) {
next();
return
}
store.dispatch('GetInfo').then(() => {
permission.init({
roles: store.getters.roles,
router: router.options.routes
});
next();
}).catch(err => {
console.log(err);
});
}
}
</script>