hookehuyr

fix(微信步数同步): 修复401未授权时错误跳转登录页的问题并优化同步流程

处理微信步数同步失败时错误触发401跳转的问题
添加手动更新按钮显示逻辑和同步失败处理
优化同步流程的错误处理和状态管理
......@@ -35,14 +35,16 @@
import { ref, onMounted, defineEmits } from 'vue'
import Taro from '@tarojs/taro'
import { syncWxStepAPI } from '@/api/points'
// import { syncWxStepAPI } from '@/api/points'
import { fetch } from '@/api/fn'
// 定义事件
const emit = defineEmits(['auth-change', 'steps-update', 'steps-synced'])
const emit = defineEmits(['auth-change', 'steps-update', 'steps-synced', 'sync-failed'])
// 响应式数据
const isAuthorized = ref(false)
const isLoading = ref(false)
const showManualUpdate = ref(false)
/**
* @description 检查微信运动授权状态
......@@ -135,22 +137,42 @@ const getWeRunData = async () => {
code: loginRes.code
})
// 同步微信步数
const { code, data } = await syncWxStepAPI({ encryptedData: weRunRes.encryptedData, iv: weRunRes.iv });
if (code) {
// 提示获取步数成功
// Taro.showToast({
// title: `获取步数成功`,
// icon: 'none'
// })
console.warn('同步微信步数成功', data);
// 触发步数同步完成事件,通知父组件
emit('steps-synced', data);
isLoading.value = false
}
// 同步微信步数 - 直接调用原始API以获取完整响应
try {
const response = await fetch.post('/srv/?a=point&t=sync_wx_step', { encryptedData: weRunRes.encryptedData, iv: weRunRes.iv });
const { code, data, msg } = response.data;
if (code == 1) {
// 提示获取步数成功
console.warn('同步微信步数成功', data);
// 同步成功,隐藏手动更新按钮
showManualUpdate.value = false;
// 触发步数同步完成事件,通知父组件
emit('steps-synced', data);
isLoading.value = false
} else {
// 检查是否是401错误且包含同步微信步数失败的消息
if (msg && msg.includes('同步微信步数失败')) {
console.warn('同步微信步数失败,显示手动更新按钮');
showManualUpdate.value = true;
emit('sync-failed', { showManualUpdate: true });
}
Taro.showToast({
title: msg || '同步步数失败',
icon: 'none',
duration: 2000
});
}
} catch (apiError) {
console.error('同步微信步数API调用失败:', apiError);
Taro.showToast({
title: '网络请求失败',
icon: 'none'
});
}
} catch (error) {
console.error('获取微信运动数据失败:', error)
......@@ -175,7 +197,8 @@ const refreshSteps = async () => {
// 暴露方法给父组件
defineExpose({
refreshSteps,
checkAuthStatus
checkAuthStatus,
showManualUpdate
})
// 组件挂载时检查授权状态,但不获取步数数据
......
......@@ -18,6 +18,7 @@
ref="weRunAuthRef"
@auth-change="handleAuthChange"
@steps-synced="handleStepsSynced"
@sync-failed="handleSyncFailed"
>
<template #default>
<!-- Today's steps section -->
......@@ -34,10 +35,11 @@
<span class="ml-1 text-gray-500">步</span>
</view>
<view class="flex gap-2">
<!-- <view class="bg-green-500 text-white px-4 py-2 rounded-full text-xs" @click="handleRefreshSteps">
更新步数
</view> -->
<view class="bg-blue-500 text-white px-4 py-2 rounded-full text-sm" @click="handleCollectAll">
<view v-if="showManualUpdateButton" class="bg-green-500 text-white px-4 py-2 rounded-full text-xs" @click="handleRefreshSteps" :class="{ 'opacity-50': isRefreshingSteps }">
<text v-if="!isRefreshingSteps">手动更新</text>
<text v-else>更新中...</text>
</view>
<view class="bg-blue-500 text-white px-4 py-2 rounded-full text-xs" @click="handleCollectAll">
一键收取
</view>
</view>
......@@ -208,6 +210,8 @@ const weRunAuthRef = ref(null)
const showTotalPointsOnly = ref(false)
const finalTotalPoints = ref(0)
const pendingPoints = ref([]) // 待收集的积分数据
const showManualUpdateButton = ref(false) // 是否显示手动更新步数按钮
const isRefreshingSteps = ref(false) // 手动更新步数的loading状态
const familyName = ref('')
const familySlogn = ref('')
......@@ -269,8 +273,15 @@ const handleCollectionComplete = (points) => {
* 手动刷新微信步数
*/
const handleRefreshSteps = async () => {
if (weRunAuthRef.value) {
await weRunAuthRef.value.refreshSteps()
if (weRunAuthRef.value && !isRefreshingSteps.value) {
try {
isRefreshingSteps.value = true
await weRunAuthRef.value.refreshSteps()
} catch (error) {
console.error('手动刷新步数失败:', error)
} finally {
isRefreshingSteps.value = false
}
}
}
......@@ -289,11 +300,25 @@ const handleAuthChange = (authorized) => {
*/
const handleStepsSynced = async (data) => {
console.log('微信步数同步完成:', data)
// 同步成功后隐藏手动更新按钮
showManualUpdateButton.value = false
// 步数同步完成后,重新获取家庭数据
await initPageData()
}
/**
* 处理微信步数同步失败事件
* @param {Object} data - 失败返回的数据
*/
const handleSyncFailed = (data) => {
console.log('微信步数同步失败:', data)
// 显示手动更新按钮
if (data.showManualUpdate) {
showManualUpdateButton.value = true
}
}
/**
* 计算总步数(包含用户步数和家庭成员步数)
* @returns {string} 格式化后的总步数
*/
......
/*
* @Date: 2022-09-19 14:11:06
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2025-09-01 13:47:06
* @LastEditTime: 2025-09-03 20:57:33
* @FilePath: /lls_program/src/utils/request.js
* @Description: 简单axios封装,后续按实际处理
*/
......@@ -151,7 +151,7 @@ service.interceptors.response.use(
* 处理401未授权状态
* 清除本地sessionid并跳转到登录页
*/
if (response.data.code === 401) {
if (response.data.code === 401 && !response.data.msg.includes('同步微信步数失败')) {
// 清除无效的sessionid
clearSessionId();
/**
......