Skip to content

Commit aaefb7f

Browse files
committed
feat: 优化登录后的页面重定向逻辑
- 在路由守卫中,将重定向参数改为完整的编码路径 - 在登录成功后,根据重定向参数跳转到原请求页面 - 优化了账户、邮箱和手机登录后的重定向逻辑 Closes #IC42TM Closes #1
1 parent 3b3b35e commit aaefb7f

File tree

5 files changed

+43
-21
lines changed

5 files changed

+43
-21
lines changed

src/router/guard.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export const setupRouterGuard = (router: Router) => {
110110
} catch (error: any) {
111111
// 过程中发生任何错误,都直接重置 Token,并重定向到登录页面
112112
await userStore.logoutCallBack()
113-
next(`/login?redirect=${to.path}`)
113+
next(`/login?redirect=${encodeURIComponent(to.fullPath)}`)
114114
}
115115
} else {
116116
next()
@@ -123,7 +123,7 @@ export const setupRouterGuard = (router: Router) => {
123123
next()
124124
} else {
125125
// 其他没有访问权限的页面将被重定向到登录页面
126-
next('/login')
126+
next(`/login?redirect=${encodeURIComponent(to.fullPath)}`)
127127
}
128128
}
129129

src/utils/http.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ http.interceptors.response.use(
100100
async onOk() {
101101
const userStore = useUserStore()
102102
await userStore.logoutCallBack()
103-
await router.replace('/login')
103+
const currentPath = router.currentRoute.value.fullPath
104+
await router.replace(`/login?redirect=${encodeURIComponent(currentPath)}`)
104105
},
105106
})
106107
} else {

src/views/login/components/account/index.vue

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,19 @@ const handleLogin = async () => {
124124
const { redirect, ...othersQuery } = router.currentRoute.value.query
125125
const { rememberMe } = loginConfig.value
126126
loginConfig.value.username = rememberMe ? form.username : ''
127-
await router.push({
128-
path: (redirect as string) || '/',
129-
query: {
130-
...othersQuery,
131-
},
132-
})
127+
128+
// 如果有重定向参数,解码并直接跳转到完整路径
129+
if (redirect) {
130+
const redirectPath = decodeURIComponent(redirect as string)
131+
await router.push(redirectPath)
132+
} else {
133+
await router.push({
134+
path: '/',
135+
query: {
136+
...othersQuery,
137+
},
138+
})
139+
}
133140
Message.success('欢迎使用')
134141
} catch (error) {
135142
console.error(error)

src/views/login/components/email/index.vue

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,19 @@ const handleLogin = async () => {
7272
await userStore.emailLogin(form)
7373
tabsStore.reset()
7474
const { redirect, ...othersQuery } = router.currentRoute.value.query
75-
await router.push({
76-
path: (redirect as string) || '/',
77-
query: {
78-
...othersQuery,
79-
},
80-
})
75+
76+
// 如果有重定向参数,解码并直接跳转到完整路径
77+
if (redirect) {
78+
const redirectPath = decodeURIComponent(redirect as string)
79+
await router.push(redirectPath)
80+
} else {
81+
await router.push({
82+
path: '/',
83+
query: {
84+
...othersQuery,
85+
},
86+
})
87+
}
8188
Message.success('欢迎使用')
8289
} catch (error) {
8390
form.captcha = ''

src/views/login/components/phone/index.vue

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,19 @@ const handleLogin = async () => {
7272
await userStore.phoneLogin(form)
7373
tabsStore.reset()
7474
const { redirect, ...othersQuery } = router.currentRoute.value.query
75-
await router.push({
76-
path: (redirect as string) || '/',
77-
query: {
78-
...othersQuery,
79-
},
80-
})
75+
76+
// 如果有重定向参数,解码并直接跳转到完整路径
77+
if (redirect) {
78+
const redirectPath = decodeURIComponent(redirect as string)
79+
await router.push(redirectPath)
80+
} else {
81+
await router.push({
82+
path: '/',
83+
query: {
84+
...othersQuery,
85+
},
86+
})
87+
}
8188
Message.success('欢迎使用')
8289
} catch (error) {
8390
form.captcha = ''

0 commit comments

Comments
 (0)