diff --git a/src/pages/login.vue b/src/pages/login.vue index a002dd11..d57fbbf7 100644 --- a/src/pages/login.vue +++ b/src/pages/login.vue @@ -77,8 +77,8 @@ function login() { store.dispatch('auth/updateUserName', username) store.dispatch('auth/updateAvatar', avatar) - // 跳转到首页 - router.push('/') + // 跳转到首页或回原始页面 + router.push(store.state.auth.originalPath) }) .catch((error: any) => { // 登录失败,显示错误提示 diff --git a/src/router/index.ts b/src/router/index.ts index b5f687aa..c406c54c 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -162,6 +162,7 @@ router.beforeEach((to, from, next) => { const isAuthenticated = store.state.auth.token !== null if (to.meta.requiresAuth && !isAuthenticated) { + store.state.auth.originalPath = to.fullPath next('/login') } else { diff --git a/src/store/auth.ts b/src/store/auth.ts index e9a42783..352c4ab0 100644 --- a/src/store/auth.ts +++ b/src/store/auth.ts @@ -7,6 +7,7 @@ interface AuthState { superUser: boolean userName: string avatar: string + originalPath: string | null } // 定义根状态类型 @@ -23,6 +24,7 @@ const authModule: Module = { superUser: false, userName: '', avatar: '', + originalPath: null, }, mutations: { setToken(state, token: string) {