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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { constantRoutes } from '@/router'
import { getRouters,getAu } from '@/api/menu'
import Layout from '@/layout/index'
import store from '@/store'
import resData from '@/views/data'
import { getUserId, getRoleId} from '@/utils/auth'
//import Cookies from 'js-cookie'
const permission = {
state: {
routes: [],
addRoutes: []
},
mutations: {
SET_ROUTES: (state, routes) => {
// let that=this
// let authList=['首页','系统管理','人群市场','应用标签管理','云镜中心', '用户库']
// let a=authList
// let checkList=[]
// res.data.map((item)=>{
// checkList.push(item.name)
// })
// a = a.filter(items => {
// if (!checkList.includes(items)) return items;
// })
// state.addRoutes = routes
// // state.routes = constantRoutes
// state.routes=constantRoutes.filter(items => {
// if (!a.includes(items.name)&&items.name!=='权限管理') return items;
// })
state.addRoutes = routes
// state.routes = constantRoutes
state.routes = constantRoutes
// if(getRoleId()==4){
// getAu(getUserId()).then(w => {
// let a=authList
// let checkList=[]
// res.data.map((item)=>{
// checkList.push(item.name)
// })
// a = a.filter(items => {
// if (!checkList.includes(items)) return items;
// })
// state.addRoutes = routes
// // state.routes = constantRoutes
// state.routes=constantRoutes.filter(items => {
// if (!a.includes(items.name)&&items.name!=='权限管理') return items;
// })
// })
// }else if(getRoleId()==2){
// state.addRoutes = routes
// let x=constantRoutes.filter(items => {
// if (!authList.includes(items.name)) return true;
// })
// for(var i=0;i<x.length;i++){
// if (x[i].name=='权限管理'){
// // for()
// x[i].children=x[i].children.filter(it=>{
// if(it.name=='安全配置'){
// return it
// }
// })
// console.log(x.children)
// }
// }
// state.routes=x
// console.log(state.routes)
// }else if(getRoleId()==3){
// state.addRoutes = routes
// let x=constantRoutes.filter(items => {
// if (!authList.includes(items.name)) return true;
// })
// for(var i=0;i<x.length;i++){
// if (x[i].name=='权限管理'){
// // for()
// x[i].children=x[i].children.filter(it=>{
// if(it.name=='操作日志'){
// return it
// }
// })
// console.log(x.children)
// }
// }
// state.routes=x
// console.log(state.routes)
// }else if(getRoleId()==1){
// state.addRoutes = routes
// let x=constantRoutes.filter(items => {
// if (!authList.includes(items.name)) return true;
// })
// for(var i=0;i<x.length;i++){
// if (x[i].name=='权限管理'){
// // for()
// x[i].children=x[i].children.filter(it=>{
// if(it.name=='账号管理'){
// return it
// }
// })
// console.log(x.children)
// }
// }
// state.routes=x
// }
}
},
actions: {
// 生成路由
GenerateRoutes({ commit }) {
return new Promise(resolve => {
// 向后端请求路由数据
let res=resData.resData
const accessedRoutes = filterAsyncRouter(res.data)
commit('SET_ROUTES', accessedRoutes)
resolve(accessedRoutes)
// getRouters().then(res => {
// const accessedRoutes = filterAsyncRouter(res.data)
// commit('SET_ROUTES', accessedRoutes)
// resolve(accessedRoutes)
// })
})
}
}
}
// 遍历后台传来的路由字符串,转换为组件对象
function filterAsyncRouter(asyncRouterMap) {
return asyncRouterMap.filter(route => {
if (route.component) {
// Layout组件特殊处理
if (route.component === 'Layout') {
route.component = Layout
} else {
route.component = loadView(route.component)
}
}
if (route.children != null && route.children && route.children.length) {
route.children = filterAsyncRouter(route.children)
}
return true
})
}
export const loadView = (view) => { // 路由懒加载
return () => import(`@/views/${view}`)
}
export default permission