From 5914dd9b25f6c76eed5cbdbd2d3600102f887c04 Mon Sep 17 00:00:00 2001 From: v-lijf43 <3067418132@qq.com> Date: Sat, 27 Apr 2024 20:12:58 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E5=BD=93=E5=89=8D=E9=A1=B5=E6=89=93?= =?UTF-8?q?=E5=BC=80=EF=BC=8C=E8=87=AA=E5=8A=A8=E7=99=BB=E5=BD=95=E4=B9=8B?= =?UTF-8?q?=E5=90=8E=E9=87=8D=E6=96=B0=E5=9B=9E=E5=88=B0=E5=BD=93=E5=89=8D?= =?UTF-8?q?=E9=A1=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ruoyi-ui/src/router/index.js | 31 +++++++++++++------------------ ruoyi-ui/src/utils/request.js | 5 +++++ ruoyi-ui/src/views/login.vue | 14 ++++++++++++-- 3 files changed, 30 insertions(+), 20 deletions(-) diff --git a/ruoyi-ui/src/router/index.js b/ruoyi-ui/src/router/index.js index fa56376..fec974b 100644 --- a/ruoyi-ui/src/router/index.js +++ b/ruoyi-ui/src/router/index.js @@ -1,5 +1,6 @@ import Vue from 'vue' import Router from 'vue-router' +import store from '@/store' Vue.use(Router) @@ -252,21 +253,15 @@ export default new Router({ routes: constantRoutes }) - -//使用钩子函数对路由进行权限跳转 -// Router.beforeEach((to, from, next) => { -// let accessToken = getToken(); -// if (to.matched.some(record => record.meta.requireAuth)) { // 判断该路由是否需要登录权限 -// if (!accessToken) { // 判断当前的token是否存在 -// next({ -// name: 'login', -// query: { redirect: to.fullPath } //将跳转的路由path作为参数,登录成功后跳转到该路由 -// }) -// } else { -// next() -// } -// } else { -// next() -// } -// } -// ) \ No newline at end of file +new Router().beforeEach((to, from, next) => { + const token = store.state.token // 假设你有一个名为token的Vuex状态 + if (token) { + // 如果token存在,继续路由导航 + next() + } else { + // 如果token不存在,跳转到登录页面 + // 在登录页面,保存当前页面的路径以便登录后返回 + localStorage.setItem('intendedRoute', to.fullPath) + next({ path: '/' }) + } +}) \ No newline at end of file diff --git a/ruoyi-ui/src/utils/request.js b/ruoyi-ui/src/utils/request.js index ac717f0..02bac9d 100644 --- a/ruoyi-ui/src/utils/request.js +++ b/ruoyi-ui/src/utils/request.js @@ -89,6 +89,11 @@ service.interceptors.response.use(res => { isRelogin.show = false; }); } + // 保存当前页面的路径,以便登录后重定向 + localStorage.setItem('intendedRoute', router.currentRoute.fullPath); + // Token过期,清除token和用户信息 + store.commit('clearToken'); + store.commit('clearUser'); return Promise.reject('无效的会话,或者会话已过期,请重新登录。') } else if (code === 500) { Message({ message: msg, type: 'error' }) diff --git a/ruoyi-ui/src/views/login.vue b/ruoyi-ui/src/views/login.vue index 7b00e86..34c551d 100644 --- a/ruoyi-ui/src/views/login.vue +++ b/ruoyi-ui/src/views/login.vue @@ -131,11 +131,21 @@ export default { Cookies.remove('rememberMe'); } this.$store.dispatch("Login", this.loginForm).then(() => { - var redirect = decodeURIComponent(this.$route.query.redirect || '/'); - this.$router.push({ path: redirect })// 获取登录成功后要跳转的路由。decodeURIComponent函数编码的 URI 进行解码 + // var redirect = decodeURIComponent(this.$route.query.redirect || '/'); + // this.$router.push({ path: redirect })// 获取登录成功后要跳转的路由。decodeURIComponent函数编码的 URI 进行解码 // this.$router.push({ // path: this.redirect || "/" // }).catch(() => { }); + // 登录成功后,返回用户原本想要访问的页面 + const intendedRoute = localStorage.getItem('intendedRoute') + if (intendedRoute) { + this.$router.push(intendedRoute) + localStorage.removeItem('intendedRoute') + } else { + this.$router.push({ + path: this.redirect || "/" + }).catch(() => { }); + } }).catch(() => { this.loading = false; if (this.captchaEnabled) {