menu.js
3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import axios from '../../util/http'
import {asyncRouterMap, constantRouterMap } from '@/router'
import {url} from '../../util/api'
/**
* 获取路由列表
*/
function getMenuInfo(token) {
return new Promise((reslove, reject) =>{
axios.get(url + "/auth/apps/" + 'e38b9e91e27df7295282cd1d876f5f26' + "/menus?shape=list").then(response => {
reslove(response);
})
});
}
/**
* cyl add
* 用于判断父级菜单是否有子级菜单被选中
* 如果有,那么该父节菜单也需要选中
*/
function existsChildren(router,roles){
for(let i = 0; i < roles.length; i++){
for(let k = 0; k < router.children.length; k++){
if(router.children[k].children && router.children[k].children.length>0){
existsChildren(router.children[k],roles);
}else{
if(roles[i].path === router.children[k].path){
return true;
}
}
}
}
return false;
}
/**
* 判断是否有权限
* @param {*} router
* @param {*} roles
*/
function hasPerminssion(router, roles) {
if(router.children && router.children.length>0){
if(existsChildren(router,roles)){
return true;
}
}
// let status = false
let status = true
if (roles) {
for(let i = 0; i < roles.length; i++){
if(roles[i].path === router.path){
status = true;
break;
}
}
}
return status
}
/**
* 根据后台返回的路由权限
* 动态匹配路由
* @param {*} asyncRouterMap
* @param {*} roles
*/
function filterAsyncRouter(asyncRouterMap, roles) {
const newData = [];
// return routers
asyncRouterMap.forEach(ele => {
let obj = {};
if (hasPerminssion(ele, roles)) {
obj['path'] = ele.path;
obj['name'] = ele.name;
obj['meta'] = ele.meta || {}
obj['component'] = ele.component;
if (ele.children && ele.children.length > 0) {
obj['children'] = filterAsyncRouter(ele.children, roles)
} else {
obj['children'] = []
}
} else {
return false
}
newData.push(obj)
});
return newData
}
/**
* 根据路由和当前跳转key(路由路径)
* 获取第三级路由
* @param {*} router
* @param {*} key
*/
function filterTwoMenu(router, key) {
let data = [],
state = true;
router.forEach(e => {
if(e.path == key)
if(e.children) {
data = e.children
} else {
data = [];
}
})
return data;
}
const menu = {
state: {
routers: constantRouterMap,
addRouter: [],
threeMenu:[],
},
mutations:{
SET_ROUTERS: (state, routers) => {
state.addRouter = routers;
state.routers = constantRouterMap.concat(routers);
},
SET_THREEMENU: (state, routers) => {
state.threeMenu = routers;
}
},
actions: {
GetMenuRole({ commit, state},data) {
if (data === ''){
commit('SET_ROUTERS', []);
return
}
return new Promise((resolve, reject) => {
getMenuInfo().then(response => {
let accessedRouters = filterAsyncRouter(asyncRouterMap,response.data.list_data);
// commit('SET_ROUTERS', accessedRouters);
commit('SET_ROUTERS', accessedRouters);
resolve(accessedRouters);
})
})
},
GetTwoMenu({commit, state},data) {
return new Promise((reslove,reject) => {
const tmenus = filterTwoMenu(state.routers, data);
commit('SET_THREEMENU', tmenus);
reslove(tmenus);
})
}
},
}
export default menu