Skip to content

Commit 1072572

Browse files
sunchenguangPanJiaChen
authored andcommitted
update: 递归过滤异步路由表
1 parent 046d136 commit 1072572

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

src/store/modules/permission.js

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,26 @@ function hasPermission(roles, route) {
88
}
99
}
1010

11+
/**
12+
* 递归过滤异步路由表,返回符合用户角色权限的路由表
13+
* @param asyncRouterMap
14+
* @param roles
15+
* @returns {Array.<T>|*}
16+
*/
17+
function filterAsyncRouter(asyncRouterMap, roles) {
18+
let accessedRouters = asyncRouterMap.filter(route => {
19+
if(hasPermission(roles, route)) {
20+
if(route.children && route.children.length) {
21+
route.children = filterAsyncRouter(route.children, roles)
22+
}
23+
return true
24+
}
25+
return false
26+
})
27+
return accessedRouters
28+
}
29+
30+
1131
const permission = {
1232
state: {
1333
routers: constantRouterMap,
@@ -25,23 +45,12 @@ const permission = {
2545
GenerateRoutes({ commit }, data) {
2646
return new Promise(resolve => {
2747
const { roles } = data;
28-
const accessedRouters = asyncRouterMap.filter(v => {
29-
if (roles.indexOf('admin') >= 0) return true;
30-
if (hasPermission(roles, v)) {
31-
if (v.children && v.children.length > 0) {
32-
v.children = v.children.filter(child => {
33-
if (hasPermission(roles, child)) {
34-
return child
35-
}
36-
return false;
37-
});
38-
return v
39-
} else {
40-
return v
41-
}
42-
}
43-
return false;
44-
});
48+
let accessedRouters
49+
if (roles.indexOf('admin') >= 0) {
50+
accessedRouters = asyncRouterMap
51+
} else {
52+
accessedRouters = filterAsyncRouter(asyncRouterMap, roles)
53+
}
4554
commit('SET_ROUTERS', accessedRouters);
4655
resolve();
4756
})

0 commit comments

Comments
 (0)