commit | author | age
|
9bcb19
|
1 |
import { asyncRouterMap, constantRouterMap } from '@/config/router.config' |
I |
2 |
|
|
3 |
/** |
|
4 |
* 过滤账户是否拥有某一个权限,并将菜单从加载列表移除 |
|
5 |
* |
|
6 |
* @param permission |
|
7 |
* @param route |
|
8 |
* @returns {boolean} |
|
9 |
*/ |
|
10 |
function hasPermission (permission, route) { |
|
11 |
// if (route.meta && route.meta.permission) { |
|
12 |
// let flag = false |
|
13 |
// for (let i = 0, len = permission.length; i < len; i++) { |
|
14 |
// flag = route.meta.permission.includes(permission[i]) |
|
15 |
// if (flag) { |
|
16 |
// return true |
|
17 |
// } |
|
18 |
// } |
|
19 |
// return false |
|
20 |
// } |
|
21 |
return true |
|
22 |
} |
|
23 |
|
|
24 |
/** |
|
25 |
* 单账户多角色时,使用该方法可过滤角色不存在的菜单 |
|
26 |
* |
|
27 |
* @param roles |
|
28 |
* @param route |
|
29 |
* @returns {*} |
|
30 |
*/ |
|
31 |
// eslint-disable-next-line |
|
32 |
function hasRole(roles, route) { |
|
33 |
if (route.meta && route.meta.roles) { |
|
34 |
return route.meta.roles.includes(roles.id) |
|
35 |
} else { |
|
36 |
return true |
|
37 |
} |
|
38 |
} |
|
39 |
|
|
40 |
function filterAsyncRouter (routerMap, roles) { |
|
41 |
const accessedRouters = routerMap.filter(route => { |
|
42 |
if (hasPermission(roles.permissionList, route)) { |
|
43 |
if (route.children && route.children.length) { |
|
44 |
route.children = filterAsyncRouter(route.children, roles) |
|
45 |
} |
|
46 |
return true |
|
47 |
} |
|
48 |
return false |
|
49 |
}) |
|
50 |
return accessedRouters |
|
51 |
} |
|
52 |
|
|
53 |
const permission = { |
|
54 |
state: { |
|
55 |
routers: constantRouterMap, |
|
56 |
addRouters: [] |
|
57 |
}, |
|
58 |
mutations: { |
|
59 |
SET_ROUTERS: (state, routers) => { |
|
60 |
state.addRouters = routers |
|
61 |
state.routers = constantRouterMap.concat(routers) |
|
62 |
} |
|
63 |
}, |
|
64 |
actions: { |
|
65 |
GenerateRoutes ({ commit }, data) { |
|
66 |
return new Promise(resolve => { |
|
67 |
const { roles } = data |
|
68 |
const accessedRouters = filterAsyncRouter(asyncRouterMap, roles) |
|
69 |
// console.log('动态获取到的菜单列表:'+JSON.stringify(accessedRouters)) |
|
70 |
commit('SET_ROUTERS', accessedRouters) |
|
71 |
resolve() |
|
72 |
}) |
|
73 |
} |
|
74 |
} |
|
75 |
} |
|
76 |
|
|
77 |
export default permission |