feat(healthkit): 实现HealthKit与服务端的双向数据同步,包括身高、体重和出生日期的获取与保存
This commit is contained in:
164
utils/health.ts
164
utils/health.ts
@@ -1061,18 +1061,170 @@ export async function testHRVDataFetch(date: Date = dayjs().toDate()): Promise<v
|
||||
}
|
||||
}
|
||||
|
||||
// 更新healthkit中的体重 (暂未实现)
|
||||
export async function updateWeight(_weight: number) {
|
||||
// === 个人健康数据读取和写入方法 ===
|
||||
|
||||
/**
|
||||
* 从 HealthKit 获取身高(单位:厘米)
|
||||
*/
|
||||
export async function fetchHeight(): Promise<number | null> {
|
||||
try {
|
||||
// Note: Weight saving would need to be implemented in native module
|
||||
console.log('体重保存到HealthKit暂未实现');
|
||||
return true; // Return true for now to not break existing functionality
|
||||
console.log('开始从 HealthKit 获取身高...');
|
||||
const result = await HealthKitManager.getHeight();
|
||||
|
||||
if (result && typeof result.value === 'number') {
|
||||
const heightInCm = Math.round(result.value);
|
||||
console.log('成功获取身高:', heightInCm, 'cm');
|
||||
return heightInCm;
|
||||
} else {
|
||||
console.log('未找到身高数据');
|
||||
return null;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('更新体重失败:', error);
|
||||
console.error('获取身高失败:', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 HealthKit 获取体重(单位:千克)
|
||||
*/
|
||||
export async function fetchWeight(): Promise<number | null> {
|
||||
try {
|
||||
console.log('开始从 HealthKit 获取体重...');
|
||||
const result = await HealthKitManager.getWeight();
|
||||
|
||||
if (result && typeof result.value === 'number') {
|
||||
const weightInKg = Math.round(result.value * 10) / 10; // 保留1位小数
|
||||
console.log('成功获取体重:', weightInKg, 'kg');
|
||||
return weightInKg;
|
||||
} else {
|
||||
console.log('未找到体重数据');
|
||||
return null;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取体重失败:', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 HealthKit 获取出生日期
|
||||
*/
|
||||
export async function fetchDateOfBirth(): Promise<string | null> {
|
||||
try {
|
||||
console.log('开始从 HealthKit 获取出生日期...');
|
||||
const result = await HealthKitManager.getDateOfBirth();
|
||||
|
||||
if (result && typeof result.value === 'string') {
|
||||
console.log('成功获取出生日期:', result.value);
|
||||
return result.value;
|
||||
} else {
|
||||
console.log('未找到出生日期数据');
|
||||
return null;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取出生日期失败:', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存身高到 HealthKit
|
||||
* @param heightInCm 身高(单位:厘米)
|
||||
* @param unit 单位,默认为 'cm'(厘米),也支持 'in'(英寸)
|
||||
*/
|
||||
export async function saveHeight(heightInCm: number, unit: 'cm' | 'in' = 'cm'): Promise<boolean> {
|
||||
try {
|
||||
console.log('开始保存身高到 HealthKit...', { heightInCm, unit });
|
||||
|
||||
if (heightInCm <= 0) {
|
||||
console.error('身高值无效:', heightInCm);
|
||||
return false;
|
||||
}
|
||||
|
||||
const options = {
|
||||
value: heightInCm,
|
||||
unit: unit
|
||||
};
|
||||
|
||||
const result = await HealthKitManager.saveHeight(options);
|
||||
|
||||
if (result && result.success) {
|
||||
console.log('身高保存成功');
|
||||
return true;
|
||||
} else {
|
||||
console.error('身高保存失败:', result);
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('保存身高到 HealthKit 失败:', error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存体重到 HealthKit
|
||||
* @param weightInKg 体重(单位:千克)
|
||||
*/
|
||||
export async function saveWeight(weightInKg: number): Promise<boolean> {
|
||||
try {
|
||||
console.log('开始保存体重到 HealthKit...', { weightInKg });
|
||||
|
||||
if (weightInKg <= 0) {
|
||||
console.error('体重值无效:', weightInKg);
|
||||
return false;
|
||||
}
|
||||
|
||||
const options = {
|
||||
value: weightInKg
|
||||
};
|
||||
|
||||
const result = await HealthKitManager.saveWeight(options);
|
||||
|
||||
if (result && result.success) {
|
||||
console.log('体重保存成功');
|
||||
return true;
|
||||
} else {
|
||||
console.error('体重保存失败:', result);
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('保存体重到 HealthKit 失败:', error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// 保持向后兼容的旧函数名
|
||||
export async function updateWeight(weight: number): Promise<boolean> {
|
||||
return saveWeight(weight);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量获取个人健康数据(身高、体重、出生日期)
|
||||
*/
|
||||
export async function fetchPersonalHealthData(): Promise<{
|
||||
height: number | null;
|
||||
weight: number | null;
|
||||
dateOfBirth: string | null;
|
||||
}> {
|
||||
try {
|
||||
console.log('开始批量获取个人健康数据...');
|
||||
|
||||
const [height, weight, dateOfBirth] = await Promise.all([
|
||||
fetchHeight(),
|
||||
fetchWeight(),
|
||||
fetchDateOfBirth()
|
||||
]);
|
||||
|
||||
console.log('个人健康数据获取完成:', { height, weight, dateOfBirth });
|
||||
|
||||
return { height, weight, dateOfBirth };
|
||||
} catch (error) {
|
||||
console.error('批量获取个人健康数据失败:', error);
|
||||
return { height: null, weight: null, dateOfBirth: null };
|
||||
}
|
||||
}
|
||||
|
||||
export async function testOxygenSaturationData(_date: Date = dayjs().toDate()): Promise<void> {
|
||||
console.log('=== 开始测试血氧饱和度数据获取 ===');
|
||||
|
||||
|
||||
Reference in New Issue
Block a user