feat(nutrition): 添加营养数据保存功能到HealthKit,包括蛋白质、脂肪和碳水化合物
This commit is contained in:
108
utils/health.ts
108
utils/health.ts
@@ -1139,6 +1139,114 @@ export async function saveWaterIntakeToHealthKit(amount: number, recordedAt?: st
|
||||
}
|
||||
}
|
||||
|
||||
// 添加蛋白质记录到 HealthKit
|
||||
export async function saveProteinToHealthKit(grams: number, recordedAt?: string): Promise<boolean> {
|
||||
try {
|
||||
console.log('开始保存蛋白质记录到HealthKit...', { grams, recordedAt });
|
||||
|
||||
const options = {
|
||||
amount: grams,
|
||||
recordedAt: recordedAt || new Date().toISOString()
|
||||
};
|
||||
|
||||
const result = await HealthKitManager.saveProteinToHealthKit(options);
|
||||
|
||||
if (result && result.success) {
|
||||
console.log('蛋白质记录保存成功:', result);
|
||||
return true;
|
||||
} else {
|
||||
console.error('蛋白质记录保存失败:', result);
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('添加蛋白质记录到 HealthKit 失败:', error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// 添加脂肪记录到 HealthKit
|
||||
export async function saveFatToHealthKit(grams: number, recordedAt?: string): Promise<boolean> {
|
||||
try {
|
||||
console.log('开始保存脂肪记录到HealthKit...', { grams, recordedAt });
|
||||
|
||||
const options = {
|
||||
amount: grams,
|
||||
recordedAt: recordedAt || new Date().toISOString()
|
||||
};
|
||||
|
||||
const result = await HealthKitManager.saveFatToHealthKit(options);
|
||||
|
||||
if (result && result.success) {
|
||||
console.log('脂肪记录保存成功:', result);
|
||||
return true;
|
||||
} else {
|
||||
console.error('脂肪记录保存失败:', result);
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('添加脂肪记录到 HealthKit 失败:', error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// 添加碳水化合物记录到 HealthKit
|
||||
export async function saveCarbohydratesToHealthKit(grams: number, recordedAt?: string): Promise<boolean> {
|
||||
try {
|
||||
console.log('开始保存碳水化合物记录到HealthKit...', { grams, recordedAt });
|
||||
|
||||
const options = {
|
||||
amount: grams,
|
||||
recordedAt: recordedAt || new Date().toISOString()
|
||||
};
|
||||
|
||||
const result = await HealthKitManager.saveCarbohydratesToHealthKit(options);
|
||||
|
||||
if (result && result.success) {
|
||||
console.log('碳水化合物记录保存成功:', result);
|
||||
return true;
|
||||
} else {
|
||||
console.error('碳水化合物记录保存失败:', result);
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('添加碳水化合物记录到 HealthKit 失败:', error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// 批量保存营养数据到 HealthKit(蛋白质、脂肪和碳水化合物)
|
||||
export async function saveNutritionToHealthKit(
|
||||
nutrition: { proteinGrams?: number; fatGrams?: number; carbohydrateGrams?: number },
|
||||
recordedAt?: string
|
||||
): Promise<{ proteinSaved: boolean; fatSaved: boolean; carbohydrateSaved: boolean }> {
|
||||
try {
|
||||
console.log('开始批量保存营养数据到HealthKit...', { nutrition, recordedAt });
|
||||
|
||||
const results = await Promise.allSettled([
|
||||
nutrition.proteinGrams && nutrition.proteinGrams > 0
|
||||
? saveProteinToHealthKit(nutrition.proteinGrams, recordedAt)
|
||||
: Promise.resolve(false),
|
||||
nutrition.fatGrams && nutrition.fatGrams > 0
|
||||
? saveFatToHealthKit(nutrition.fatGrams, recordedAt)
|
||||
: Promise.resolve(false),
|
||||
nutrition.carbohydrateGrams && nutrition.carbohydrateGrams > 0
|
||||
? saveCarbohydratesToHealthKit(nutrition.carbohydrateGrams, recordedAt)
|
||||
: Promise.resolve(false),
|
||||
]);
|
||||
|
||||
const proteinSaved = results[0].status === 'fulfilled' ? results[0].value : false;
|
||||
const fatSaved = results[1].status === 'fulfilled' ? results[1].value : false;
|
||||
const carbohydrateSaved = results[2].status === 'fulfilled' ? results[2].value : false;
|
||||
|
||||
console.log('营养数据批量保存结果:', { proteinSaved, fatSaved, carbohydrateSaved });
|
||||
|
||||
return { proteinSaved, fatSaved, carbohydrateSaved };
|
||||
} catch (error) {
|
||||
console.error('批量保存营养数据到 HealthKit 失败:', error);
|
||||
return { proteinSaved: false, fatSaved: false, carbohydrateSaved: false };
|
||||
}
|
||||
}
|
||||
|
||||
// 获取 HealthKit 中的饮水记录
|
||||
export async function getWaterIntakeFromHealthKit(options: HealthDataOptions): Promise<any[]> {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user