Просмотр исходного кода

feat: wire today steps/exercise and 30-day sleep history to real Apple Health data

17846405080 1 месяц назад
Родитель
Сommit
703f1c6680

+ 28 - 16
campus_health_app/frontend/campus-health-app/src/app/modules/dashboard/dashboard.component.ts

@@ -286,7 +286,7 @@ export class DashboardComponent {
   }
 
   private loadHealthMetrics() {
-    // 最新一天睡眠 + 恢复分
+    // 最新一天:睡眠 + 运动 + 步数
     this.http.get<any>('/api/health-metrics/latest').subscribe({
       next: (res) => {
         if (res.success && res.data) {
@@ -299,11 +299,19 @@ export class DashboardComponent {
           this.sleepCardData.deepSleepDuration = Number(d.deepSleepHours) || 0;
           this.sleepCardData.sleepStatus = total >= 7 ? '良好' : total >= 6 ? '一般' : '偏少';
           this.sleepCardData.sleepScore = Math.min(100, Math.round(total / 9 * 100));
-          // 详情弹窗
+          this.sleepCardData.avgBedtime = d.metricDate?.slice(0, 10) || '昨晚';
           this.sleepData.totalSleepTime = total;
           this.sleepData.deepSleepDuration = Number(d.deepSleepHours) || 0;
           this.sleepData.remSleepDuration = Number(d.remSleepHours) || 0;
           this.sleepData.lightSleepDuration = Number(d.coreSleepHours) || 0;
+
+          // 今日运动 / 步数(接真实数据)
+          const steps = Number(d.stepCount) || 0;
+          const exMin = Number(d.exerciseMinutes) || 0;
+          this.healthStats.steps = steps.toLocaleString();
+          this.healthStats.stepProgress = Math.round(steps / 10000 * 100);
+          this.healthStats.exerciseMinutes = exMin;
+          this.exerciseData.duration = exMin;
         }
       }
     });
@@ -323,20 +331,24 @@ export class DashboardComponent {
   }
 
   private initMonthlyData() {
-    // 生成30天模拟数据
-    for (let i = 1; i <= 30; i++) {
-      this.monthlySleepData.push({
-        date: `${i}日`,
-        deepSleep: 1.5 + Math.random(),
-        lightSleep: 3.5 + Math.random(),
-        remSleep: 1.0 + Math.random() * 0.5,
-        totalSleep: 0,
-        score: 70 + Math.floor(Math.random() * 30)
-      });
-      // 计算总睡眠
-      const last = this.monthlySleepData[this.monthlySleepData.length - 1];
-      last.totalSleep = last.deepSleep + last.lightSleep + last.remSleep;
-    }
+    // 从 API 拉最近 30 天真实睡眠数据
+    this.http.get<any>('/api/health-metrics/recent?days=30').subscribe({
+      next: (res) => {
+        if (res.success && Array.isArray(res.data)) {
+          this.monthlySleepData = res.data
+            .filter((r: any) => Number(r.sleepDurationHours) > 0)
+            .map((r: any) => ({
+              date: r.metricDate?.slice(5, 10) || '',
+              deepSleep: Number(r.deepSleepHours) || 0,
+              lightSleep: Number(r.coreSleepHours) || 0,
+              remSleep: Number(r.remSleepHours) || 0,
+              totalSleep: Number(r.sleepDurationHours) || 0,
+              score: Math.min(100, Math.round((Number(r.sleepDurationHours) || 0) / 9 * 100))
+            }));
+          this.updateSleepChart();
+        }
+      }
+    });
   }
 
   // 切换睡眠视图