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) {