feat(登录): 添加安全跳转功能并启用登录验证
实现安全跳转函数以处理带参数的redirect场景,同时恢复路由守卫的登录验证逻辑
Showing
2 changed files
with
37 additions
and
20 deletions
| 1 | /* | 1 | /* |
| 2 | * @Date: 2025-10-30 10:29:15 | 2 | * @Date: 2025-10-30 10:29:15 |
| 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com | 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com |
| 4 | - * @LastEditTime: 2025-11-12 10:06:07 | 4 | + * @LastEditTime: 2025-11-12 15:48:17 |
| 5 | * @FilePath: /stdj_h5/src/router/index.js | 5 | * @FilePath: /stdj_h5/src/router/index.js |
| 6 | * @Description: 文件描述 | 6 | * @Description: 文件描述 |
| 7 | */ | 7 | */ |
| ... | @@ -104,26 +104,25 @@ router.beforeEach((to, from, next) => { | ... | @@ -104,26 +104,25 @@ router.beforeEach((to, from, next) => { |
| 104 | 104 | ||
| 105 | /** | 105 | /** |
| 106 | * 访问控制:除首页('/')与登录页外,其余页面均需登录 | 106 | * 访问控制:除首页('/')与登录页外,其余页面均需登录 |
| 107 | - * 说明:优先读取 Cookie 中的 token;若不存在则回退读取本地存储 token,避免重复登录。 | ||
| 108 | */ | 107 | */ |
| 109 | - // const token_cookie = Cookies.get('token-stdj') | 108 | + const token_cookie = Cookies.get('token-stdj') |
| 110 | - // const is_login = !!(token_cookie) | 109 | + const is_login = token_cookie !== undefined |
| 111 | 110 | ||
| 112 | - // // 白名单:无需登录校验的路径 | 111 | + // 白名单:无需登录校验的路径 |
| 113 | - // const white_list = ['/', '/login'] | 112 | + const white_list = ['/', '/jz_login'] |
| 114 | - // const need_auth = !white_list.includes(to.path) | 113 | + const need_auth = !white_list.includes(to.path) |
| 115 | 114 | ||
| 116 | - // if (need_auth && !is_login) { | 115 | + if (need_auth && !is_login) { |
| 117 | - // // 在重定向前关闭或重置上一跳的loading,避免计数残留 | 116 | + // 在重定向前关闭或重置上一跳的loading,避免计数残留 |
| 118 | - // try { | 117 | + try { |
| 119 | - // const loading = useLoadingStore() | 118 | + const loading = useLoadingStore() |
| 120 | - // loading.reset() | 119 | + loading.reset() |
| 121 | - // } catch (e) { | 120 | + } catch (e) { |
| 122 | - // void e | 121 | + void e |
| 123 | - // } | 122 | + } |
| 124 | - // next({ name: '登录', query: { redirect: to.fullPath } }) | 123 | + next({ name: '登录-戒子', query: { redirect: to.fullPath } }) |
| 125 | - // return | 124 | + return |
| 126 | - // } | 125 | + } |
| 127 | 126 | ||
| 128 | next() | 127 | next() |
| 129 | }) | 128 | }) | ... | ... |
| ... | @@ -69,6 +69,24 @@ const is_not_empty = function (v) { | ... | @@ -69,6 +69,24 @@ const is_not_empty = function (v) { |
| 69 | return String(v || '').trim().length > 0 | 69 | return String(v || '').trim().length > 0 |
| 70 | } | 70 | } |
| 71 | 71 | ||
| 72 | +const safe_navigate = function () { | ||
| 73 | + // 跳转目标处理:兼容 redirect 携带 query/hash 的场景 | ||
| 74 | + // 说明:当地址形如 /login?redirect=/masters?pid=3651943 时,使用字符串导航更安全; | ||
| 75 | + // 同时解析并保留查询与哈希,避免对象形式 path 丢失查询参数。 | ||
| 76 | + const redirect_raw = String(route.query.redirect || '').trim() | ||
| 77 | + let redirect_target = '/studentInfo' | ||
| 78 | + if (redirect_raw) { | ||
| 79 | + try { | ||
| 80 | + const url = new URL(redirect_raw, window.location.origin) | ||
| 81 | + redirect_target = url.pathname + url.search + url.hash | ||
| 82 | + } catch (err) { | ||
| 83 | + // 若不是标准URL(仅相对路径),直接使用或回退到默认页 | ||
| 84 | + redirect_target = redirect_raw.startsWith('/') ? redirect_raw : '/studentInfo' | ||
| 85 | + } | ||
| 86 | + } | ||
| 87 | + router.replace(redirect_target) | ||
| 88 | +} | ||
| 89 | + | ||
| 72 | /** | 90 | /** |
| 73 | * 身份证登录 | 91 | * 身份证登录 |
| 74 | * 说明:仅凭身份证号进行登录 | 92 | * 说明:仅凭身份证号进行登录 |
| ... | @@ -87,8 +105,8 @@ const on_login_by_id = async function () { | ... | @@ -87,8 +105,8 @@ const on_login_by_id = async function () { |
| 87 | Cookies.set('token-stdj', data.id, { expires: 1 }) | 105 | Cookies.set('token-stdj', data.id, { expires: 1 }) |
| 88 | showSuccessToast('登录成功') | 106 | showSuccessToast('登录成功') |
| 89 | setTimeout(() => { | 107 | setTimeout(() => { |
| 90 | - // 直接跳转到戒子信息页 | 108 | + // 登录成功后,根据 redirect 参数跳转 |
| 91 | - router.replace({ path: '/studentInfo' }) | 109 | + safe_navigate() |
| 92 | }, 1000) | 110 | }, 1000) |
| 93 | } | 111 | } |
| 94 | } catch (e) { | 112 | } catch (e) { | ... | ... |
-
Please register or login to post a comment