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

164 lines
4.3 KiB
JavaScript
Raw Normal View History

2021-06-11 10:00:22 +08:00
import router from './router'
import store from './store'
import { ElMessage } from 'element-plus'
2022-06-22 10:45:41 +08:00
import { getPermission } from '@/utils/api'
2021-07-05 17:40:43 +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, '/entityDetail'] // 权限白名单
2021-06-11 10:00:22 +08:00
router.beforeEach(async (to, from, next) => {
if (to.path.indexOf('/login') == -1) {
sessionStorage.setItem(storageKey.tokenExpireCurrentPath, 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) {
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
2022-01-03 22:46:22 +08:00
if (!localStorage.getItem(storageKey.i18n)) {
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)
}
if (to.path) {
if (hasMenu(store.getters.menuList, to.path)) {
next()
2021-06-11 10:00:22 +08:00
} else {
ElMessage.error('No access') // TODO 国际化
2021-06-11 10:00:22 +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: tokenExpireCurrentPath } })
}
} else {
next()
}
2021-06-11 10:00:22 +08:00
} else {
2022-04-13 10:14:36 +08:00
next({ path: '/login', query: { redirect: to.fullPath } })
2021-06-11 10:00:22 +08:00
}
}
})
// menuList中是否包含route权限
export function hasMenu (menuList, route) {
return menuList.some(menu => {
if (menu.route === route) {
return true
} else {
if (menu.children) {
if (hasMenu(menu.children, route)) {
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)
}
// 用法 v-has="code" | v-has="[code...]" 任意匹配一个 | v-has:all="[code...]" 全匹配
export const hasPermission = {
beforeMount (el, binding) {
// 节点权限处理
const buttonCode = binding.value
const arg = binding.arg
if (buttonCode) {
if (buttonCode instanceof Array) {
let has = true
if (arg && arg === 'all') { // 全匹配
buttonCode.forEach(button => {
if (has) {
has = hasButton(store.getters.buttonList, button)
}
})
} else { // 任意匹配
has = buttonCode.some(button => {
return hasButton(store.getters.buttonList, button)
})
}
if (!has) {
el.parentNode.removeChild(el)
}
} else { // 单个匹配
if (!hasButton(store.getters.buttonList, buttonCode)) {
el.parentNode && el.parentNode.removeChild(el)
}
}
}
}
}
// 根据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
}
}
}
}