hookehuyr

feat(auth): 添加带重试机制的页面跳转功能

增加页面跳转的重试机制和错误处理,提升授权后返回页面的可靠性
- 添加重试逻辑,默认重试2次
- 实现多级降级方案确保跳转成功
- 增加错误日志和用户友好提示
......@@ -60,28 +60,130 @@ export const navigateToAuth = (returnPath) => {
* 授权完成后返回原页面
* @param {string} defaultPath - 默认返回路径,如果没有保存的路径则使用此路径
*/
export const returnToOriginalPage = (defaultPath = '/pages/Dashboard/index') => {
/**
* 返回到原始页面,带有容错处理机制
* @param {string} defaultPath - 默认跳转路径
* @param {number} retryCount - 重试次数,默认为2
*/
export const returnToOriginalPage = async (defaultPath = '/pages/Dashboard/index', retryCount = 2) => {
const router = routerStore()
const savedPath = router.url
if (savedPath && savedPath !== '') {
// 清除保存的路径
router.remove()
/**
* 执行页面跳转的核心函数
* @param {string} targetPath - 目标路径
* @param {boolean} isHomePage - 是否为首页
*/
const executeNavigation = async (targetPath, isHomePage = false) => {
try {
if (isHomePage) {
await Taro.reLaunch({
url: targetPath
})
} else {
await Taro.redirectTo({
url: targetPath
})
}
return true
} catch (error) {
console.warn('页面跳转失败:', error)
return false
}
}
// 判断是否是首页,如果是首页使用reLaunch,否则使用redirectTo
if (savedPath.includes('Dashboard/index') || savedPath === 'pages/Dashboard/index') {
Taro.reLaunch({
url: `/${savedPath}`
})
/**
* 带重试机制的页面跳转
* @param {string} targetPath - 目标路径
* @param {boolean} isHomePage - 是否为首页
* @param {number} maxRetries - 最大重试次数
*/
const navigateWithRetry = async (targetPath, isHomePage, maxRetries) => {
for (let i = 0; i <= maxRetries; i++) {
const success = await executeNavigation(targetPath, isHomePage)
if (success) {
return true
}
// 如果不是最后一次重试,等待一段时间后重试
if (i < maxRetries) {
console.warn(`页面跳转重试 ${i + 1}/${maxRetries}`)
await new Promise(resolve => setTimeout(resolve, 1000 * (i + 1))) // 递增延迟
}
}
return false
}
try {
if (savedPath && savedPath !== '') {
// 清除保存的路径
router.remove()
const targetUrl = `/${savedPath}`
const isHomePage = savedPath.includes('Dashboard/index') || savedPath === 'pages/Dashboard/index'
// 尝试跳转到保存的页面
const success = await navigateWithRetry(targetUrl, isHomePage, retryCount)
if (!success) {
console.warn('跳转到保存页面失败,尝试跳转到默认页面')
// 如果跳转失败,尝试跳转到默认页面
const fallbackSuccess = await navigateWithRetry(defaultPath, true, retryCount)
if (!fallbackSuccess) {
// 最后的降级方案:使用 switchTab 跳转到首页
console.warn('所有跳转方式失败,使用 switchTab 降级方案')
try {
await Taro.switchTab({
url: '/pages/Dashboard/index'
})
} catch (switchError) {
console.error('switchTab 也失败了:', switchError)
// 显示用户友好的错误提示
Taro.showToast({
title: '页面跳转失败,请重新打开小程序',
icon: 'none',
duration: 3000
})
}
}
}
} else {
Taro.redirectTo({
url: `/${savedPath}`
// 没有保存的路径,直接跳转到默认页面
const success = await navigateWithRetry(defaultPath, true, retryCount)
if (!success) {
// 降级方案
console.warn('跳转到默认页面失败,使用 switchTab 降级方案')
try {
await Taro.switchTab({
url: '/pages/Dashboard/index'
})
} catch (switchError) {
console.error('switchTab 也失败了:', switchError)
Taro.showToast({
title: '页面跳转失败,请重新打开小程序',
icon: 'none',
duration: 3000
})
}
}
}
} catch (error) {
console.error('returnToOriginalPage 执行出错:', error)
// 最终的错误处理
try {
await Taro.switchTab({
url: '/pages/Dashboard/index'
})
} catch (finalError) {
console.error('最终降级方案也失败了:', finalError)
Taro.showToast({
title: '系统异常,请重新打开小程序',
icon: 'none',
duration: 3000
})
}
} else {
Taro.reLaunch({
url: defaultPath
})
}
}
......