From 2f52706d0b8cd121a2ef05f2d73b359bf4a4781a Mon Sep 17 00:00:00 2001 From: hejianhao <15708179461@qq.com> Date: 星期四, 08 五月 2025 15:56:58 +0800 Subject: [PATCH] 路由拦截 --- laboratory/src/router/index.js | 96 ++++++++++++++++++++++++++---------------------- 1 files changed, 52 insertions(+), 44 deletions(-) diff --git a/laboratory/src/router/index.js b/laboratory/src/router/index.js index 7636c9e..1921fda 100644 --- a/laboratory/src/router/index.js +++ b/laboratory/src/router/index.js @@ -668,52 +668,60 @@ document.title = to.meta.title || '实验室流程'; // 登录验证 - console.log('to.path', to.path) - console.log('sessionStorage.getItem(token)', sessionStorage.getItem('token')); - - if (!sessionStorage.getItem('token') && to.path != "/") { - next('/') - } else if (sessionStorage.getItem('token') && to.path == "/") { - next('/system') - } else { - // 判断是否拥有要跳转菜单权限 - let menus = store.state.menus - if (to.meta.hasOwnProperty('privilege') && !menus.includes(to.meta.privilege)) { - return + // 排除登录页的校验 + if (to.path === "/login") { + if (sessionStorage.getItem('token')) { + next('/system'); // 已登录状态访问登录页时重定向到系统首页 + return; } - - // 设置标签列表 - if (!to.meta.hide || !to.meta.oneself) { - let tagList = JSON.parse(sessionStorage.getItem('tagList') || '[]') - // 判断是否存在 - let isExist = tagList.some(item => item.path === to.path) - if (!isExist) { - // 只保存必要的信息 - const tagInfo = { - path: to.path, - name: to.name, - meta: to.meta - } - tagList.push(tagInfo) - sessionStorage.setItem('tagList', JSON.stringify(tagList)) - store.commit('SET_TAGLIST', tagList) - } - } - - // 判断是否需要缓存 - if (to.meta.keepAlive) { - let keepAliveList = JSON.parse(sessionStorage.getItem('keepAliveList') || '[]') - // 判断是否已经缓存 - let isExist = keepAliveList.includes(to.name) - if (!isExist) { - keepAliveList.push(to.name) - sessionStorage.setItem('keepAliveList', JSON.stringify(keepAliveList)) - store.commit('SET_KEEPALIVELIST', keepAliveList) - } - } - - next() + next(); + return; } + + // 登录状态校验 + const isAuthenticated = sessionStorage.getItem('token'); + if (!isAuthenticated) { + next('/login'); // 未登录用户重定向到登录页 + return; + } + + // 判断是否拥有要跳转菜单权限 + let menus = store.state.menus + if (to.meta.hasOwnProperty('privilege') && !menus.includes(to.meta.privilege)) { + return + } + + // 设置标签列表 + if (!to.meta.hide || !to.meta.oneself) { + let tagList = JSON.parse(sessionStorage.getItem('tagList') || '[]') + // 判断是否存在 + let isExist = tagList.some(item => item.path === to.path) + if (!isExist) { + // 只保存必要的信息 + const tagInfo = { + path: to.path, + name: to.name, + meta: to.meta + } + tagList.push(tagInfo) + sessionStorage.setItem('tagList', JSON.stringify(tagList)) + store.commit('SET_TAGLIST', tagList) + } + } + + // 判断是否需要缓存 + if (to.meta.keepAlive) { + let keepAliveList = JSON.parse(sessionStorage.getItem('keepAliveList') || '[]') + // 判断是否已经缓存 + let isExist = keepAliveList.includes(to.name) + if (!isExist) { + keepAliveList.push(to.name) + sessionStorage.setItem('keepAliveList', JSON.stringify(keepAliveList)) + store.commit('SET_KEEPALIVELIST', keepAliveList) + } + } + + next() }); export default router; -- Gitblit v1.7.1