This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
cyber-narrator-cn-ui/src/permission.js

254 lines
7.5 KiB
JavaScript
Raw Normal View History

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'
import axios from 'axios'
import { storageKey } from '@/utils/constants'
import { loadI18n } from '@/i18n'
2021-06-11 10:00:22 +08:00
const loginWhiteList = ['/login', '/'] // 免登陆白名单
const permissionWhiteList = [...loginWhiteList] // 权限白名单
2021-06-11 10:00:22 +08:00
router.beforeEach(async (to, from, next) => {
2023-08-25 11:43:46 +08:00
if (to.path.indexOf('/login') === -1) {
sessionStorage.setItem(storageKey.tokenExpireCurrentPath, decodeURIComponent(to.fullPath))
}
2021-07-01 15:39:48 +08:00
// 加载iso-3166-2资源
2021-07-05 17:40:43 +08:00
loadGeoData()
// 加载baseUrl
if (!axios.defaults.baseURL) {
// eslint-disable-next-line no-undef
2022-06-22 10:45:41 +08:00
axios.defaults.baseURL = BASE_CONFIG.baseUrl
}
2022-01-03 22:46:22 +08:00
if (localStorage.getItem(storageKey.token)) {
// 加载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 {
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: []
2023-11-09 16:17:25 +08:00
}
handleRoutes(menuList, homeRoute.children)
router.addRoute(homeRoute)
next({ ...to, replace: true })
} else {
if (to.path) {
next()
2023-11-09 16:17:25 +08:00
}
}
2021-06-11 10:00:22 +08:00
}
} 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()
}
2021-06-11 10:00:22 +08:00
} else {
next({ path: '/login', query: { redirect: decodeURIComponent(to.fullPath) } })
2021-06-11 10:00:22 +08:00
}
}
})
// menuList中是否包含code
export function hasMenu (menuList, code) {
2021-06-11 10:00:22 +08:00
return menuList.some(menu => {
if (menu.code === code) {
2021-06-11 10:00:22 +08:00
return true
} else {
if (menu.children) {
if (hasMenu(menu.children, code)) {
2021-06-11 10:00:22 +08:00
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
}
2021-06-11 10:00:22 +08:00
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)
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 'tag':
return () => import('@/views/tag/Tag')
case 'createUserDefinedLibrary':
case 'editUserDefinedLibrary':
return () => import('@/views/tag/TagForm')
2023-11-09 16:17:25 +08:00
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')
2023-11-09 16:17:25 +08:00
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')
2023-11-09 16:17:25 +08:00
case 'I18N':
return () => import('@/views/administration/I18n')
2023-12-27 16:05:35 +08:00
case 'system':
return () => import('@/views/system/Index')
case 'plugin':
return () => import('@/views/system/Plugin')
2024-02-26 11:51:13 +08:00
case 'locationMap':
case 'traceTracking':
return () => import('@/views/location/Index')
2023-11-09 16:17:25 +08:00
default:
return null
}
}
export function handleRoutes (menus, routes) {
menus.forEach(menu => {
if (menu.route === '' && (!menu.children || menu.children.length < 0)) {
return false
}
// administration的路由使用了嵌套其他的是平铺
2023-12-29 17:06:14 +08:00
if (menu.pid === 0 && (menu.code === 'administration' || menu.code === 'system')) {
2023-11-09 16:17:25 +08:00
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)
}
}
})
}