2021-06-11 10:00:22 +08:00
|
|
|
|
import router from './router'
|
|
|
|
|
|
import store from './store'
|
2022-06-22 10:45:41 +08:00
|
|
|
|
import { getPermission } from '@/utils/api'
|
2022-10-09 18:47:09 +08:00
|
|
|
|
import { loadGeoData } from '@/utils/tools'
|
2021-06-22 21:19:04 +08:00
|
|
|
|
import axios from 'axios'
|
|
|
|
|
|
import { storageKey } from '@/utils/constants'
|
|
|
|
|
|
import { loadI18n } from '@/i18n'
|
2021-06-11 10:00:22 +08:00
|
|
|
|
|
2021-08-13 09:39:02 +08:00
|
|
|
|
const loginWhiteList = ['/login', '/'] // 免登陆白名单
|
2023-05-15 15:31:22 +08:00
|
|
|
|
const permissionWhiteList = [...loginWhiteList] // 权限白名单
|
2021-06-11 10:00:22 +08:00
|
|
|
|
|
2021-06-22 21:19:04 +08:00
|
|
|
|
router.beforeEach(async (to, from, next) => {
|
2023-08-25 11:43:46 +08:00
|
|
|
|
if (to.path.indexOf('/login') === -1) {
|
2022-09-09 15:17:50 +08:00
|
|
|
|
sessionStorage.setItem(storageKey.tokenExpireCurrentPath, decodeURIComponent(to.fullPath))
|
2022-05-18 17:04:27 +08:00
|
|
|
|
}
|
2021-07-01 15:39:48 +08:00
|
|
|
|
// 加载iso-3166-2资源
|
2021-07-05 17:40:43 +08:00
|
|
|
|
loadGeoData()
|
2021-06-22 21:19:04 +08:00
|
|
|
|
// 加载baseUrl
|
|
|
|
|
|
if (!axios.defaults.baseURL) {
|
2023-03-02 17:55:47 +08:00
|
|
|
|
// eslint-disable-next-line no-undef
|
2022-06-22 10:45:41 +08:00
|
|
|
|
axios.defaults.baseURL = BASE_CONFIG.baseUrl
|
2021-06-22 21:19:04 +08:00
|
|
|
|
}
|
2022-01-03 22:46:22 +08:00
|
|
|
|
if (localStorage.getItem(storageKey.token)) {
|
2021-06-22 21:19:04 +08:00
|
|
|
|
// 加载i18n
|
2023-08-09 18:32:52 +08:00
|
|
|
|
await loadI18n()
|
2021-07-01 15:39:48 +08:00
|
|
|
|
// 加载权限
|
2021-06-11 10:00:22 +08:00
|
|
|
|
if (permissionWhiteList.indexOf(to.path) !== -1) {
|
|
|
|
|
|
next()
|
|
|
|
|
|
} else {
|
2021-06-22 21:19:04 +08:00
|
|
|
|
if (store.getters.menuList.length === 0) {
|
|
|
|
|
|
const { menuList, buttonList, roleList } = await getPermission()
|
|
|
|
|
|
store.commit('setMenuList', menuList)
|
|
|
|
|
|
store.commit('setButtonList', buttonList)
|
|
|
|
|
|
store.commit('setRoleList', roleList)
|
2023-11-09 16:17:25 +08:00
|
|
|
|
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) {
|
2021-06-22 21:19:04 +08:00
|
|
|
|
next()
|
2023-11-09 16:17:25 +08:00
|
|
|
|
}
|
2021-06-22 21:19:04 +08:00
|
|
|
|
}
|
2021-06-11 10:00:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
if (loginWhiteList.indexOf(to.path) !== -1) {
|
2022-05-19 18:26:51 +08:00
|
|
|
|
const tokenExpireCurrentPath = sessionStorage.getItem(storageKey.tokenExpireCurrentPath)
|
|
|
|
|
|
if (to.path === '/login' && tokenExpireCurrentPath) {
|
|
|
|
|
|
if (hasParam(to.fullPath, 'redirect')) {
|
|
|
|
|
|
next()
|
|
|
|
|
|
} else {
|
2022-09-09 15:17:50 +08:00
|
|
|
|
next({ path: '/login', query: { redirect: decodeURIComponent(tokenExpireCurrentPath) } })
|
2022-05-19 18:26:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
next()
|
|
|
|
|
|
}
|
2021-06-11 10:00:22 +08:00
|
|
|
|
} else {
|
2022-09-09 15:17:50 +08:00
|
|
|
|
next({ path: '/login', query: { redirect: decodeURIComponent(to.fullPath) } })
|
2021-06-11 10:00:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
|
2023-11-10 11:43:11 +08:00
|
|
|
|
// menuList中是否包含code
|
|
|
|
|
|
export function hasMenu (menuList, code) {
|
2021-06-11 10:00:22 +08:00
|
|
|
|
return menuList.some(menu => {
|
2023-11-10 11:43:11 +08:00
|
|
|
|
if (menu.code === code) {
|
2021-06-11 10:00:22 +08:00
|
|
|
|
return true
|
|
|
|
|
|
} else {
|
|
|
|
|
|
if (menu.children) {
|
2023-11-10 11:43:11 +08:00
|
|
|
|
if (hasMenu(menu.children, code)) {
|
2021-06-11 10:00:22 +08:00
|
|
|
|
return true
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return false
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-05-19 18:26:51 +08:00
|
|
|
|
export function hasParam (url, param) {
|
|
|
|
|
|
let hasParam = false
|
|
|
|
|
|
let tempArr = url.split('?')
|
2023-03-02 17:55:47 +08:00
|
|
|
|
// const query = {}
|
2022-05-19 18:26:51 +08:00
|
|
|
|
if (tempArr[1]) {
|
|
|
|
|
|
tempArr = tempArr[1].split('&')
|
|
|
|
|
|
tempArr.forEach(t => {
|
|
|
|
|
|
const kv = t.split('=')
|
|
|
|
|
|
if (kv[0] === param) {
|
|
|
|
|
|
hasParam = true
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
return hasParam
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-06-11 10:00:22 +08:00
|
|
|
|
export function hasButton (buttonList, code) {
|
|
|
|
|
|
return buttonList.some(button => button === code)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-11-10 11:43:11 +08:00
|
|
|
|
// 根据code从menuList和buttonList中判断是否有权限
|
|
|
|
|
|
export function hasPermission (code) {
|
|
|
|
|
|
return hasButton(store.getters.buttonList, code) || hasMenu(store.getters.menuList, code)
|
2021-06-11 10:00:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 根据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
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-11-09 16:17:25 +08:00
|
|
|
|
|
|
|
|
|
|
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 'knowledgeBase':
|
|
|
|
|
|
return () => import('@/views/setting/KnowledgeBase')
|
2023-11-15 22:22:53 +08:00
|
|
|
|
case 'userDefinedLibrary':
|
|
|
|
|
|
return () => import('@/views/setting/KnowledgeBaseUserDefinedList')
|
2023-11-09 16:17:25 +08:00
|
|
|
|
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 'I18N':
|
|
|
|
|
|
return () => import('@/views/administration/I18n')
|
|
|
|
|
|
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') {
|
|
|
|
|
|
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)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|