forked from AkkomaGang/admin-fe
update: 递归过滤异步路由表
This commit is contained in:
parent
046d1369d2
commit
1072572ac6
1 changed files with 26 additions and 17 deletions
|
@ -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) {
|
|
||||||
v.children = v.children.filter(child => {
|
|
||||||
if (hasPermission(roles, child)) {
|
|
||||||
return child
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
});
|
|
||||||
return v
|
|
||||||
} else {
|
} else {
|
||||||
return v
|
accessedRouters = filterAsyncRouter(asyncRouterMap, roles)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return false;
|
|
||||||
});
|
|
||||||
commit('SET_ROUTERS', accessedRouters);
|
commit('SET_ROUTERS', accessedRouters);
|
||||||
resolve();
|
resolve();
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue