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: '/' }); next({ path: '/' });
} else { } else {
if (to.meta && to.meta.role) { // 判断即将进入的页面是否需要权限 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)) { // 判断权限 if (hasPermission(store.getters.roles, to.meta.role)) { // 判断权限
next(); // 有权限 next(); // 有权限
} else { } else {
@ -70,7 +70,20 @@ router.beforeEach((to, from, next) => {
}); });
} }
} else { // 页面不需要权限 直接进入 } 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 { } else {

View File

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

View File

@ -12,9 +12,7 @@
<script> <script>
import { Navbar, Sidebar, AppMain } from 'views/layout'; import { Navbar, Sidebar, AppMain } from 'views/layout';
import store from 'store';
import router from 'router';
import permission from 'store/permission';
export default { export default {
name: 'layout', name: 'layout',
components: { components: {
@ -26,22 +24,6 @@
sidebar() { sidebar() {
return this.$store.state.app.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> </script>