diff --git a/src/router/index.js b/src/router/index.js index 6b5e4c4..cfd5a5e 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -59,40 +59,50 @@ const router = new Router({ routes: globalRoutes.concat(mainRoutes) }) -router.beforeEach((to, from, next) => {console.log(Vue.prototype.$store) - // 添加动态(菜单)路由 - // 1. 已经添加 or 全局路由, 直接访问 - // 2. 获取菜单列表, 添加并保存本地存储 +router.beforeEach((to, from, next) => { + const token = localStorage.getItem('token'); + const isLogin = to.name === 'login'; // 判断是否是登录页 + + // 保存原始路径(仅在未登录状态下保存) + if (!token && !isLogin) { + console.log('保存重定向路径:', to.fullPath); + localStorage.setItem('redirectPath', to.fullPath); + } + + // 动态路由逻辑处理 if (router.options.isAddDynamicMenuRoutes || fnCurrentRouteType(to, globalRoutes) === 'global') { - next() + next(); } else { - let menuType = localStorage.getItem('menuType') http({ url: http.adornUrl('/sys/menu/nav'), method: 'get', params: { - 'l': i18n.locale, //i18n.locale + l: i18n.locale, // 当前语言 menuType: 'pc' } - }).then(({data}) => { - if (data && data.code === 0) { - fnAddDynamicMenuRoutes(data.menuList) - router.options.isAddDynamicMenuRoutes = true - sessionStorage.setItem('menuList', JSON.stringify(data.menuList || '[]')) - sessionStorage.setItem('permissions', JSON.stringify(data.permissions || '[]')) - sessionStorage.setItem('userConfig', JSON.stringify(data.userConfig || '[]')) - next({ ...to, replace: true }) - } else { - sessionStorage.setItem('menuList', '[]') - sessionStorage.setItem('permissions', '[]') - next() - } - }).catch((e) => { - console.log(`%c${e} 请求菜单列表和权限失败,跳转至登录页!!`, 'color:blue') - router.push({ name: 'login' }) }) + .then(({ data }) => { + if (data && data.code === 0) { + fnAddDynamicMenuRoutes(data.menuList); // 添加动态菜单路由 + router.options.isAddDynamicMenuRoutes = true; + sessionStorage.setItem('menuList', JSON.stringify(data.menuList || '[]')); + sessionStorage.setItem('permissions', JSON.stringify(data.permissions || '[]')); + sessionStorage.setItem('userConfig', JSON.stringify(data.userConfig || '[]')); + next({ ...to, replace: true }); + } else { + console.error('菜单数据获取失败:', data.msg); + sessionStorage.setItem('menuList', '[]'); + sessionStorage.setItem('permissions', '[]'); + next(); + } + }) + .catch((e) => { + console.error(`%c${e} 请求菜单列表和权限失败,跳转至登录页!!`, 'color:blue'); + router.push({ name: 'login' }); // 跳转到登录页 + }); } -}) +}); + /** * 判断当前路由类型, global: 全局路由, main: 主入口路由 diff --git a/src/views/common/login.vue b/src/views/common/login.vue index 04149e7..cd8a8ef 100644 --- a/src/views/common/login.vue +++ b/src/views/common/login.vue @@ -71,8 +71,20 @@ import { getUUID } from '@/utils' } } }, + mounted() { + // 仅当 redirectPath 为空时设置 + const redirectPath = this.$route.query.redirect || '/home'; + if (!localStorage.getItem('redirectPath')) { + localStorage.setItem('redirectPath', redirectPath); + } + }, created () { this.userName(); + // 如果 redirectPath 已经存在,不要覆盖它 + const currentPath = localStorage.getItem('redirectPath'); + if (!currentPath) { + localStorage.setItem('redirectPath', this.$route.fullPath); + } }, methods: { // 获取上次登陆的用户名 @@ -93,17 +105,13 @@ import { getUUID } from '@/utils' }) }).then(({data}) => { if (data && data.code === 0) { + console.log('跳转前路径:', localStorage.getItem('redirectPath')); // 检查路径是否正确 this.$cookie.set('token', data.token) - - const prevLink = document.referrer; - - console.log('prevLink',prevLink) - // if($.trim(prevLink)==='') { - this.$router.replace({name: 'home'}) - // } - // if(prevLink.indexOf('www.example.com')===-1){ //来自其它站点 - // location.href = 'www.example.com/index.html'; - // } + // 动态跳转逻辑 + const redirectPath = localStorage.getItem('redirectPath') || '/home'; + console.log('Redirecting to:', redirectPath); // 调试输出 + this.$router.replace(redirectPath); // 根据保存的路径跳转 + // this.$router.replace({name: 'home'}) this.$i18n.locale=data.language localStorage.setItem('locale', data.language) localStorage.setItem('refresh', "0") diff --git a/src/views/modules/auth/authInquiry.vue b/src/views/modules/auth/authInquiry.vue index 0a0dcae..e0d7f3d 100644 --- a/src/views/modules/auth/authInquiry.vue +++ b/src/views/modules/auth/authInquiry.vue @@ -312,6 +312,13 @@ import {updateAuthHist} from "../../../api/auth/auth"; import OssComponents from "../oss/ossComponents.vue"; export default { + beforeRouteEnter(to, from, next) { + next((vm) => { + // 跳转后清除 redirectPath + localStorage.removeItem('redirectPath'); + console.log('redirectPath 已清除'); + }); + }, computed: { statusDisplay() { return this.inquiryInfoData.status === 'C' ? '草稿' : (this.inquiryInfoData.status === 'B' ? '已下达' : '已报价') diff --git a/src/views/modules/auth/authQuote.vue b/src/views/modules/auth/authQuote.vue index 59f3a0d..c0baa7a 100644 --- a/src/views/modules/auth/authQuote.vue +++ b/src/views/modules/auth/authQuote.vue @@ -216,6 +216,13 @@ import TpCost from "../quote/detail/tpCost.vue"; import QuoteDetail from "../quote/detail/quoteDetail.vue"; export default { + beforeRouteEnter(to, from, next) { + next((vm) => { + // 跳转后清除 redirectPath + localStorage.removeItem('redirectPath'); + console.log('redirectPath 已清除'); + }); + }, computed: { // quotationInformationFlag () { // return (value) => {