hookehuyr

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

增加页面跳转的重试机制和错误处理,提升授权后返回页面的可靠性
- 添加重试逻辑,默认重试2次
- 实现多级降级方案确保跳转成功
- 增加错误日志和用户友好提示
...@@ -60,28 +60,130 @@ export const navigateToAuth = (returnPath) => { ...@@ -60,28 +60,130 @@ export const navigateToAuth = (returnPath) => {
60 * 授权完成后返回原页面 60 * 授权完成后返回原页面
61 * @param {string} defaultPath - 默认返回路径,如果没有保存的路径则使用此路径 61 * @param {string} defaultPath - 默认返回路径,如果没有保存的路径则使用此路径
62 */ 62 */
63 -export const returnToOriginalPage = (defaultPath = '/pages/Dashboard/index') => { 63 +/**
64 + * 返回到原始页面,带有容错处理机制
65 + * @param {string} defaultPath - 默认跳转路径
66 + * @param {number} retryCount - 重试次数,默认为2
67 + */
68 +export const returnToOriginalPage = async (defaultPath = '/pages/Dashboard/index', retryCount = 2) => {
64 const router = routerStore() 69 const router = routerStore()
65 const savedPath = router.url 70 const savedPath = router.url
66 71
67 - if (savedPath && savedPath !== '') { 72 + /**
68 - // 清除保存的路径 73 + * 执行页面跳转的核心函数
69 - router.remove() 74 + * @param {string} targetPath - 目标路径
75 + * @param {boolean} isHomePage - 是否为首页
76 + */
77 + const executeNavigation = async (targetPath, isHomePage = false) => {
78 + try {
79 + if (isHomePage) {
80 + await Taro.reLaunch({
81 + url: targetPath
82 + })
83 + } else {
84 + await Taro.redirectTo({
85 + url: targetPath
86 + })
87 + }
88 + return true
89 + } catch (error) {
90 + console.warn('页面跳转失败:', error)
91 + return false
92 + }
93 + }
70 94
71 - // 判断是否是首页,如果是首页使用reLaunch,否则使用redirectTo 95 + /**
72 - if (savedPath.includes('Dashboard/index') || savedPath === 'pages/Dashboard/index') { 96 + * 带重试机制的页面跳转
73 - Taro.reLaunch({ 97 + * @param {string} targetPath - 目标路径
74 - url: `/${savedPath}` 98 + * @param {boolean} isHomePage - 是否为首页
75 - }) 99 + * @param {number} maxRetries - 最大重试次数
100 + */
101 + const navigateWithRetry = async (targetPath, isHomePage, maxRetries) => {
102 + for (let i = 0; i <= maxRetries; i++) {
103 + const success = await executeNavigation(targetPath, isHomePage)
104 + if (success) {
105 + return true
106 + }
107 +
108 + // 如果不是最后一次重试,等待一段时间后重试
109 + if (i < maxRetries) {
110 + console.warn(`页面跳转重试 ${i + 1}/${maxRetries}`)
111 + await new Promise(resolve => setTimeout(resolve, 1000 * (i + 1))) // 递增延迟
112 + }
113 + }
114 + return false
115 + }
116 +
117 + try {
118 + if (savedPath && savedPath !== '') {
119 + // 清除保存的路径
120 + router.remove()
121 +
122 + const targetUrl = `/${savedPath}`
123 + const isHomePage = savedPath.includes('Dashboard/index') || savedPath === 'pages/Dashboard/index'
124 +
125 + // 尝试跳转到保存的页面
126 + const success = await navigateWithRetry(targetUrl, isHomePage, retryCount)
127 +
128 + if (!success) {
129 + console.warn('跳转到保存页面失败,尝试跳转到默认页面')
130 + // 如果跳转失败,尝试跳转到默认页面
131 + const fallbackSuccess = await navigateWithRetry(defaultPath, true, retryCount)
132 +
133 + if (!fallbackSuccess) {
134 + // 最后的降级方案:使用 switchTab 跳转到首页
135 + console.warn('所有跳转方式失败,使用 switchTab 降级方案')
136 + try {
137 + await Taro.switchTab({
138 + url: '/pages/Dashboard/index'
139 + })
140 + } catch (switchError) {
141 + console.error('switchTab 也失败了:', switchError)
142 + // 显示用户友好的错误提示
143 + Taro.showToast({
144 + title: '页面跳转失败,请重新打开小程序',
145 + icon: 'none',
146 + duration: 3000
147 + })
148 + }
149 + }
150 + }
76 } else { 151 } else {
77 - Taro.redirectTo({ 152 + // 没有保存的路径,直接跳转到默认页面
78 - url: `/${savedPath}` 153 + const success = await navigateWithRetry(defaultPath, true, retryCount)
154 +
155 + if (!success) {
156 + // 降级方案
157 + console.warn('跳转到默认页面失败,使用 switchTab 降级方案')
158 + try {
159 + await Taro.switchTab({
160 + url: '/pages/Dashboard/index'
161 + })
162 + } catch (switchError) {
163 + console.error('switchTab 也失败了:', switchError)
164 + Taro.showToast({
165 + title: '页面跳转失败,请重新打开小程序',
166 + icon: 'none',
167 + duration: 3000
168 + })
169 + }
170 + }
171 + }
172 + } catch (error) {
173 + console.error('returnToOriginalPage 执行出错:', error)
174 + // 最终的错误处理
175 + try {
176 + await Taro.switchTab({
177 + url: '/pages/Dashboard/index'
178 + })
179 + } catch (finalError) {
180 + console.error('最终降级方案也失败了:', finalError)
181 + Taro.showToast({
182 + title: '系统异常,请重新打开小程序',
183 + icon: 'none',
184 + duration: 3000
79 }) 185 })
80 } 186 }
81 - } else {
82 - Taro.reLaunch({
83 - url: defaultPath
84 - })
85 } 187 }
86 } 188 }
87 189
......