hookehuyr

feat(登录): 添加安全跳转功能并启用登录验证

实现安全跳转函数以处理带参数的redirect场景,同时恢复路由守卫的登录验证逻辑
/*
* @Date: 2025-10-30 10:29:15
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2025-11-12 10:06:07
* @LastEditTime: 2025-11-12 15:48:17
* @FilePath: /stdj_h5/src/router/index.js
* @Description: 文件描述
*/
......@@ -104,26 +104,25 @@ router.beforeEach((to, from, next) => {
/**
* 访问控制:除首页('/')与登录页外,其余页面均需登录
* 说明:优先读取 Cookie 中的 token;若不存在则回退读取本地存储 token,避免重复登录。
*/
// const token_cookie = Cookies.get('token-stdj')
// const is_login = !!(token_cookie)
const token_cookie = Cookies.get('token-stdj')
const is_login = token_cookie !== undefined
// // 白名单:无需登录校验的路径
// const white_list = ['/', '/login']
// const need_auth = !white_list.includes(to.path)
// 白名单:无需登录校验的路径
const white_list = ['/', '/jz_login']
const need_auth = !white_list.includes(to.path)
// if (need_auth && !is_login) {
// // 在重定向前关闭或重置上一跳的loading,避免计数残留
// try {
// const loading = useLoadingStore()
// loading.reset()
// } catch (e) {
// void e
// }
// next({ name: '登录', query: { redirect: to.fullPath } })
// return
// }
if (need_auth && !is_login) {
// 在重定向前关闭或重置上一跳的loading,避免计数残留
try {
const loading = useLoadingStore()
loading.reset()
} catch (e) {
void e
}
next({ name: '登录-戒子', query: { redirect: to.fullPath } })
return
}
next()
})
......
......@@ -69,6 +69,24 @@ const is_not_empty = function (v) {
return String(v || '').trim().length > 0
}
const safe_navigate = function () {
// 跳转目标处理:兼容 redirect 携带 query/hash 的场景
// 说明:当地址形如 /login?redirect=/masters?pid=3651943 时,使用字符串导航更安全;
// 同时解析并保留查询与哈希,避免对象形式 path 丢失查询参数。
const redirect_raw = String(route.query.redirect || '').trim()
let redirect_target = '/studentInfo'
if (redirect_raw) {
try {
const url = new URL(redirect_raw, window.location.origin)
redirect_target = url.pathname + url.search + url.hash
} catch (err) {
// 若不是标准URL(仅相对路径),直接使用或回退到默认页
redirect_target = redirect_raw.startsWith('/') ? redirect_raw : '/studentInfo'
}
}
router.replace(redirect_target)
}
/**
* 身份证登录
* 说明:仅凭身份证号进行登录
......@@ -87,8 +105,8 @@ const on_login_by_id = async function () {
Cookies.set('token-stdj', data.id, { expires: 1 })
showSuccessToast('登录成功')
setTimeout(() => {
// 直接跳转到戒子信息页
router.replace({ path: '/studentInfo' })
// 登录成功后,根据 redirect 参数跳转
safe_navigate()
}, 1000)
}
} catch (e) {
......