fix: 修复未加入家庭用户重复跳转Welcome页面的问题
修改Dashboard页面和请求拦截器中的跳转逻辑,增加当前路由检查 更新silentAuth函数支持skipRedirect参数,避免在Welcome页面重复跳转
Showing
3 changed files
with
58 additions
and
32 deletions
| ... | @@ -363,12 +363,22 @@ useLoad(async () => { | ... | @@ -363,12 +363,22 @@ useLoad(async () => { |
| 363 | // 检查用户是否已加入家庭 | 363 | // 检查用户是否已加入家庭 |
| 364 | const hasFamily = await checkUserHasFamily() | 364 | const hasFamily = await checkUserHasFamily() |
| 365 | 365 | ||
| 366 | - // 如果用户没有加入家庭,跳转到欢迎页面 | 366 | + // 如果用户没有加入家庭,检查当前页面是否已经是Welcome页面 |
| 367 | if (!hasFamily) { | 367 | if (!hasFamily) { |
| 368 | - console.warn('用户未加入家庭,跳转到欢迎页面'); | 368 | + // 获取当前页面路由 |
| 369 | - await Taro.reLaunch({ | 369 | + const pages = Taro.getCurrentPages(); |
| 370 | - url: '/pages/Welcome/index' | 370 | + const currentPage = pages[pages.length - 1]; |
| 371 | - }) | 371 | + const currentRoute = currentPage?.route || ''; |
| 372 | + | ||
| 373 | + // 只有当前页面不是Welcome页面时才跳转,避免重复跳转 | ||
| 374 | + if (currentRoute !== 'pages/Welcome/index') { | ||
| 375 | + console.warn('用户未加入家庭,跳转到欢迎页面'); | ||
| 376 | + await Taro.reLaunch({ | ||
| 377 | + url: '/pages/Welcome/index' | ||
| 378 | + }) | ||
| 379 | + } else { | ||
| 380 | + console.log('当前已在Welcome页面,跳过跳转'); | ||
| 381 | + } | ||
| 372 | return | 382 | return |
| 373 | } | 383 | } |
| 374 | }, | 384 | }, | ... | ... |
| 1 | /* | 1 | /* |
| 2 | * @Date: 2025-01-25 10:00:00 | 2 | * @Date: 2025-01-25 10:00:00 |
| 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com | 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com |
| 4 | - * @LastEditTime: 2025-09-12 21:32:12 | 4 | + * @LastEditTime: 2025-09-12 21:49:10 |
| 5 | * @FilePath: /lls_program/src/utils/authRedirect.js | 5 | * @FilePath: /lls_program/src/utils/authRedirect.js |
| 6 | * @Description: 授权重定向处理工具函数 | 6 | * @Description: 授权重定向处理工具函数 |
| 7 | */ | 7 | */ |
| ... | @@ -180,19 +180,19 @@ export const handleSharePageAuth = async (options, callback) => { | ... | @@ -180,19 +180,19 @@ export const handleSharePageAuth = async (options, callback) => { |
| 180 | try { | 180 | try { |
| 181 | // 使用静默授权 | 181 | // 使用静默授权 |
| 182 | await silentAuth( | 182 | await silentAuth( |
| 183 | - () => { | 183 | + () => { |
| 184 | - // 静默授权成功 | 184 | + // 静默授权成功 |
| 185 | - if (callback && typeof callback === 'function') { | 185 | + if (callback && typeof callback === 'function') { |
| 186 | - callback() | 186 | + callback() |
| 187 | - } | 187 | + } |
| 188 | - }, | 188 | + }, |
| 189 | - () => { | 189 | + () => { |
| 190 | - // 静默授权失败时,仍然跳转到授权页面作为备选方案 | 190 | + // 静默授权失败时,仍然跳转到授权页面作为备选方案 |
| 191 | - Taro.navigateTo({ | 191 | + Taro.navigateTo({ |
| 192 | - url: '/pages/auth/index' | 192 | + url: '/pages/auth/index' |
| 193 | - }) | 193 | + }) |
| 194 | - } | 194 | + } |
| 195 | - ) | 195 | + ) |
| 196 | return true | 196 | return true |
| 197 | } catch (error) { | 197 | } catch (error) { |
| 198 | console.error('静默授权异常:', error) | 198 | console.error('静默授权异常:', error) |
| ... | @@ -226,9 +226,10 @@ export const addShareFlag = (path) => { | ... | @@ -226,9 +226,10 @@ export const addShareFlag = (path) => { |
| 226 | * 在后台处理授权,不跳转页面,避免用户感知 | 226 | * 在后台处理授权,不跳转页面,避免用户感知 |
| 227 | * @param {Function} onSuccess - 授权成功回调 | 227 | * @param {Function} onSuccess - 授权成功回调 |
| 228 | * @param {Function} onError - 授权失败回调 | 228 | * @param {Function} onError - 授权失败回调 |
| 229 | + * @param {boolean} skipRedirect - 是否跳过重定向,默认false | ||
| 229 | * @returns {Promise} 授权结果 | 230 | * @returns {Promise} 授权结果 |
| 230 | */ | 231 | */ |
| 231 | -export const silentAuth = async (onSuccess, onError) => { | 232 | +export const silentAuth = async (onSuccess, onError, skipRedirect = false) => { |
| 232 | try { | 233 | try { |
| 233 | // 检查是否已经授权 | 234 | // 检查是否已经授权 |
| 234 | if (!needAuth()) { | 235 | if (!needAuth()) { |
| ... | @@ -264,12 +265,12 @@ export const silentAuth = async (onSuccess, onError) => { | ... | @@ -264,12 +265,12 @@ export const silentAuth = async (onSuccess, onError) => { |
| 264 | 265 | ||
| 265 | // 测试环境下传递openid,正式环境不传递 | 266 | // 测试环境下传递openid,正式环境不传递 |
| 266 | if (process.env.NODE_ENV === 'development') { | 267 | if (process.env.NODE_ENV === 'development') { |
| 267 | - requestData.openid = 'h-008'; | 268 | + // requestData.openid = 'h-008'; |
| 268 | // requestData.openid = 'h-009'; | 269 | // requestData.openid = 'h-009'; |
| 269 | // requestData.openid = 'h-010'; | 270 | // requestData.openid = 'h-010'; |
| 270 | // requestData.openid = 'h-011'; | 271 | // requestData.openid = 'h-011'; |
| 271 | // requestData.openid = 'h-012'; | 272 | // requestData.openid = 'h-012'; |
| 272 | - // requestData.openid = 'h-013'; | 273 | + requestData.openid = 'h-013'; |
| 273 | // requestData.openid = 'oWbdFvkD5VtloC50wSNR9IWiU2q8'; | 274 | // requestData.openid = 'oWbdFvkD5VtloC50wSNR9IWiU2q8'; |
| 274 | // requestData.openid = 'oex8h5QZnZJto3ttvO6swSvylAQo'; | 275 | // requestData.openid = 'oex8h5QZnZJto3ttvO6swSvylAQo'; |
| 275 | } | 276 | } |
| ... | @@ -307,13 +308,15 @@ export const silentAuth = async (onSuccess, onError) => { | ... | @@ -307,13 +308,15 @@ export const silentAuth = async (onSuccess, onError) => { |
| 307 | try { | 308 | try { |
| 308 | const hasFamily = await checkUserHasFamily() | 309 | const hasFamily = await checkUserHasFamily() |
| 309 | 310 | ||
| 310 | - // 如果用户没有加入家庭,跳转到欢迎页面 | 311 | + // 如果用户没有加入家庭,根据skipRedirect参数决定是否跳转 |
| 311 | if (!hasFamily) { | 312 | if (!hasFamily) { |
| 312 | - await Taro.reLaunch({ | 313 | + if (!skipRedirect) { |
| 313 | - url: '/pages/Welcome/index' | 314 | + await Taro.reLaunch({ |
| 314 | - }) | 315 | + url: '/pages/Welcome/index' |
| 316 | + }) | ||
| 317 | + } | ||
| 315 | 318 | ||
| 316 | - const result = { ...response.data, redirected: true } | 319 | + const result = { ...response.data, redirected: !skipRedirect } |
| 317 | if (onSuccess) { | 320 | if (onSuccess) { |
| 318 | onSuccess(result) | 321 | onSuccess(result) |
| 319 | } | 322 | } | ... | ... |
| 1 | /* | 1 | /* |
| 2 | * @Date: 2022-09-19 14:11:06 | 2 | * @Date: 2022-09-19 14:11:06 |
| 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com | 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com |
| 4 | - * @LastEditTime: 2025-09-12 10:44:34 | 4 | + * @LastEditTime: 2025-09-12 22:05:53 |
| 5 | * @FilePath: /lls_program/src/utils/request.js | 5 | * @FilePath: /lls_program/src/utils/request.js |
| 6 | * @Description: 简单axios封装,后续按实际处理 | 6 | * @Description: 简单axios封装,后续按实际处理 |
| 7 | */ | 7 | */ |
| ... | @@ -153,18 +153,31 @@ service.interceptors.response.use( | ... | @@ -153,18 +153,31 @@ service.interceptors.response.use( |
| 153 | // 清除无效的sessionid | 153 | // 清除无效的sessionid |
| 154 | clearSessionId(); | 154 | clearSessionId(); |
| 155 | 155 | ||
| 156 | - // 使用静默授权处理,避免页面跳转 | 156 | + // 检查当前页面,避免在Welcome页面重复跳转 |
| 157 | + let currentRoute = ''; | ||
| 158 | + try { | ||
| 159 | + const pages = getCurrentPages(); | ||
| 160 | + if (pages && pages.length > 0) { | ||
| 161 | + const currentPage = pages[pages.length - 1]; | ||
| 162 | + currentRoute = currentPage.route || ''; | ||
| 163 | + } | ||
| 164 | + } catch (error) { | ||
| 165 | + console.error('获取当前页面路由失败:', error); | ||
| 166 | + } | ||
| 167 | + | ||
| 168 | + // 使用静默授权处理,但避免重复跳转 | ||
| 157 | return silentAuth( | 169 | return silentAuth( |
| 158 | () => { | 170 | () => { |
| 159 | // 授权成功后重新发起原始请求 | 171 | // 授权成功后重新发起原始请求 |
| 160 | const originalRequest = response.config; | 172 | const originalRequest = response.config; |
| 161 | return service.request(originalRequest); | 173 | return service.request(originalRequest); |
| 162 | }, | 174 | }, |
| 163 | - (error) => { | 175 | + () => { |
| 164 | // 静默授权失败,直接返回错误,不跳转页面 | 176 | // 静默授权失败,直接返回错误,不跳转页面 |
| 165 | - console.error('静默授权失败:', error); | ||
| 166 | return Promise.reject(new Error('授权失败,请稍后重试')); | 177 | return Promise.reject(new Error('授权失败,请稍后重试')); |
| 167 | - } | 178 | + }, |
| 179 | + // 传入当前路由,避免重复跳转到Welcome页面 | ||
| 180 | + currentRoute === 'pages/Welcome/index' | ||
| 168 | ).catch(() => { | 181 | ).catch(() => { |
| 169 | // 如果静默授权完全失败,直接返回错误,不跳转页面 | 182 | // 如果静默授权完全失败,直接返回错误,不跳转页面 |
| 170 | return Promise.reject(new Error('授权失败,请稍后重试')); | 183 | return Promise.reject(new Error('授权失败,请稍后重试')); | ... | ... |
-
Please register or login to post a comment