fix(登录): 解码重定向参数并优化路由认证检查
修复登录后重定向参数未解码的问题,确保正确跳转 同时改进路由守卫逻辑,支持通过元信息检查认证需求
Showing
2 changed files
with
14 additions
and
5 deletions
| 1 | /* | 1 | /* |
| 2 | * @Date: 2025-03-20 20:36:36 | 2 | * @Date: 2025-03-20 20:36:36 |
| 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com | 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com |
| 4 | - * @LastEditTime: 2025-06-13 10:03:08 | 4 | + * @LastEditTime: 2025-11-17 14:15:18 |
| 5 | * @FilePath: /mlaj/src/router/guards.js | 5 | * @FilePath: /mlaj/src/router/guards.js |
| 6 | * @Description: 路由守卫逻辑 | 6 | * @Description: 路由守卫逻辑 |
| 7 | */ | 7 | */ |
| ... | @@ -22,6 +22,10 @@ export const authRequiredRoutes = [ | ... | @@ -22,6 +22,10 @@ export const authRequiredRoutes = [ |
| 22 | path: '/activities/[^/]+/signup', | 22 | path: '/activities/[^/]+/signup', |
| 23 | regex: true, | 23 | regex: true, |
| 24 | }, | 24 | }, |
| 25 | + { | ||
| 26 | + path: '/checkin', | ||
| 27 | + exact: false, | ||
| 28 | + }, | ||
| 25 | ] | 29 | ] |
| 26 | 30 | ||
| 27 | // TAG: 微信授权检查 | 31 | // TAG: 微信授权检查 |
| ... | @@ -52,7 +56,8 @@ export const checkAuth = (to) => { | ... | @@ -52,7 +56,8 @@ export const checkAuth = (to) => { |
| 52 | const currentUser = JSON.parse(localStorage.getItem('currentUser')) | 56 | const currentUser = JSON.parse(localStorage.getItem('currentUser')) |
| 53 | 57 | ||
| 54 | // 检查当前路由是否需要认证 | 58 | // 检查当前路由是否需要认证 |
| 55 | - const needAuth = authRequiredRoutes.some((route) => { | 59 | + // 方式一:白名单匹配(兼容旧逻辑) |
| 60 | + const needAuthByList = authRequiredRoutes.some((route) => { | ||
| 56 | // 如果是正则匹配模式 | 61 | // 如果是正则匹配模式 |
| 57 | if (route.regex) { | 62 | if (route.regex) { |
| 58 | return new RegExp(`^${route.path}$`).test(to.path) | 63 | return new RegExp(`^${route.path}$`).test(to.path) |
| ... | @@ -64,6 +69,9 @@ export const checkAuth = (to) => { | ... | @@ -64,6 +69,9 @@ export const checkAuth = (to) => { |
| 64 | // 默认前缀匹配模式 | 69 | // 默认前缀匹配模式 |
| 65 | return to.path.startsWith(route.path) | 70 | return to.path.startsWith(route.path) |
| 66 | }) | 71 | }) |
| 72 | + // 方式二:读取路由元信息 requiresAuth(推荐) | ||
| 73 | + const needAuthByMeta = to.matched.some(record => record.meta && record.meta.requiresAuth === true) | ||
| 74 | + const needAuth = needAuthByList || needAuthByMeta | ||
| 67 | 75 | ||
| 68 | if (needAuth && !currentUser) { | 76 | if (needAuth && !currentUser) { |
| 69 | // 未登录时重定向到登录页面 | 77 | // 未登录时重定向到登录页面 | ... | ... |
| ... | @@ -297,9 +297,10 @@ const handleSubmit = async () => { | ... | @@ -297,9 +297,10 @@ const handleSubmit = async () => { |
| 297 | 297 | ||
| 298 | if (success) { | 298 | if (success) { |
| 299 | // 如果有重定向参数,登录成功后跳转到对应页面 | 299 | // 如果有重定向参数,登录成功后跳转到对应页面 |
| 300 | - const redirect = $route.query.redirect; | 300 | + // 说明:redirect 是经过 URL 编码的,需要先解码再跳转 |
| 301 | - router.push(redirect || "/"); | 301 | + const redirect_raw = $route.query.redirect; |
| 302 | - // router.push("/"); | 302 | + const redirect = redirect_raw ? decodeURIComponent(redirect_raw) : "/"; |
| 303 | + router.push(redirect); | ||
| 303 | } else { | 304 | } else { |
| 304 | error.value = "登录失败,请检查您的输入项"; | 305 | error.value = "登录失败,请检查您的输入项"; |
| 305 | } | 306 | } | ... | ... |
-
Please register or login to post a comment