hookehuyr

fix(登录): 解码重定向参数并优化路由认证检查

修复登录后重定向参数未解码的问题,确保正确跳转
同时改进路由守卫逻辑,支持通过元信息检查认证需求
/*
* @Date: 2025-03-20 20:36:36
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2025-06-13 10:03:08
* @LastEditTime: 2025-11-17 14:15:18
* @FilePath: /mlaj/src/router/guards.js
* @Description: 路由守卫逻辑
*/
......@@ -22,6 +22,10 @@ export const authRequiredRoutes = [
path: '/activities/[^/]+/signup',
regex: true,
},
{
path: '/checkin',
exact: false,
},
]
// TAG: 微信授权检查
......@@ -52,7 +56,8 @@ export const checkAuth = (to) => {
const currentUser = JSON.parse(localStorage.getItem('currentUser'))
// 检查当前路由是否需要认证
const needAuth = authRequiredRoutes.some((route) => {
// 方式一:白名单匹配(兼容旧逻辑)
const needAuthByList = authRequiredRoutes.some((route) => {
// 如果是正则匹配模式
if (route.regex) {
return new RegExp(`^${route.path}$`).test(to.path)
......@@ -64,6 +69,9 @@ export const checkAuth = (to) => {
// 默认前缀匹配模式
return to.path.startsWith(route.path)
})
// 方式二:读取路由元信息 requiresAuth(推荐)
const needAuthByMeta = to.matched.some(record => record.meta && record.meta.requiresAuth === true)
const needAuth = needAuthByList || needAuthByMeta
if (needAuth && !currentUser) {
// 未登录时重定向到登录页面
......
......@@ -297,9 +297,10 @@ const handleSubmit = async () => {
if (success) {
// 如果有重定向参数,登录成功后跳转到对应页面
const redirect = $route.query.redirect;
router.push(redirect || "/");
// router.push("/");
// 说明:redirect 是经过 URL 编码的,需要先解码再跳转
const redirect_raw = $route.query.redirect;
const redirect = redirect_raw ? decodeURIComponent(redirect_raw) : "/";
router.push(redirect);
} else {
error.value = "登录失败,请检查您的输入项";
}
......