feat: 添加后台任务管理器,支持喝水和站立提醒功能

This commit is contained in:
richarjiang
2025-09-05 10:29:02 +08:00
parent acb3907344
commit 460a7e4289
9 changed files with 583 additions and 13 deletions

View File

@@ -460,13 +460,13 @@ export async function fetchTodayHRV(): Promise<number | null> {
// 获取最近几小时内的实时HRV数据
export async function fetchRecentHRV(hoursBack: number = 2): Promise<number | null> {
console.log(`开始获取最近${hoursBack}小时内的HRV数据...`);
const now = new Date();
const options = {
startDate: dayjs(now).subtract(hoursBack, 'hour').toDate().toISOString(),
endDate: now.toISOString()
};
return fetchHeartRateVariability(options);
}
@@ -543,18 +543,18 @@ export async function saveWaterIntakeToHealthKit(amount: number, recordedAt?: st
endDate: recordedAt ? new Date(recordedAt).toISOString() : new Date().toISOString(),
};
AppleHealthKit.saveWater(waterOptions, (error: Object, result: boolean) => {
AppleHealthKit.saveWater(waterOptions, (error: Object, result) => {
if (error) {
console.error('添加饮水记录到 HealthKit 失败:', error);
resolve(false);
return;
}
console.log('成功添加饮水记录到 HealthKit:', {
originalAmount: amount,
convertedAmount: amount / 1000,
recordedAt,
result
console.log('成功添加饮水记录到 HealthKit:', {
originalAmount: amount,
convertedAmount: amount / 1000,
recordedAt,
result
});
resolve(true);
});
@@ -570,7 +570,7 @@ export async function getWaterIntakeFromHealthKit(options: HealthDataOptions): P
resolve([]);
return;
}
console.log('从 HealthKit 获取饮水记录:', results);
resolve(results || []);
});
@@ -584,7 +584,31 @@ export async function deleteWaterIntakeFromHealthKit(recordId: string, recordedA
// 这是一个占位函数,实际实现可能需要更复杂的逻辑
console.log('注意: HealthKit 通常不支持直接删除单条饮水记录');
console.log('记录信息:', { recordId, recordedAt });
// 返回 true 表示"成功"(但实际上可能没有真正删除)
return Promise.resolve(true);
}
// 获取当前小时的站立状态
export async function getCurrentHourStandStatus(): Promise<{ hasStood: boolean; standHours: number; standHoursGoal: number }> {
try {
const currentHour = new Date().getHours();
console.log(`检查当前小时 ${currentHour} 的站立状态...`);
// 获取今日健康数据
const todayHealthData = await fetchTodayHealthData();
return {
hasStood: todayHealthData.standHours > currentHour - 1, // 如果站立小时数大于当前小时-1说明当前小时已站立
standHours: todayHealthData.standHours,
standHoursGoal: todayHealthData.standHoursGoal
};
} catch (error) {
console.error('获取当前小时站立状态失败:', error);
return {
hasStood: true, // 默认认为已站立,避免过度提醒
standHours: 0,
standHoursGoal: 12
};
}
}