fix: 权限修改
This commit is contained in:
parent
cd8148f65a
commit
255f707e0c
@ -1,24 +1,25 @@
|
|||||||
import store from '@/store'; // 导入 Vuex store 实例
|
import store from '@/store';
|
||||||
import router from '@/router'; // 导入 Vue Router 实例
|
import router from '@/router';
|
||||||
import { notification } from 'ant-design-vue'; // 导入 ant-design-vue 的 notification 组件
|
import { notification } from 'ant-design-vue';
|
||||||
import NProgress from 'nprogress'; // 导入进度条组件
|
import NProgress from 'nprogress';
|
||||||
import 'nprogress/nprogress.css'; // 导入进度条样式
|
import 'nprogress/nprogress.css';
|
||||||
NProgress.configure({ showSpinner: false }); // 配置进度条,隐藏加载动画
|
|
||||||
|
|
||||||
const loginPath = '/login'; // 登录路径
|
NProgress.configure({ showSpinner: false });
|
||||||
const defaultRoutePath = '/race/list'; // 默认路由路径
|
|
||||||
const whiteList = ['Login']; // 白名单,无需登录即可访问的页面
|
const loginPath = '/login';
|
||||||
|
const defaultRoutePath = '/race/list';
|
||||||
|
const whiteList = ['Login', 'Index']; // 白名单保持不变
|
||||||
|
|
||||||
router.beforeEach(async(to, from, next) => {
|
router.beforeEach(async(to, from, next) => {
|
||||||
NProgress.start(); // 开始进度条
|
NProgress.start();
|
||||||
|
const tokens = window.localStorage.getItem("token");
|
||||||
|
|
||||||
const tokens = window.localStorage.getItem("token"); // 使用相同的键来获取token
|
|
||||||
/* 未登录情况下的路由拦截 */
|
/* 未登录情况下的路由拦截 */
|
||||||
if (!tokens) {
|
if (!tokens) {
|
||||||
// alert(tokens)
|
// 修改判断逻辑,同时检查路由名称和路径
|
||||||
if (whiteList.includes(to.name)) { // 如果在白名单中,直接放行
|
if (whiteList.includes(to.name) || to.path === '/Index') {
|
||||||
next();
|
next();
|
||||||
} else { // 否则重定向至登录页面
|
} else {
|
||||||
next({
|
next({
|
||||||
path: loginPath,
|
path: loginPath,
|
||||||
query: {
|
query: {
|
||||||
@ -30,69 +31,59 @@ router.beforeEach(async(to, from, next) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* 已登录情况下访问登录界面的处理 */
|
/* 已登录情况下访问登录界面的处理 */
|
||||||
if (to.path === loginPath) { // 如果已登录且访问登录页面,则重定向至默认页面
|
if (to.path === loginPath) {
|
||||||
next(defaultRoutePath);
|
next(defaultRoutePath);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 已获取用户信息的情况 */
|
/* 已获取用户信息的情况 */
|
||||||
if (store.getters.permissions!=null && store.getters.permissions!=="") { // 如果已获取用户权限信息,则直接放行
|
if (store.getters.permissions != null && store.getters.permissions !== "") {
|
||||||
next();
|
next();
|
||||||
NProgress.done(); // 结束进度条
|
NProgress.done();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 未获取用户信息的情况 */
|
/* 未获取用户信息的情况 */
|
||||||
try {
|
try {
|
||||||
await store.dispatch('initUser'); // 异步获取用户信息
|
await store.dispatch('initUser');
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
store.commit('logout'); // 获取失败时执行退出登录操作
|
store.commit('logout');
|
||||||
next({ path: loginPath, query: { redirect: to.fullPath } }); // 重定向至登录页面
|
next({ path: loginPath, query: { redirect: to.fullPath } });
|
||||||
notification.error({
|
notification.error({
|
||||||
message: '错误',
|
message: '错误',
|
||||||
description: '请求用户信息失败,请重试',
|
description: '请求用户信息失败,请重试',
|
||||||
}); // 显示错误通知
|
});
|
||||||
NProgress.done(); // 结束进度条
|
NProgress.done();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 成功获取用户信息的情况 */
|
/* 成功获取用户信息的情况 */
|
||||||
// 检查路由权限
|
if (!checkAccess(to)) {
|
||||||
if (!checkAccess(to)) { // 如果当前路由无权限访问,则重定向至默认页面
|
|
||||||
next({ path: defaultRoutePath, replace: true });
|
next({ path: defaultRoutePath, replace: true });
|
||||||
NProgress.done(); // 结束进度条
|
NProgress.done();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const redirect = decodeURIComponent(from.query.redirect || to.path); // 获取重定向地址
|
|
||||||
// 如果重定向地址与当前路径相同,则替换历史记录
|
const redirect = decodeURIComponent(from.query.redirect || to.path);
|
||||||
if (redirect === to.path) {
|
if (redirect === to.path) {
|
||||||
next({...to, replace: true });
|
next({...to, replace: true });
|
||||||
} else { // 否则正常重定向
|
} else {
|
||||||
next(redirect);
|
next(redirect);
|
||||||
}
|
}
|
||||||
NProgress.done(); // 结束进度条
|
NProgress.done();
|
||||||
});
|
});
|
||||||
|
|
||||||
router.afterEach(() => {
|
router.afterEach(() => {
|
||||||
NProgress.done(); // 结束进度条
|
NProgress.done();
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
|
||||||
* 判断当前路由是否具有权限访问
|
|
||||||
* @param {Route} route 路由对象
|
|
||||||
* @returns {boolean} 是否具有权限
|
|
||||||
*/
|
|
||||||
|
|
||||||
function checkAccess(route) {
|
function checkAccess(route) {
|
||||||
|
const userPrivileges = store.state.user.userPrivileges == null ? 1 : store.state.user.userPrivileges;
|
||||||
const userPrivileges = store.state.user.userPrivileges==null ? 1 : store.state.user.userPrivileges; // 获取用户权限信息
|
const requiredPrivileges = route.meta.auth;
|
||||||
const requiredPrivileges = route.meta.auth; // 获取当前路由所需权限信息
|
console.log(userPrivileges);
|
||||||
console.log(userPrivileges)
|
|
||||||
// 如果路由未设置权限要求,则默认放行
|
if (!requiredPrivileges) {
|
||||||
if (!requiredPrivileges) {
|
return true;
|
||||||
return true;
|
}
|
||||||
}
|
return requiredPrivileges.includes(userPrivileges);
|
||||||
|
}
|
||||||
// 检查用户的权限是否包含在所需权限中
|
|
||||||
return requiredPrivileges.includes(userPrivileges);
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user