fix(auth): 修复授权后跳转逻辑并启用降级导航
修复授权成功后跳转回原页面时硬编码路径的问题,改为使用默认跳转 启用被注释的导航降级逻辑,当navigateTo失败时自动降级为redirectTo
Showing
3 changed files
with
113 additions
and
114 deletions
| 1 | /* | 1 | /* |
| 2 | * @Date: 2025-06-28 10:33:00 | 2 | * @Date: 2025-06-28 10:33:00 |
| 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com | 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com |
| 4 | - * @LastEditTime: 2026-01-12 23:48:04 | 4 | + * @LastEditTime: 2026-01-13 00:07:41 |
| 5 | * @FilePath: /xyxBooking-weapp/src/app.js | 5 | * @FilePath: /xyxBooking-weapp/src/app.js |
| 6 | * @Description: 文件描述 | 6 | * @Description: 文件描述 |
| 7 | */ | 7 | */ |
| ... | @@ -20,130 +20,130 @@ import { formatDatetime } from '@/utils/tools' | ... | @@ -20,130 +20,130 @@ import { formatDatetime } from '@/utils/tools' |
| 20 | * @returns 格式化后的支付记录数组 | 20 | * @returns 格式化后的支付记录数组 |
| 21 | */ | 21 | */ |
| 22 | const formatGroup = (data) => { | 22 | const formatGroup = (data) => { |
| 23 | - let lastPayId = null; | 23 | + let lastPayId = null; |
| 24 | - for (let i = 0; i < data.length; i++) { | 24 | + for (let i = 0; i < data.length; i++) { |
| 25 | - if (data[i].pay_id !== lastPayId) { | 25 | + if (data[i].pay_id !== lastPayId) { |
| 26 | - data[i].sort = 1; | 26 | + data[i].sort = 1; |
| 27 | - lastPayId = data[i].pay_id; | 27 | + lastPayId = data[i].pay_id; |
| 28 | - } else { | 28 | + } else { |
| 29 | - data[i].sort = 0; | 29 | + data[i].sort = 0; |
| 30 | + } | ||
| 30 | } | 31 | } |
| 31 | - } | 32 | + return data; |
| 32 | - return data; | ||
| 33 | } | 33 | } |
| 34 | 34 | ||
| 35 | const App = createApp({ | 35 | const App = createApp({ |
| 36 | // 对应 onLaunch | 36 | // 对应 onLaunch |
| 37 | - async onLaunch(options) { | 37 | + async onLaunch(options) { |
| 38 | - const path = options?.path || '' | 38 | + const path = options?.path || '' |
| 39 | - const query = options?.query || {} | 39 | + const query = options?.query || {} |
| 40 | 40 | ||
| 41 | - const query_string = Object.keys(query) | 41 | + const query_string = Object.keys(query) |
| 42 | - .map((key) => `${key}=${encodeURIComponent(query[key])}`) | 42 | + .map((key) => `${key}=${encodeURIComponent(query[key])}`) |
| 43 | - .join('&') | 43 | + .join('&') |
| 44 | - const full_path = query_string ? `${path}?${query_string}` : path | 44 | + const full_path = query_string ? `${path}?${query_string}` : path |
| 45 | 45 | ||
| 46 | - // 保存当前页面路径,用于授权后跳转回原页面 | 46 | + // 保存当前页面路径,用于授权后跳转回原页面 |
| 47 | - if (full_path) { | 47 | + if (full_path) { |
| 48 | - saveCurrentPagePath(full_path) | 48 | + saveCurrentPagePath(full_path) |
| 49 | - } | 49 | + } |
| 50 | 50 | ||
| 51 | - /** | 51 | + /** |
| 52 | - * 预加载二维码数据 | 52 | + * 预加载二维码数据 |
| 53 | - * - 仅在有网络连接时调用 | 53 | + * - 仅在有网络连接时调用 |
| 54 | - * - 成功后将数据存储到本地缓存(key: OFFLINE_QR_DATA) | 54 | + * - 成功后将数据存储到本地缓存(key: OFFLINE_QR_DATA) |
| 55 | - * - 失败则移除缓存 | 55 | + * - 失败则移除缓存 |
| 56 | - */ | 56 | + */ |
| 57 | - const preloadQrData = async () => { | 57 | + const preloadQrData = async () => { |
| 58 | - try { | 58 | + try { |
| 59 | - const { code, data } = await qrcodeListAPI(); | 59 | + const { code, data } = await qrcodeListAPI(); |
| 60 | - if (code && data) { | 60 | + if (code && data) { |
| 61 | - data.forEach(item => { | 61 | + data.forEach(item => { |
| 62 | - item.datetime = formatDatetime({ begin_time: item.begin_time, end_time: item.end_time }) | 62 | + item.datetime = formatDatetime({ begin_time: item.begin_time, end_time: item.end_time }) |
| 63 | - item.sort = 0; | 63 | + item.sort = 0; |
| 64 | - }); | 64 | + }); |
| 65 | - const validData = data.filter(item => item.qr_code !== ''); | 65 | + const validData = data.filter(item => item.qr_code !== ''); |
| 66 | - if (validData.length > 0) { | 66 | + if (validData.length > 0) { |
| 67 | - const processed = formatGroup(validData); | 67 | + const processed = formatGroup(validData); |
| 68 | - const offline_data = processed.map(item => ({ | 68 | + const offline_data = processed.map(item => ({ |
| 69 | - name: item.name, | 69 | + name: item.name, |
| 70 | - id_number: item.id_number, | 70 | + id_number: item.id_number, |
| 71 | - qr_code: item.qr_code, | 71 | + qr_code: item.qr_code, |
| 72 | - begin_time: item.begin_time, | 72 | + begin_time: item.begin_time, |
| 73 | - end_time: item.end_time, | 73 | + end_time: item.end_time, |
| 74 | - datetime: item.datetime, | 74 | + datetime: item.datetime, |
| 75 | - pay_id: item.pay_id, | 75 | + pay_id: item.pay_id, |
| 76 | - sort: item.sort, | 76 | + sort: item.sort, |
| 77 | - })); | 77 | + })); |
| 78 | - Taro.setStorageSync('OFFLINE_QR_DATA', offline_data); | 78 | + Taro.setStorageSync('OFFLINE_QR_DATA', offline_data); |
| 79 | - } else { | 79 | + } else { |
| 80 | - Taro.removeStorageSync('OFFLINE_QR_DATA'); | 80 | + Taro.removeStorageSync('OFFLINE_QR_DATA'); |
| 81 | - } | 81 | + } |
| 82 | + } | ||
| 83 | + } catch (e) { | ||
| 84 | + console.error('Preload QR failed', e); | ||
| 82 | } | 85 | } |
| 83 | - } catch (e) { | 86 | + }; |
| 84 | - console.error('Preload QR failed', e); | ||
| 85 | - } | ||
| 86 | - }; | ||
| 87 | 87 | ||
| 88 | - /** | 88 | + /** |
| 89 | - * 监听网络状态变化 | 89 | + * 监听网络状态变化 |
| 90 | - * - 当网络连接且有授权时,调用 preloadQrData 预加载二维码数据 | 90 | + * - 当网络连接且有授权时,调用 preloadQrData 预加载二维码数据 |
| 91 | - */ | 91 | + */ |
| 92 | - Taro.onNetworkStatusChange((res) => { | 92 | + Taro.onNetworkStatusChange((res) => { |
| 93 | - if (res.isConnected && hasAuth()) { | 93 | + if (res.isConnected && hasAuth()) { |
| 94 | - preloadQrData() | 94 | + preloadQrData() |
| 95 | - } | 95 | + } |
| 96 | - }) | 96 | + }) |
| 97 | 97 | ||
| 98 | - const is_usable_network = (network_type) => { | 98 | + const is_usable_network = (network_type) => { |
| 99 | - return ['wifi', '4g', '5g', '3g'].includes(network_type) | 99 | + return ['wifi', '4g', '5g', '3g'].includes(network_type) |
| 100 | - } | 100 | + } |
| 101 | 101 | ||
| 102 | - /** | 102 | + /** |
| 103 | - * 尝试在网络可用时预加载二维码数据 | 103 | + * 尝试在网络可用时预加载二维码数据 |
| 104 | - * - 仅在有授权时调用 | 104 | + * - 仅在有授权时调用 |
| 105 | - * - 成功后将数据存储到本地缓存(key: OFFLINE_QR_DATA) | 105 | + * - 成功后将数据存储到本地缓存(key: OFFLINE_QR_DATA) |
| 106 | - * - 失败则移除缓存 | 106 | + * - 失败则移除缓存 |
| 107 | - * @returns {Promise<void>} | 107 | + * @returns {Promise<void>} |
| 108 | - */ | 108 | + */ |
| 109 | - const try_preload_when_online = () => { | 109 | + const try_preload_when_online = () => { |
| 110 | - if (!hasAuth()) return | 110 | + if (!hasAuth()) return |
| 111 | - Taro.getNetworkType({ | 111 | + Taro.getNetworkType({ |
| 112 | - success: (res) => { | 112 | + success: (res) => { |
| 113 | - if (is_usable_network(res.networkType)) { | 113 | + if (is_usable_network(res.networkType)) { |
| 114 | - preloadQrData() | 114 | + preloadQrData() |
| 115 | + } | ||
| 115 | } | 116 | } |
| 116 | - } | 117 | + }) |
| 117 | - }) | 118 | + } |
| 118 | - } | ||
| 119 | 119 | ||
| 120 | - /** | 120 | + /** |
| 121 | - * 尝试在有授权时预加载二维码数据 | 121 | + * 尝试在有授权时预加载二维码数据 |
| 122 | - * - 若无授权,则尝试静默授权 | 122 | + * - 若无授权,则尝试静默授权 |
| 123 | - * - 授权成功后调用 try_preload_when_online 预加载数据 | 123 | + * - 授权成功后调用 try_preload_when_online 预加载数据 |
| 124 | - * - 授权失败则跳转至授权页面 | 124 | + * - 授权失败则跳转至授权页面 |
| 125 | - */ | 125 | + */ |
| 126 | - if (hasAuth()) { | 126 | + if (hasAuth()) { |
| 127 | - try_preload_when_online() | 127 | + try_preload_when_online() |
| 128 | - return | 128 | + return |
| 129 | - } | 129 | + } |
| 130 | 130 | ||
| 131 | - if (path === 'pages/auth/index') return | 131 | + if (path === 'pages/auth/index') return |
| 132 | 132 | ||
| 133 | - try { | 133 | + try { |
| 134 | - // 尝试静默授权 | 134 | + // 尝试静默授权 |
| 135 | - await silentAuth() | 135 | + await silentAuth() |
| 136 | - // 授权成功后预加载数据 | 136 | + // 授权成功后预加载数据 |
| 137 | - try_preload_when_online() | 137 | + try_preload_when_online() |
| 138 | - } catch (error) { | 138 | + } catch (error) { |
| 139 | - console.error('静默授权失败:', error) | 139 | + console.error('静默授权失败:', error) |
| 140 | - // 授权失败则跳转至授权页面 | 140 | + // 授权失败则跳转至授权页面 |
| 141 | - navigateToAuth(full_path || undefined) | 141 | + navigateToAuth(full_path || undefined) |
| 142 | - } | 142 | + } |
| 143 | - }, | 143 | + }, |
| 144 | - onShow() { | 144 | + onShow() { |
| 145 | - }, | 145 | + }, |
| 146 | - // 入口组件不需要实现 render 方法,即使实现了也会被 taro 所覆盖 | 146 | + // 入口组件不需要实现 render 方法,即使实现了也会被 taro 所覆盖 |
| 147 | }); | 147 | }); |
| 148 | 148 | ||
| 149 | App.use(createPinia()) | 149 | App.use(createPinia()) | ... | ... |
| ... | @@ -19,7 +19,7 @@ import { silentAuth, returnToOriginalPage } from '@/utils/authRedirect' | ... | @@ -19,7 +19,7 @@ import { silentAuth, returnToOriginalPage } from '@/utils/authRedirect' |
| 19 | 19 | ||
| 20 | useDidShow(() => { | 20 | useDidShow(() => { |
| 21 | silentAuth() | 21 | silentAuth() |
| 22 | - .then(() => returnToOriginalPage('/pages/index/index')) | 22 | + .then(() => returnToOriginalPage()) |
| 23 | .catch((error) => { | 23 | .catch((error) => { |
| 24 | Taro.showToast({ title: error?.message || '授权失败', icon: 'none' }) | 24 | Taro.showToast({ title: error?.message || '授权失败', icon: 'none' }) |
| 25 | }) | 25 | }) | ... | ... |
| ... | @@ -211,10 +211,9 @@ export const navigateToAuth = (return_path) => { | ... | @@ -211,10 +211,9 @@ export const navigateToAuth = (return_path) => { |
| 211 | return | 211 | return |
| 212 | } | 212 | } |
| 213 | 213 | ||
| 214 | - // TAG: navigateTo 失败时(例如页面栈满),降级为 redirectTo | 214 | + Taro.navigateTo({ url: '/pages/auth/index' }).catch(() => { |
| 215 | - // Taro.navigateTo({ url: '/pages/auth/index' }).catch(() => { | 215 | + return Taro.redirectTo({ url: '/pages/auth/index' }) |
| 216 | - // return Taro.redirectTo({ url: '/pages/auth/index' }) | 216 | + }) |
| 217 | - // }) | ||
| 218 | } | 217 | } |
| 219 | 218 | ||
| 220 | /** | 219 | /** | ... | ... |
-
Please register or login to post a comment