Files
sgxt_web/src/permission.js
2025-12-12 16:36:26 +08:00

77 lines
2.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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