Files
ba_web/src/permission.js

59 lines
1.7 KiB
JavaScript
Raw Normal View History

2025-09-22 09:01:41 +08:00
import router from './router'
2026-01-14 17:49:38 +08:00
import Base64 from "base-64";
2025-09-22 09:01:41 +08:00
import store from './store'
import {
getItem,
} from "@/utils/storage";
2026-01-14 17:49:38 +08:00
2025-09-22 09:01:41 +08:00
// 白名单
2026-01-14 17:49:38 +08:00
const whiteList = ['/login', '/oatuh_login', '/404', '/401', '/zeroTrust_login', '/focusExploration', '/clueVerification', '/deploymentApproval']
2025-09-22 09:01:41 +08:00
/**
* 路由前置守卫
* to 去哪里
* from 来自哪
* next 往下走
*/
2025-09-26 16:08:24 +08:00
router.beforeEach(async (to, from, next) => {
// 存在 token ,进入主页
// if (store.state.user.token) {
// 快捷访问
2026-01-14 17:49:38 +08:00
// console.log(store.getters.token);
2025-09-26 16:08:24 +08:00
if (store.getters.token) {
2026-01-14 17:49:38 +08:00
// console.log("路由1");
2025-09-26 16:08:24 +08:00
// 判断用户资料是否获取
// 若不存在用户信息,则需要获取用户信息
// 触发获取用户信息的 action并获取用户当前权限
2026-01-14 17:49:38 +08:00
store.commit('permission/setRouteReady', 1)
2025-09-26 16:08:24 +08:00
// 添加完动态路由之后,需要在进行一次主动跳转
2026-01-14 17:49:38 +08:00
const afterMenuList = getItem('menusPermission');
2025-09-26 16:08:24 +08:00
// 处理用户权限,筛选出需要添加的权限
2026-01-14 17:49:38 +08:00
// console.log(store.state.permission.routes);
2025-10-28 18:26:31 +08:00
if (store.state.permission.routes.length === 0) {
2025-09-26 16:08:24 +08:00
const filterRoutes = await store.dispatch('permission/filterRoutes', afterMenuList)
filterRoutes.forEach(item => {
router.addRoute(item)
})
2026-01-14 17:49:38 +08:00
// console.log("已添加动态路由");
2025-09-26 16:08:24 +08:00
next({
...to,
replace: true
})
} else {
2026-01-14 17:49:38 +08:00
// console.log('已存在路由');
2025-09-26 16:08:24 +08:00
next()
}
// 利用 addRoute 循环添加
} else {
// 没有token的情况下可以进入白名单
if (whiteList.indexOf(to.path) > -1) {
2026-01-14 17:49:38 +08:00
// console.log("路由2");
2025-09-26 16:08:24 +08:00
next()
2026-01-15 15:28:53 +08:00
} else {
// 路由不在白名单中,重定向到登录页面
next('/login')
2025-09-26 16:08:24 +08:00
}
}
})