hookehuyr

refactor(WeRunAuth): 移除模拟步数数据并改进同步逻辑

重构微信运动授权组件,移除本地模拟步数数据,改为完全依赖后端同步
添加新的steps-synced事件通知父组件同步完成
```

```msg
feat(Dashboard): 实现家庭数据接口集成

将仪表盘页面改为从接口获取家庭信息和步数数据
移除本地模拟数据,改进初始化流程
```

```msg
docs(family): 添加积分来源类型注释

补充pending_points.source_type字段的枚举值说明
......@@ -68,7 +68,7 @@ export const getMyFamiliesAPI = () => fn(fetch.get(Api.LIST_MY_FAMILIES));
* @returns {number} response.data.pending_points[].id - 积分ID
* @returns {string} response.data.pending_points[].title - 积分标题
* @returns {string} response.data.pending_points[].points - 积分数量
* @returns {string} response.data.pending_points[].source_type - 积分来源
* @returns {string} response.data.pending_points[].source_type - 积分来源 (WALKING=步数, CHECK_IN=参与一次活动打卡, CHECK_IN_COUNT=完成活动打卡次数, FAMILY_SIZE=家庭成员达5人, COMPANION_PHOTO=陪伴拍照, WHEELCHAIR_COMPANION=陪伴轮椅拍照)
* @returns {string} response.data.pending_points[].note - 积分说明
* @returns {Array} response.data.step_ranking - 步数排名
* @returns {string} response.data.step_ranking[].user_id - 用户ID
......
......@@ -26,7 +26,7 @@
<!-- 已授权状态 - 显示步数相关内容 -->
<view v-else>
<slot :steps="todaySteps" :isLoading="isLoading" />
<slot :isLoading="isLoading" />
</view>
</view>
</template>
......@@ -38,11 +38,10 @@ import Taro from '@tarojs/taro'
import { syncWxStepAPI } from '@/api/points'
// 定义事件
const emit = defineEmits(['auth-change', 'steps-update'])
const emit = defineEmits(['auth-change', 'steps-update', 'steps-synced'])
// 响应式数据
const isAuthorized = ref(false)
const todaySteps = ref(0)
const isLoading = ref(false)
/**
......@@ -130,14 +129,6 @@ const getWeRunData = async () => {
// 获取微信运动数据
const weRunRes = await Taro.getWeRunData()
// 这里需要将加密数据发送到后端解密
// 暂时使用模拟数据
const mockSteps = Math.floor(Math.random() * 10000) + 1000
todaySteps.value = mockSteps
// 通知父组件步数更新
emit('steps-update', todaySteps.value)
console.log('微信运动数据获取成功:', {
encryptedData: weRunRes.encryptedData,
iv: weRunRes.iv,
......@@ -153,17 +144,16 @@ const getWeRunData = async () => {
// icon: 'none'
// })
console.warn('同步微信步数成功', data);
// todaySteps.value = decryptedData.todaySteps
// 触发步数同步完成事件,通知父组件
emit('steps-synced', data);
isLoading.value = false
}
} catch (error) {
console.error('获取微信运动数据失败:', error)
// 如果获取失败,使用默认值
todaySteps.value = 0
emit('steps-update', 0)
Taro.showToast({
title: '获取步数失败',
icon: 'none'
......
......@@ -17,9 +17,9 @@
<WeRunAuth
ref="weRunAuthRef"
@auth-change="handleAuthChange"
@steps-update="handleStepsUpdate"
@steps-synced="handleStepsSynced"
>
<template #default="{ steps, isLoading }">
<template #default>
<!-- Today's steps section -->
<!-- TODO: 今日获取的步数怎么同步到下面那个积分池里面去 -->
<view class="px-5 py-6 bg-white rounded-xl shadow-md mx-4 mt-4">
......@@ -29,16 +29,7 @@
<view class="flex justify-between items-center">
<view class="flex items-baseline">
<span class="text-4xl font-bold">
<template v-if="isLoading">
<span class="loading-dots">
<span class="dot">.</span>
<span class="dot">.</span>
<span class="dot">.</span>
</span>
</template>
<template v-else>
{{ steps.toLocaleString() }}
</template>
{{ todaySteps }}
</span>
<span class="ml-1 text-gray-500">步</span>
</view>
......@@ -86,7 +77,7 @@
<view class="flex justify-between items-center mb-4">
<h2 class="font-medium text-lg">今日家庭步数排行</h2>
<span class="text-sm text-gray-500">
总计 {{ getTotalSteps(steps).toLocaleString() }} 步
总计 {{ getTotalSteps(totalFamilySteps).toLocaleString() }} 步
</span>
</view>
<view class="grid grid-cols-4 gap-2">
......@@ -206,7 +197,7 @@ const defaultFamilyCover = 'https://cdn.ipadbiz.cn/lls_prog/images/default-famil
// 默认头像
const defaultAvatar = 'https://cdn.ipadbiz.cn/mlaj/images/icon_1.jpeg'
// 接口信息
import { getMyFamiliesAPI } from '@/api/family'
import { getMyFamiliesAPI, getFamilyDashboardAPI } from '@/api/family'
const todaySteps = ref(0);
const isWeRunAuthorized = ref(false);
......@@ -289,12 +280,13 @@ const handleAuthChange = (authorized) => {
}
/**
* 处理步数数据更新
* @param {number} steps - 步数
* 处理微信步数同步完成事件
* @param {Object} data - 同步返回的数据
*/
const handleStepsUpdate = (steps) => {
todaySteps.value = steps
console.log('步数更新:', steps)
const handleStepsSynced = async (data) => {
console.log('微信步数同步完成:', data)
// 步数同步完成后,重新获取家庭数据
await initPageData()
}
/**
......@@ -376,11 +368,27 @@ const openFamilyRank = () => {
Taro.navigateTo({ url: '/pages/FamilyRank/index' });
}
const family_id = ref('');
const totalFamilySteps = ref(0);
const initPageData = async () => {
const { code, data } = await getFamilyDashboardAPI({ family_id: family_id.value });
if (code) {
// 获取用户信息
familyName.value = '张爷爷的家庭'
familySlogn.value = '每日走万步,全家一起行'
familyCover.value = defaultFamilyCover
console.warn(data);
// 真实情况下 需要重新获取积分列表, 测试随机积分获取状态, 接口有积分列表就显示出来, 没有就不显示
showTotalPointsOnly.value = !data.pending_points.length;
// 总积分数量也要从接口获取传进去
finalTotalPoints.value = 10086;
// 获取用户信息
familyName.value = data.family.name;
familySlogn.value = data.family.note;
familyCover.value = data.family.avatar_url || defaultFamilyCover;
// 获取今日我的步数
todaySteps.value = data.my_today_step;
// 获取家庭总步数
totalFamilySteps.value = data.family_today_step;
}
}
useDidShow(async () => {
......@@ -391,21 +399,18 @@ useDidShow(async () => {
if (!data.length) {
Taro.redirectTo({ url: '/pages/Welcome/index' });
return; // 直接返回,不执行后续逻辑
}
}
// 只有在用户已加入家庭的情况下才初始化页面数据
initPageData();
// 刷新微信步数数据(只有在已加入家庭时才获取)
} else {
family_id.value = data[0].id;
// 先初始化基础页面数据(不包含步数相关数据)
await initPageData();
// 然后刷新微信步数数据(只有在已加入家庭时才获取)
if (weRunAuthRef.value) {
weRunAuthRef.value.checkAuthStatus(true);
}
}
}
// TODO: 真实情况下 需要重新获取积分列表, 测试随机积分获取状态, 接口有积分列表就显示出来, 没有就不显示
showTotalPointsOnly.value = Math.random() > 0.5 ? true : false;
// 总积分数量也要从接口获取传进去
finalTotalPoints.value = 10086;
})
useReady(async () => {
......