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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
import router from './router'
import store from './store'
import { getPermission } from '@/utils/api'
import { loadGeoData } from '@/utils/tools'
import axios from 'axios'
import { storageKey } from '@/utils/constants'
import { loadI18n } from '@/i18n'
const loginWhiteList = ['/login', '/'] // 免登陆白名单
const permissionWhiteList = [...loginWhiteList] // 权限白名单
router.beforeEach(async (to, from, next) => {
if (to.path.indexOf('/login') === -1) {
sessionStorage.setItem(storageKey.tokenExpireCurrentPath, decodeURIComponent(to.fullPath))
}
// 加载iso-3166-2资源
loadGeoData()
// 加载baseUrl
if (!axios.defaults.baseURL) {
// eslint-disable-next-line no-undef
axios.defaults.baseURL = BASE_CONFIG.baseUrl
}
if (localStorage.getItem(storageKey.token)) {
// 加载i18n
await loadI18n()
// 加载权限
if (permissionWhiteList.indexOf(to.path) !== -1) {
next()
} else {
if (store.getters.menuList.length === 0) {
const { menuList, buttonList, roleList } = await getPermission()
store.commit('setMenuList', menuList)
store.commit('setButtonList', buttonList)
store.commit('setRoleList', roleList)
const homeRoute = {
path: '/',
name: 'home',
component: () => import('@/components/layout/Home'),
children: []
}
handleRoutes(menuList, homeRoute.children)
router.addRoute(homeRoute)
next({ ...to, replace: true })
} else {
if (to.path) {
next()
}
}
}
} else {
if (loginWhiteList.indexOf(to.path) !== -1) {
const tokenExpireCurrentPath = sessionStorage.getItem(storageKey.tokenExpireCurrentPath)
if (to.path === '/login' && tokenExpireCurrentPath) {
if (hasParam(to.fullPath, 'redirect')) {
next()
} else {
next({ path: '/login', query: { redirect: decodeURIComponent(tokenExpireCurrentPath) } })
}
} else {
next()
}
} else {
next({ path: '/login', query: { redirect: decodeURIComponent(to.fullPath) } })
}
}
})
// menuList中是否包含code
export function hasMenu (menuList, code) {
return menuList.some(menu => {
if (menu.code === code) {
return true
} else {
if (menu.children) {
if (hasMenu(menu.children, code)) {
return true
}
}
}
return false
})
}
export function hasParam (url, param) {
let hasParam = false
let tempArr = url.split('?')
// const query = {}
if (tempArr[1]) {
tempArr = tempArr[1].split('&')
tempArr.forEach(t => {
const kv = t.split('=')
if (kv[0] === param) {
hasParam = true
}
})
}
return hasParam
}
export function hasButton (buttonList, code) {
return buttonList.some(button => button === code)
}
// 根据code从menuList和buttonList中判断是否有权限
export function hasPermission (code) {
return hasButton(store.getters.buttonList, code) || hasMenu(store.getters.menuList, code)
}
// 根据orderNum排序
export function sortByOrderNum (menuList) {
const r = menuList.sort((a, b) => {
return a.orderNum - b.orderNum
})
r.forEach(menu => {
if (menu.children && getChildMenu(menu).length > 0) {
menu.children = menu.children.sort((a, b) => {
return a.orderNum - b.orderNum
})
}
})
return r
}
function getChildMenu (menu) {
return menu.children.filter(m => m.type === 1)
}
/* 获取第一个菜单 */
export function getWelcomeMenu (menu) {
for (let i = 0; i < menu.length; i++) {
const m = menu[i]
if (m.type === 1) {
if (m.children && getChildMenu(m).length > 0) {
return getWelcomeMenu(m.children)
} else {
return m
}
}
}
}
export function handleComponent (code) {
switch (code) {
case 'networkOverview':
case 'networkAppPerformance':
case 'dnsServiceInsights':
case 'linkMonitor':
return () => import('@/views/charts2/Panel')
case 'entity':
return () => import('@/views/entityExplorer/EntityExplorer')
case 'entityDetail':
return () => import('@/views/entityExplorer/EntityDetail')
case 'entityGraph':
return () => import('@/views/entityExplorer/EntityGraph')
case 'securityEvents':
case 'performanceEvents':
return () => import('@/views/detections/Index')
case 'detectionPolicy':
return () => import('@/views/detections/detectionPolicies/Index')
case 'createDetectionPolicy':
case 'editDetectionPolicy':
return () => import('@/views/detections/detectionPolicies/PolicyForm')
case 'report':
return () => import('@/views/report/Report')
case 'tag':
return () => import('@/views/tag/Tag')
case 'createTag':
case 'editTag':
return () => import('@/views/tag/TagForm')
case 'knowledgeBase':
return () => import('@/views/setting/KnowledgeBase')
case 'userDefinedLibrary':
return () => import('@/views/tag/Tag')
// case 'userDefinedLibrary':
// return () => import('@/views/setting/KnowledgeBaseUserDefinedList')
// case 'createUserDefinedLibrary':
// case 'editUserDefinedLibrary':
// return () => import('@/views/setting/KnowledgeBaseForm')
case 'administration':
return () => import('@/views/administration/Index')
case 'user':
return () => import('@/views/administration/User')
case 'role':
return () => import('@/views/administration/Roles')
case 'operationLog':
return () => import('@/views/administration/OperationLog')
case 'appearance':
return () => import('@/views/administration/Appearance')
case 'license':
return () => import('@/views/administration/License')
case 'I18N':
return () => import('@/views/administration/I18n')
case 'system':
return () => import('@/views/system/Index')
case 'plugin':
return () => import('@/views/system/Plugin')
case 'locationMap':
case 'traceTracking':
return () => import('@/views/location/Index')
case 'dataSource':
return () => import('@/views/setting/sources/Sources')
case 'createDataSource':
case 'editDataSource':
case 'viewDataSource':
return () => import('@/views/setting/sources/SourcesForm')
case 'entityIntegration':
return () => import('@/views/setting/entitySetting/EntitySetting')
case 'createEntityIntegration':
case 'editEntityIntegration':
case 'viewEntityIntegration':
return () => import('@/views/setting/entitySetting/EntitySettingForm')
default:
return null
}
}
export function handleRoutes (menus, routes) {
menus.forEach(menu => {
if (menu.route === '' && (!menu.children || menu.children.length < 0)) {
return false
}
// administration的路由使用了嵌套,其他的是平铺
if (menu.pid === 0 && (menu.code === 'administration' || menu.code === 'system')) {
const path = menu.route.replace('redirect:', '')
if (menu.children && menu.children.length > 0) {
const route = {
name: menu.name,
path,
redirect: menu.children[0].route,
component: handleComponent(menu.code),
children: []
}
menu.children.forEach(c => {
route.children.push({
name: c.name,
path: c.route,
component: handleComponent(c.code)
})
})
routes.push(route)
}
} else {
if (menu.route && menu.route.startsWith('redirect:')) {
const path = menu.route.replace('redirect:', '')
if (menu.children && menu.children.length > 0) {
routes.push({
name: menu.name,
path,
redirect: menu.children[0].route
})
handleRoutes(menu.children, routes)
}
} else {
routes.push({
name: menu.code,
path: menu.route,
component: handleComponent(menu.code)
})
}
if (menu.children && menu.children.length > 0) {
handleRoutes(menu.children, routes)
}
}
})
}
|