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'
|
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', '/'] // 免登陆白名单
|
2022-01-18 23:12:03 +08:00
|
|
|
const permissionWhiteList = [...loginWhiteList, '/entityDetail'] // 权限白名单
|
2021-06-11 10:00:22 +08:00
|
|
|
|
2021-06-22 21:19:04 +08:00
|
|
|
router.beforeEach(async (to, from, next) => {
|
2022-05-18 17:04:27 +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) {
|
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
|
2022-01-03 22:46:22 +08:00
|
|
|
if (!localStorage.getItem(storageKey.i18n)) {
|
2021-06-22 21:19:04 +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)
|
|
|
|
|
}
|
|
|
|
|
if (to.path) {
|
2023-02-24 18:51:17 +08:00
|
|
|
next()
|
|
|
|
|
/* if (hasMenu(store.getters.menuList, to.path)) {
|
2021-06-22 21:19:04 +08:00
|
|
|
next()
|
2021-06-11 10:00:22 +08:00
|
|
|
} else {
|
2021-06-22 21:19:04 +08:00
|
|
|
ElMessage.error('No access') // TODO 国际化
|
2023-02-24 18:51:17 +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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-19 18:26:51 +08:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|