使用addRoutes重构权限

This commit is contained in:
Pan
2017-05-17 16:26:33 +08:00
parent 624af00ba3
commit faea4d8bf6
10 changed files with 191 additions and 192 deletions

View File

@@ -16,21 +16,11 @@ const app = {
Cookies.set('sidebarStatus', 0);
}
state.sidebar.opened = !state.sidebar.opened;
},
SET_LIVENEWS_CHANNELS: (status, channels) => {
status.livenewsChannels = JSON.stringify(channels);
Cookies.set('livenewsChannels', JSON.stringify(channels));
}
},
actions: {
ToggleSideBar: ({ commit }) => {
commit('TOGGLE_SIDEBAR')
},
setTheme: ({ commit }, theme) => {
commit('SET_THEME', theme)
},
setlivenewsChannels: ({ commit }, channels) => {
commit('SET_LIVENEWS_CHANNELS', channels)
}
}
};

View File

@@ -0,0 +1,51 @@
import { asyncRouterMap, constantRouterMap } from 'src/router';
function hasPermission(roles, route) {
if (route.meta && route.meta.role) {
return roles.some(role => route.meta.role.indexOf(role) >= 0)
} else {
return true
}
}
const permission = {
state: {
routers: constantRouterMap,
addRouters: []
},
mutations: {
SET_ROUTERS: (state, routers) => {
state.addRouters = routers;
state.routers = constantRouterMap.concat(routers);
}
},
actions: {
// s
GenerateRoutes({ commit }, data) {
const { roles } = data;
const accessedRouters = asyncRouterMap.filter(v => {
if (roles.indexOf('admin') >= 0) return true;
if (hasPermission(roles, v)) {
if (v.children && v.children.length > 0) {
v.children = v.children.filter(child => {
if (hasPermission(roles, child)) {
return child
}
return false;
});
return v
} else {
return v
}
}
return false;
});
commit('SET_ROUTERS', accessedRouters);
}
}
};
export default permission;