feat: 支持健康数据上报
This commit is contained in:
@@ -14,7 +14,7 @@ import { WorkoutSummaryCard } from '@/components/WorkoutSummaryCard';
|
||||
import { Colors } from '@/constants/Colors';
|
||||
import { useAppDispatch, useAppSelector } from '@/hooks/redux';
|
||||
import { useAuthGuard } from '@/hooks/useAuthGuard';
|
||||
import { syncHealthKitToServer } from '@/services/healthKitSync';
|
||||
import { syncDailyHealthReport, syncHealthKitToServer } from '@/services/healthKitSync';
|
||||
import { setHealthData } from '@/store/healthSlice';
|
||||
import { fetchDailyMoodCheckins, selectLatestMoodRecordByDate } from '@/store/moodSlice';
|
||||
import { updateUserProfile } from '@/store/userSlice';
|
||||
@@ -64,7 +64,8 @@ export default function ExploreScreen() {
|
||||
const { t } = useTranslation();
|
||||
const stepGoal = useAppSelector((s) => s.user.profile?.dailyStepsGoal) ?? 2000;
|
||||
const userProfile = useAppSelector((s) => s.user.profile);
|
||||
|
||||
const todayWaterStats = useAppSelector((s) => s.water.todayStats);
|
||||
|
||||
const { pushIfAuthedElseLogin, isLoggedIn, ensureLoggedIn } = useAuthGuard();
|
||||
const router = useRouter();
|
||||
|
||||
@@ -293,6 +294,7 @@ export default function ExploreScreen() {
|
||||
try {
|
||||
logger.info('开始同步 HealthKit 个人健康数据到服务端...');
|
||||
|
||||
// 1. 同步个人资料 (身高、体重、出生日期)
|
||||
// 传入当前用户资料,用于 diff 比较
|
||||
const success = await syncHealthKitToServer(
|
||||
async (data) => {
|
||||
@@ -302,20 +304,36 @@ export default function ExploreScreen() {
|
||||
);
|
||||
|
||||
if (success) {
|
||||
logger.info('HealthKit 数据同步到服务端成功');
|
||||
logger.info('HealthKit 个人资料同步到服务端成功');
|
||||
} else {
|
||||
logger.info('HealthKit 数据同步到服务端跳过(无变化)或失败');
|
||||
logger.info('HealthKit 个人资料同步到服务端跳过(无变化)或失败');
|
||||
}
|
||||
|
||||
// 2. 同步每日健康数据报表 (活动、睡眠、心率等)
|
||||
// 传入今日饮水量
|
||||
const waterIntake = todayWaterStats?.totalAmount;
|
||||
logger.info('开始同步每日健康数据报表...', { waterIntake });
|
||||
|
||||
const reportSuccess = await syncDailyHealthReport(waterIntake);
|
||||
|
||||
if (reportSuccess) {
|
||||
logger.info('每日健康数据报表同步成功');
|
||||
} else {
|
||||
logger.info('每日健康数据报表同步跳过(无变化)或失败');
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
logger.error('同步 HealthKit 数据到服务端失败:', error);
|
||||
}
|
||||
}, [isLoggedIn, dispatch, userProfile]);
|
||||
}, [isLoggedIn, dispatch, userProfile, todayWaterStats]);
|
||||
|
||||
// 初始加载时执行数据加载和同步
|
||||
useEffect(() => {
|
||||
loadAllData(currentSelectedDate);
|
||||
|
||||
// 延迟1秒后执行同步,避免影响初始加载性能
|
||||
// 如果 todayWaterStats 还未加载完成,可能会导致第一次同步时 waterIntake 为 undefined
|
||||
// 但 waterSlice.fetchTodayWaterStats 会在 loadAllData 中被调用
|
||||
const syncTimer = setTimeout(() => {
|
||||
syncHealthDataToServer();
|
||||
}, 1000);
|
||||
|
||||
Reference in New Issue
Block a user