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)); ...@@ -68,7 +68,7 @@ export const getMyFamiliesAPI = () => fn(fetch.get(Api.LIST_MY_FAMILIES));
68 * @returns {number} response.data.pending_points[].id - 积分ID 68 * @returns {number} response.data.pending_points[].id - 积分ID
69 * @returns {string} response.data.pending_points[].title - 积分标题 69 * @returns {string} response.data.pending_points[].title - 积分标题
70 * @returns {string} response.data.pending_points[].points - 积分数量 70 * @returns {string} response.data.pending_points[].points - 积分数量
71 - * @returns {string} response.data.pending_points[].source_type - 积分来源 71 + * @returns {string} response.data.pending_points[].source_type - 积分来源 (WALKING=步数, CHECK_IN=参与一次活动打卡, CHECK_IN_COUNT=完成活动打卡次数, FAMILY_SIZE=家庭成员达5人, COMPANION_PHOTO=陪伴拍照, WHEELCHAIR_COMPANION=陪伴轮椅拍照)
72 * @returns {string} response.data.pending_points[].note - 积分说明 72 * @returns {string} response.data.pending_points[].note - 积分说明
73 * @returns {Array} response.data.step_ranking - 步数排名 73 * @returns {Array} response.data.step_ranking - 步数排名
74 * @returns {string} response.data.step_ranking[].user_id - 用户ID 74 * @returns {string} response.data.step_ranking[].user_id - 用户ID
......
...@@ -26,7 +26,7 @@ ...@@ -26,7 +26,7 @@
26 26
27 <!-- 已授权状态 - 显示步数相关内容 --> 27 <!-- 已授权状态 - 显示步数相关内容 -->
28 <view v-else> 28 <view v-else>
29 - <slot :steps="todaySteps" :isLoading="isLoading" /> 29 + <slot :isLoading="isLoading" />
30 </view> 30 </view>
31 </view> 31 </view>
32 </template> 32 </template>
...@@ -38,11 +38,10 @@ import Taro from '@tarojs/taro' ...@@ -38,11 +38,10 @@ import Taro from '@tarojs/taro'
38 import { syncWxStepAPI } from '@/api/points' 38 import { syncWxStepAPI } from '@/api/points'
39 39
40 // 定义事件 40 // 定义事件
41 -const emit = defineEmits(['auth-change', 'steps-update']) 41 +const emit = defineEmits(['auth-change', 'steps-update', 'steps-synced'])
42 42
43 // 响应式数据 43 // 响应式数据
44 const isAuthorized = ref(false) 44 const isAuthorized = ref(false)
45 -const todaySteps = ref(0)
46 const isLoading = ref(false) 45 const isLoading = ref(false)
47 46
48 /** 47 /**
...@@ -130,14 +129,6 @@ const getWeRunData = async () => { ...@@ -130,14 +129,6 @@ const getWeRunData = async () => {
130 // 获取微信运动数据 129 // 获取微信运动数据
131 const weRunRes = await Taro.getWeRunData() 130 const weRunRes = await Taro.getWeRunData()
132 131
133 - // 这里需要将加密数据发送到后端解密
134 - // 暂时使用模拟数据
135 - const mockSteps = Math.floor(Math.random() * 10000) + 1000
136 - todaySteps.value = mockSteps
137 -
138 - // 通知父组件步数更新
139 - emit('steps-update', todaySteps.value)
140 -
141 console.log('微信运动数据获取成功:', { 132 console.log('微信运动数据获取成功:', {
142 encryptedData: weRunRes.encryptedData, 133 encryptedData: weRunRes.encryptedData,
143 iv: weRunRes.iv, 134 iv: weRunRes.iv,
...@@ -153,17 +144,16 @@ const getWeRunData = async () => { ...@@ -153,17 +144,16 @@ const getWeRunData = async () => {
153 // icon: 'none' 144 // icon: 'none'
154 // }) 145 // })
155 console.warn('同步微信步数成功', data); 146 console.warn('同步微信步数成功', data);
156 - // todaySteps.value = decryptedData.todaySteps 147 +
148 + // 触发步数同步完成事件,通知父组件
149 + emit('steps-synced', data);
150 +
157 isLoading.value = false 151 isLoading.value = false
158 } 152 }
159 153
160 } catch (error) { 154 } catch (error) {
161 console.error('获取微信运动数据失败:', error) 155 console.error('获取微信运动数据失败:', error)
162 156
163 - // 如果获取失败,使用默认值
164 - todaySteps.value = 0
165 - emit('steps-update', 0)
166 -
167 Taro.showToast({ 157 Taro.showToast({
168 title: '获取步数失败', 158 title: '获取步数失败',
169 icon: 'none' 159 icon: 'none'
......
...@@ -17,9 +17,9 @@ ...@@ -17,9 +17,9 @@
17 <WeRunAuth 17 <WeRunAuth
18 ref="weRunAuthRef" 18 ref="weRunAuthRef"
19 @auth-change="handleAuthChange" 19 @auth-change="handleAuthChange"
20 - @steps-update="handleStepsUpdate" 20 + @steps-synced="handleStepsSynced"
21 > 21 >
22 - <template #default="{ steps, isLoading }"> 22 + <template #default>
23 <!-- Today's steps section --> 23 <!-- Today's steps section -->
24 <!-- TODO: 今日获取的步数怎么同步到下面那个积分池里面去 --> 24 <!-- TODO: 今日获取的步数怎么同步到下面那个积分池里面去 -->
25 <view class="px-5 py-6 bg-white rounded-xl shadow-md mx-4 mt-4"> 25 <view class="px-5 py-6 bg-white rounded-xl shadow-md mx-4 mt-4">
...@@ -29,16 +29,7 @@ ...@@ -29,16 +29,7 @@
29 <view class="flex justify-between items-center"> 29 <view class="flex justify-between items-center">
30 <view class="flex items-baseline"> 30 <view class="flex items-baseline">
31 <span class="text-4xl font-bold"> 31 <span class="text-4xl font-bold">
32 - <template v-if="isLoading"> 32 + {{ todaySteps }}
33 - <span class="loading-dots">
34 - <span class="dot">.</span>
35 - <span class="dot">.</span>
36 - <span class="dot">.</span>
37 - </span>
38 - </template>
39 - <template v-else>
40 - {{ steps.toLocaleString() }}
41 - </template>
42 </span> 33 </span>
43 <span class="ml-1 text-gray-500">步</span> 34 <span class="ml-1 text-gray-500">步</span>
44 </view> 35 </view>
...@@ -86,7 +77,7 @@ ...@@ -86,7 +77,7 @@
86 <view class="flex justify-between items-center mb-4"> 77 <view class="flex justify-between items-center mb-4">
87 <h2 class="font-medium text-lg">今日家庭步数排行</h2> 78 <h2 class="font-medium text-lg">今日家庭步数排行</h2>
88 <span class="text-sm text-gray-500"> 79 <span class="text-sm text-gray-500">
89 - 总计 {{ getTotalSteps(steps).toLocaleString() }} 步 80 + 总计 {{ getTotalSteps(totalFamilySteps).toLocaleString() }} 步
90 </span> 81 </span>
91 </view> 82 </view>
92 <view class="grid grid-cols-4 gap-2"> 83 <view class="grid grid-cols-4 gap-2">
...@@ -206,7 +197,7 @@ const defaultFamilyCover = 'https://cdn.ipadbiz.cn/lls_prog/images/default-famil ...@@ -206,7 +197,7 @@ const defaultFamilyCover = 'https://cdn.ipadbiz.cn/lls_prog/images/default-famil
206 // 默认头像 197 // 默认头像
207 const defaultAvatar = 'https://cdn.ipadbiz.cn/mlaj/images/icon_1.jpeg' 198 const defaultAvatar = 'https://cdn.ipadbiz.cn/mlaj/images/icon_1.jpeg'
208 // 接口信息 199 // 接口信息
209 -import { getMyFamiliesAPI } from '@/api/family' 200 +import { getMyFamiliesAPI, getFamilyDashboardAPI } from '@/api/family'
210 201
211 const todaySteps = ref(0); 202 const todaySteps = ref(0);
212 const isWeRunAuthorized = ref(false); 203 const isWeRunAuthorized = ref(false);
...@@ -289,12 +280,13 @@ const handleAuthChange = (authorized) => { ...@@ -289,12 +280,13 @@ const handleAuthChange = (authorized) => {
289 } 280 }
290 281
291 /** 282 /**
292 - * 处理步数数据更新 283 + * 处理微信步数同步完成事件
293 - * @param {number} steps - 步数 284 + * @param {Object} data - 同步返回的数据
294 */ 285 */
295 -const handleStepsUpdate = (steps) => { 286 +const handleStepsSynced = async (data) => {
296 - todaySteps.value = steps 287 + console.log('微信步数同步完成:', data)
297 - console.log('步数更新:', steps) 288 + // 步数同步完成后,重新获取家庭数据
289 + await initPageData()
298 } 290 }
299 291
300 /** 292 /**
...@@ -376,11 +368,27 @@ const openFamilyRank = () => { ...@@ -376,11 +368,27 @@ const openFamilyRank = () => {
376 Taro.navigateTo({ url: '/pages/FamilyRank/index' }); 368 Taro.navigateTo({ url: '/pages/FamilyRank/index' });
377 } 369 }
378 370
371 +const family_id = ref('');
372 +const totalFamilySteps = ref(0);
373 +
379 const initPageData = async () => { 374 const initPageData = async () => {
380 - // 获取用户信息 375 + const { code, data } = await getFamilyDashboardAPI({ family_id: family_id.value });
381 - familyName.value = '张爷爷的家庭' 376 + if (code) {
382 - familySlogn.value = '每日走万步,全家一起行' 377 + // 获取用户信息
383 - familyCover.value = defaultFamilyCover 378 + console.warn(data);
379 + // 真实情况下 需要重新获取积分列表, 测试随机积分获取状态, 接口有积分列表就显示出来, 没有就不显示
380 + showTotalPointsOnly.value = !data.pending_points.length;
381 + // 总积分数量也要从接口获取传进去
382 + finalTotalPoints.value = 10086;
383 + // 获取用户信息
384 + familyName.value = data.family.name;
385 + familySlogn.value = data.family.note;
386 + familyCover.value = data.family.avatar_url || defaultFamilyCover;
387 + // 获取今日我的步数
388 + todaySteps.value = data.my_today_step;
389 + // 获取家庭总步数
390 + totalFamilySteps.value = data.family_today_step;
391 + }
384 } 392 }
385 393
386 useDidShow(async () => { 394 useDidShow(async () => {
...@@ -391,21 +399,18 @@ useDidShow(async () => { ...@@ -391,21 +399,18 @@ useDidShow(async () => {
391 if (!data.length) { 399 if (!data.length) {
392 Taro.redirectTo({ url: '/pages/Welcome/index' }); 400 Taro.redirectTo({ url: '/pages/Welcome/index' });
393 return; // 直接返回,不执行后续逻辑 401 return; // 直接返回,不执行后续逻辑
402 + } else {
403 + family_id.value = data[0].id;
404 + // 先初始化基础页面数据(不包含步数相关数据)
405 + await initPageData();
406 + // 然后刷新微信步数数据(只有在已加入家庭时才获取)
407 + if (weRunAuthRef.value) {
408 + weRunAuthRef.value.checkAuthStatus(true);
409 + }
394 } 410 }
395 } 411 }
396 412
397 - // 只有在用户已加入家庭的情况下才初始化页面数据
398 - initPageData();
399 -
400 - // 刷新微信步数数据(只有在已加入家庭时才获取)
401 - if (weRunAuthRef.value) {
402 - weRunAuthRef.value.checkAuthStatus(true);
403 - }
404 413
405 - // TODO: 真实情况下 需要重新获取积分列表, 测试随机积分获取状态, 接口有积分列表就显示出来, 没有就不显示
406 - showTotalPointsOnly.value = Math.random() > 0.5 ? true : false;
407 - // 总积分数量也要从接口获取传进去
408 - finalTotalPoints.value = 10086;
409 }) 414 })
410 415
411 useReady(async () => { 416 useReady(async () => {
......