Files
sgxt_web/src/permission.js

77 lines
2.2 KiB
JavaScript
Raw Normal View History

2025-04-12 14:54:02 +08:00
import router from './router'
2025-10-09 21:33:58 +08:00
import Base64 from "base-64";
2025-04-12 14:54:02 +08:00
import store from './store'
import {
setItem,
getItem,
removeAllItem
} from "@/utils/storage";
2025-10-09 21:33:58 +08:00
import {
getCookie
} from "@/utils/cookie";
2025-04-12 14:54:02 +08:00
// 白名单
2025-10-09 21:33:58 +08:00
const whiteList = ['/login', '/oatuh_login', '/404', '/401', '/zeroTrust_login', '/focusExploration', '/clueVerification', '/deploymentApproval']
2025-04-12 14:54:02 +08:00
/**
* 路由前置守卫
* to 去哪里
* from 来自哪
* next 往下走
*/
2025-04-15 14:49:25 +08:00
router.beforeEach(async (to, from, next) => {
// 存在 token ,进入主页
// if (store.state.user.token) {
// 快捷访问
// console.log(store.getters.token);
2025-04-15 14:49:25 +08:00
if (store.getters.token) {
// console.log("路由1");
2025-12-05 21:36:34 +08:00
2025-04-15 14:49:25 +08:00
// 判断用户资料是否获取
// 若不存在用户信息,则需要获取用户信息
// 触发获取用户信息的 action并获取用户当前权限
2025-12-05 21:36:34 +08:00
store.commit('permission/setRouteReady', 1)
2025-04-15 14:49:25 +08:00
// 添加完动态路由之后,需要在进行一次主动跳转
2025-12-05 21:36:34 +08:00
const afterMenuList = getItem('menusPermission');
2025-04-15 14:49:25 +08:00
// 处理用户权限,筛选出需要添加的权限
2025-12-12 16:36:26 +08:00
// console.log(store.state.permission.routes);
2025-12-05 21:36:34 +08:00
if (store.state.permission.routes.length === 0) {
2025-04-15 14:49:25 +08:00
const filterRoutes = await store.dispatch('permission/filterRoutes', afterMenuList)
filterRoutes.forEach(item => {
router.addRoute(item)
})
2025-12-12 16:36:26 +08:00
// console.log("已添加动态路由");
2025-04-15 14:49:25 +08:00
next({
...to,
replace: true
})
} else {
2025-12-12 16:36:26 +08:00
// console.log('已存在路由');
2025-04-15 14:49:25 +08:00
next()
}
// 利用 addRoute 循环添加
} else {
// 没有token的情况下可以进入白名单
if (whiteList.indexOf(to.path) > -1) {
2025-12-12 16:36:26 +08:00
// console.log("路由2");
2025-04-15 14:49:25 +08:00
next()
} else {
2025-10-09 21:33:58 +08:00
const cookie = getCookie("clientKey");
if (cookie) {
2025-12-12 16:36:26 +08:00
// console.log("路由3");
2025-10-09 21:33:58 +08:00
next(`/zeroTrust_login`)
2025-04-15 14:49:25 +08:00
} else {
2025-12-12 16:36:26 +08:00
// console.log("路由4");
2025-10-09 21:33:58 +08:00
const isOatuh = getItem('isOatuh')
// 没有token的情况下可以进入白名单
if (isOatuh) {
2025-12-12 16:36:26 +08:00
// console.log("路由5");
2025-10-09 21:33:58 +08:00
const idEntityCard = getItem('idEntityCard')
next(`/oatuh_login?token=${Base64.encode(idEntityCard)}`)
} else {
next('/login')
}
2025-04-15 14:49:25 +08:00
}
}
}
})