refactor(sleep): 重构睡眠数据获取逻辑,移除冗余代码并优化组件结构

- 从 healthSlice 和 health.ts 中移除 sleepDuration 字段及相关获取逻辑
- 将 SleepCard 改为按需异步获取睡眠数据,支持传入指定日期
- 睡眠详情页改为通过路由参数接收日期,支持查看历史记录
- 移除 statistics 页面对 sleepDuration 的直接依赖,统一由 SleepCard 管理
- 删除未使用的 SleepStageChart 组件,简化页面结构
This commit is contained in:
richarjiang
2025-09-11 09:08:51 +08:00
parent aee87e8900
commit 62690ee3fc
5 changed files with 43 additions and 181 deletions

View File

@@ -57,7 +57,6 @@ export type TodayHealthData = {
steps: number;
activeEnergyBurned: number; // kilocalories
basalEnergyBurned: number; // kilocalories - 基础代谢率
sleepDuration: number; // 睡眠时长(分钟)
hrv: number | null; // 心率变异性 (ms)
// 健身圆环数据
activeCalories: number;
@@ -442,43 +441,6 @@ async function fetchBasalEnergyBurned(options: HealthDataOptions): Promise<numbe
});
}
async function fetchSleepDuration(date: Date): Promise<number> {
return new Promise((resolve) => {
// 使用睡眠专用的日期范围,包含前一天晚上的睡眠数据
const sleepOptions = createSleepDateRange(date);
AppleHealthKit.getSleepSamples(sleepOptions, (err, res) => {
if (err) {
logError('睡眠数据', err);
return resolve(0);
}
if (!res || !Array.isArray(res) || res.length === 0) {
logWarning('睡眠', '为空或格式错误');
return resolve(0);
}
logSuccess('睡眠', 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));
});
});
}
async function fetchHeartRateVariability(options: HealthDataOptions): Promise<number | null> {
return new Promise((resolve) => {
console.log('=== 开始获取HRV数据 ===');
@@ -626,7 +588,6 @@ function getDefaultHealthData(): TodayHealthData {
steps: 0,
activeEnergyBurned: 0,
basalEnergyBurned: 0,
sleepDuration: 0,
hrv: null,
activeCalories: 0,
activeCaloriesGoal: 350,
@@ -653,7 +614,6 @@ export async function fetchHealthDataForDate(date: Date): Promise<TodayHealthDat
hourlySteps,
activeEnergyBurned,
basalEnergyBurned,
sleepDuration,
hrv,
activitySummary,
oxygenSaturation,
@@ -663,31 +623,17 @@ export async function fetchHealthDataForDate(date: Date): Promise<TodayHealthDat
fetchHourlyStepSamples(date),
fetchActiveEnergyBurned(options),
fetchBasalEnergyBurned(options),
fetchSleepDuration(date), // 传入日期而不是options
fetchHeartRateVariability(options),
fetchActivitySummary(options),
fetchOxygenSaturation(options),
fetchHeartRate(options)
]);
console.log('指定日期健康数据获取完成:', {
steps,
hourlySteps,
activeEnergyBurned,
basalEnergyBurned,
sleepDuration,
hrv,
activitySummary,
oxygenSaturation,
heartRate
});
return {
steps,
hourlySteps,
activeEnergyBurned,
basalEnergyBurned,
sleepDuration,
hrv,
activeCalories: Math.round(activitySummary?.activeEnergyBurned || 0),
activeCaloriesGoal: Math.round(activitySummary?.activeEnergyBurnedGoal || 350),