fix(微信步数同步): 修复401未授权时错误跳转登录页的问题并优化同步流程
处理微信步数同步失败时错误触发401跳转的问题 添加手动更新按钮显示逻辑和同步失败处理 优化同步流程的错误处理和状态管理
Showing
3 changed files
with
74 additions
and
26 deletions
| ... | @@ -35,14 +35,16 @@ | ... | @@ -35,14 +35,16 @@ |
| 35 | import { ref, onMounted, defineEmits } from 'vue' | 35 | import { ref, onMounted, defineEmits } from 'vue' |
| 36 | import Taro from '@tarojs/taro' | 36 | import Taro from '@tarojs/taro' |
| 37 | 37 | ||
| 38 | -import { syncWxStepAPI } from '@/api/points' | 38 | +// import { syncWxStepAPI } from '@/api/points' |
| 39 | +import { fetch } from '@/api/fn' | ||
| 39 | 40 | ||
| 40 | // 定义事件 | 41 | // 定义事件 |
| 41 | -const emit = defineEmits(['auth-change', 'steps-update', 'steps-synced']) | 42 | +const emit = defineEmits(['auth-change', 'steps-update', 'steps-synced', 'sync-failed']) |
| 42 | 43 | ||
| 43 | // 响应式数据 | 44 | // 响应式数据 |
| 44 | const isAuthorized = ref(false) | 45 | const isAuthorized = ref(false) |
| 45 | const isLoading = ref(false) | 46 | const isLoading = ref(false) |
| 47 | +const showManualUpdate = ref(false) | ||
| 46 | 48 | ||
| 47 | /** | 49 | /** |
| 48 | * @description 检查微信运动授权状态 | 50 | * @description 检查微信运动授权状态 |
| ... | @@ -135,22 +137,42 @@ const getWeRunData = async () => { | ... | @@ -135,22 +137,42 @@ const getWeRunData = async () => { |
| 135 | code: loginRes.code | 137 | code: loginRes.code |
| 136 | }) | 138 | }) |
| 137 | 139 | ||
| 138 | - // 同步微信步数 | 140 | + // 同步微信步数 - 直接调用原始API以获取完整响应 |
| 139 | - const { code, data } = await syncWxStepAPI({ encryptedData: weRunRes.encryptedData, iv: weRunRes.iv }); | 141 | + try { |
| 140 | - if (code) { | 142 | + const response = await fetch.post('/srv/?a=point&t=sync_wx_step', { encryptedData: weRunRes.encryptedData, iv: weRunRes.iv }); |
| 141 | - // 提示获取步数成功 | 143 | + const { code, data, msg } = response.data; |
| 142 | - // Taro.showToast({ | 144 | + |
| 143 | - // title: `获取步数成功`, | 145 | + if (code == 1) { |
| 144 | - // icon: 'none' | 146 | + // 提示获取步数成功 |
| 145 | - // }) | 147 | + console.warn('同步微信步数成功', data); |
| 146 | - console.warn('同步微信步数成功', data); | 148 | + |
| 147 | - | 149 | + // 同步成功,隐藏手动更新按钮 |
| 148 | - // 触发步数同步完成事件,通知父组件 | 150 | + showManualUpdate.value = false; |
| 149 | - emit('steps-synced', data); | 151 | + |
| 150 | - | 152 | + // 触发步数同步完成事件,通知父组件 |
| 151 | - isLoading.value = false | 153 | + emit('steps-synced', data); |
| 152 | - } | ||
| 153 | 154 | ||
| 155 | + isLoading.value = false | ||
| 156 | + } else { | ||
| 157 | + // 检查是否是401错误且包含同步微信步数失败的消息 | ||
| 158 | + if (msg && msg.includes('同步微信步数失败')) { | ||
| 159 | + console.warn('同步微信步数失败,显示手动更新按钮'); | ||
| 160 | + showManualUpdate.value = true; | ||
| 161 | + emit('sync-failed', { showManualUpdate: true }); | ||
| 162 | + } | ||
| 163 | + Taro.showToast({ | ||
| 164 | + title: msg || '同步步数失败', | ||
| 165 | + icon: 'none', | ||
| 166 | + duration: 2000 | ||
| 167 | + }); | ||
| 168 | + } | ||
| 169 | + } catch (apiError) { | ||
| 170 | + console.error('同步微信步数API调用失败:', apiError); | ||
| 171 | + Taro.showToast({ | ||
| 172 | + title: '网络请求失败', | ||
| 173 | + icon: 'none' | ||
| 174 | + }); | ||
| 175 | + } | ||
| 154 | } catch (error) { | 176 | } catch (error) { |
| 155 | console.error('获取微信运动数据失败:', error) | 177 | console.error('获取微信运动数据失败:', error) |
| 156 | 178 | ||
| ... | @@ -175,7 +197,8 @@ const refreshSteps = async () => { | ... | @@ -175,7 +197,8 @@ const refreshSteps = async () => { |
| 175 | // 暴露方法给父组件 | 197 | // 暴露方法给父组件 |
| 176 | defineExpose({ | 198 | defineExpose({ |
| 177 | refreshSteps, | 199 | refreshSteps, |
| 178 | - checkAuthStatus | 200 | + checkAuthStatus, |
| 201 | + showManualUpdate | ||
| 179 | }) | 202 | }) |
| 180 | 203 | ||
| 181 | // 组件挂载时检查授权状态,但不获取步数数据 | 204 | // 组件挂载时检查授权状态,但不获取步数数据 | ... | ... |
| ... | @@ -18,6 +18,7 @@ | ... | @@ -18,6 +18,7 @@ |
| 18 | ref="weRunAuthRef" | 18 | ref="weRunAuthRef" |
| 19 | @auth-change="handleAuthChange" | 19 | @auth-change="handleAuthChange" |
| 20 | @steps-synced="handleStepsSynced" | 20 | @steps-synced="handleStepsSynced" |
| 21 | + @sync-failed="handleSyncFailed" | ||
| 21 | > | 22 | > |
| 22 | <template #default> | 23 | <template #default> |
| 23 | <!-- Today's steps section --> | 24 | <!-- Today's steps section --> |
| ... | @@ -34,10 +35,11 @@ | ... | @@ -34,10 +35,11 @@ |
| 34 | <span class="ml-1 text-gray-500">步</span> | 35 | <span class="ml-1 text-gray-500">步</span> |
| 35 | </view> | 36 | </view> |
| 36 | <view class="flex gap-2"> | 37 | <view class="flex gap-2"> |
| 37 | - <!-- <view class="bg-green-500 text-white px-4 py-2 rounded-full text-xs" @click="handleRefreshSteps"> | 38 | + <view v-if="showManualUpdateButton" class="bg-green-500 text-white px-4 py-2 rounded-full text-xs" @click="handleRefreshSteps" :class="{ 'opacity-50': isRefreshingSteps }"> |
| 38 | - 更新步数 | 39 | + <text v-if="!isRefreshingSteps">手动更新</text> |
| 39 | - </view> --> | 40 | + <text v-else>更新中...</text> |
| 40 | - <view class="bg-blue-500 text-white px-4 py-2 rounded-full text-sm" @click="handleCollectAll"> | 41 | + </view> |
| 42 | + <view class="bg-blue-500 text-white px-4 py-2 rounded-full text-xs" @click="handleCollectAll"> | ||
| 41 | 一键收取 | 43 | 一键收取 |
| 42 | </view> | 44 | </view> |
| 43 | </view> | 45 | </view> |
| ... | @@ -208,6 +210,8 @@ const weRunAuthRef = ref(null) | ... | @@ -208,6 +210,8 @@ const weRunAuthRef = ref(null) |
| 208 | const showTotalPointsOnly = ref(false) | 210 | const showTotalPointsOnly = ref(false) |
| 209 | const finalTotalPoints = ref(0) | 211 | const finalTotalPoints = ref(0) |
| 210 | const pendingPoints = ref([]) // 待收集的积分数据 | 212 | const pendingPoints = ref([]) // 待收集的积分数据 |
| 213 | +const showManualUpdateButton = ref(false) // 是否显示手动更新步数按钮 | ||
| 214 | +const isRefreshingSteps = ref(false) // 手动更新步数的loading状态 | ||
| 211 | 215 | ||
| 212 | const familyName = ref('') | 216 | const familyName = ref('') |
| 213 | const familySlogn = ref('') | 217 | const familySlogn = ref('') |
| ... | @@ -269,8 +273,15 @@ const handleCollectionComplete = (points) => { | ... | @@ -269,8 +273,15 @@ const handleCollectionComplete = (points) => { |
| 269 | * 手动刷新微信步数 | 273 | * 手动刷新微信步数 |
| 270 | */ | 274 | */ |
| 271 | const handleRefreshSteps = async () => { | 275 | const handleRefreshSteps = async () => { |
| 272 | - if (weRunAuthRef.value) { | 276 | + if (weRunAuthRef.value && !isRefreshingSteps.value) { |
| 273 | - await weRunAuthRef.value.refreshSteps() | 277 | + try { |
| 278 | + isRefreshingSteps.value = true | ||
| 279 | + await weRunAuthRef.value.refreshSteps() | ||
| 280 | + } catch (error) { | ||
| 281 | + console.error('手动刷新步数失败:', error) | ||
| 282 | + } finally { | ||
| 283 | + isRefreshingSteps.value = false | ||
| 284 | + } | ||
| 274 | } | 285 | } |
| 275 | } | 286 | } |
| 276 | 287 | ||
| ... | @@ -289,11 +300,25 @@ const handleAuthChange = (authorized) => { | ... | @@ -289,11 +300,25 @@ const handleAuthChange = (authorized) => { |
| 289 | */ | 300 | */ |
| 290 | const handleStepsSynced = async (data) => { | 301 | const handleStepsSynced = async (data) => { |
| 291 | console.log('微信步数同步完成:', data) | 302 | console.log('微信步数同步完成:', data) |
| 303 | + // 同步成功后隐藏手动更新按钮 | ||
| 304 | + showManualUpdateButton.value = false | ||
| 292 | // 步数同步完成后,重新获取家庭数据 | 305 | // 步数同步完成后,重新获取家庭数据 |
| 293 | await initPageData() | 306 | await initPageData() |
| 294 | } | 307 | } |
| 295 | 308 | ||
| 296 | /** | 309 | /** |
| 310 | + * 处理微信步数同步失败事件 | ||
| 311 | + * @param {Object} data - 失败返回的数据 | ||
| 312 | + */ | ||
| 313 | +const handleSyncFailed = (data) => { | ||
| 314 | + console.log('微信步数同步失败:', data) | ||
| 315 | + // 显示手动更新按钮 | ||
| 316 | + if (data.showManualUpdate) { | ||
| 317 | + showManualUpdateButton.value = true | ||
| 318 | + } | ||
| 319 | +} | ||
| 320 | + | ||
| 321 | +/** | ||
| 297 | * 计算总步数(包含用户步数和家庭成员步数) | 322 | * 计算总步数(包含用户步数和家庭成员步数) |
| 298 | * @returns {string} 格式化后的总步数 | 323 | * @returns {string} 格式化后的总步数 |
| 299 | */ | 324 | */ | ... | ... |
| 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-01 13:47:06 | 4 | + * @LastEditTime: 2025-09-03 20:57:33 |
| 5 | * @FilePath: /lls_program/src/utils/request.js | 5 | * @FilePath: /lls_program/src/utils/request.js |
| 6 | * @Description: 简单axios封装,后续按实际处理 | 6 | * @Description: 简单axios封装,后续按实际处理 |
| 7 | */ | 7 | */ |
| ... | @@ -151,7 +151,7 @@ service.interceptors.response.use( | ... | @@ -151,7 +151,7 @@ service.interceptors.response.use( |
| 151 | * 处理401未授权状态 | 151 | * 处理401未授权状态 |
| 152 | * 清除本地sessionid并跳转到登录页 | 152 | * 清除本地sessionid并跳转到登录页 |
| 153 | */ | 153 | */ |
| 154 | - if (response.data.code === 401) { | 154 | + if (response.data.code === 401 && !response.data.msg.includes('同步微信步数失败')) { |
| 155 | // 清除无效的sessionid | 155 | // 清除无效的sessionid |
| 156 | clearSessionId(); | 156 | clearSessionId(); |
| 157 | /** | 157 | /** | ... | ... |
-
Please register or login to post a comment