feat: 添加睡眠详情页面,集成睡眠数据获取功能,优化健康数据权限管理,更新相关组件以支持睡眠统计和展示

This commit is contained in:
richarjiang
2025-09-08 09:54:33 +08:00
parent df7f04808e
commit e91283fe4e
14 changed files with 1186 additions and 261 deletions

View File

@@ -103,6 +103,14 @@ function createDateRange(date: Date): HealthDataOptions {
};
}
// 睡眠数据专用的日期范围函数 - 从前一天晚上到当天结束
function createSleepDateRange(date: Date): HealthDataOptions {
return {
startDate: dayjs(date).subtract(1, 'day').hour(18).minute(0).second(0).millisecond(0).toDate().toISOString(), // 前一天18:00开始
endDate: dayjs(date).endOf('day').toDate().toISOString() // 当天结束
};
}
// 睡眠时长计算
function calculateSleepDuration(samples: any[]): number {
return samples.reduce((total, sample) => {
@@ -433,9 +441,12 @@ async function fetchBasalEnergyBurned(options: HealthDataOptions): Promise<numbe
});
}
async function fetchSleepDuration(options: HealthDataOptions): Promise<number> {
async function fetchSleepDuration(date: Date): Promise<number> {
return new Promise((resolve) => {
AppleHealthKit.getSleepSamples(options, (err, res) => {
// 使用睡眠专用的日期范围,包含前一天晚上的睡眠数据
const sleepOptions = createSleepDateRange(date);
AppleHealthKit.getSleepSamples(sleepOptions, (err, res) => {
if (err) {
logError('睡眠数据', err);
return resolve(0);
@@ -445,7 +456,24 @@ async function fetchSleepDuration(options: HealthDataOptions): Promise<number> {
return resolve(0);
}
logSuccess('睡眠', res);
resolve(calculateSleepDuration(res));
// 过滤睡眠数据,只计算主睡眠时间段
const filteredSamples = res.filter(sample => {
if (!sample || !sample.startDate || !sample.endDate) return false;
const startDate = dayjs(sample.startDate);
const endDate = dayjs(sample.endDate);
const targetDate = dayjs(date);
// 判断这个睡眠段是否属于当天的主睡眠
// 睡眠段的结束时间应该在当天,或者睡眠段跨越了前一天晚上到当天早上
const isMainSleepPeriod = endDate.isSame(targetDate, 'day') ||
(startDate.isBefore(targetDate, 'day') && endDate.isAfter(targetDate.startOf('day')));
return isMainSleepPeriod;
});
resolve(calculateSleepDuration(filteredSamples));
});
});
}
@@ -634,7 +662,7 @@ export async function fetchHealthDataForDate(date: Date): Promise<TodayHealthDat
fetchHourlyStepSamples(date),
fetchActiveEnergyBurned(options),
fetchBasalEnergyBurned(options),
fetchSleepDuration(options),
fetchSleepDuration(date), // 传入日期而不是options
fetchHeartRateVariability(options),
fetchActivitySummary(options),
fetchOxygenSaturation(options),