update: 递归过滤异步路由表

This commit is contained in:
孙晨光 2017-06-23 14:45:59 +08:00 committed by 花裤衩
parent 046d1369d2
commit 1072572ac6
1 changed files with 26 additions and 17 deletions

View File

@ -8,6 +8,26 @@ function hasPermission(roles, route) {
} }
} }
/**
* 递归过滤异步路由表返回符合用户角色权限的路由表
* @param asyncRouterMap
* @param roles
* @returns {Array.<T>|*}
*/
function filterAsyncRouter(asyncRouterMap, roles) {
let accessedRouters = asyncRouterMap.filter(route => {
if(hasPermission(roles, route)) {
if(route.children && route.children.length) {
route.children = filterAsyncRouter(route.children, roles)
}
return true
}
return false
})
return accessedRouters
}
const permission = { const permission = {
state: { state: {
routers: constantRouterMap, routers: constantRouterMap,
@ -25,23 +45,12 @@ const permission = {
GenerateRoutes({ commit }, data) { GenerateRoutes({ commit }, data) {
return new Promise(resolve => { return new Promise(resolve => {
const { roles } = data; const { roles } = data;
const accessedRouters = asyncRouterMap.filter(v => { let accessedRouters
if (roles.indexOf('admin') >= 0) return true; if (roles.indexOf('admin') >= 0) {
if (hasPermission(roles, v)) { accessedRouters = asyncRouterMap
if (v.children && v.children.length > 0) { } else {
v.children = v.children.filter(child => { accessedRouters = filterAsyncRouter(asyncRouterMap, roles)
if (hasPermission(roles, child)) { }
return child
}
return false;
});
return v
} else {
return v
}
}
return false;
});
commit('SET_ROUTERS', accessedRouters); commit('SET_ROUTERS', accessedRouters);
resolve(); resolve();
}) })