xukang 4 ヶ月 前
コミット
37602ec314

+ 2 - 1
TFPower-app/src/app/tab2/tab2.page.html

@@ -56,7 +56,8 @@
           <ion-card-title>打卡区域</ion-card-title>
         </ion-card-header>
         <ion-card-content>
-          <ion-datetime displayFormat="YYYY-MM-DD" [value]="selectedDate" (ionChange)="onDateChange($event)">
+
+          <ion-datetime [value]="realDate.toISOString()" (ionChange)="onDateChange($event)">
           </ion-datetime>
           <div class="card-info">
             <p><strong>已打卡天数:</strong>{{ user.get('days') }}天</p>

+ 12 - 4
TFPower-app/src/app/tab2/tab2.page.scss

@@ -240,15 +240,23 @@ ion-content {
 .checkin .user-info {
   flex: 1; /* 使信息部分占满剩余空间 */
 }
-.checkin .card-info {
+
+.checkin .card-info p {
+  margin: 5px 0;
+}
+.datetime-container {
   display: flex;
   flex-direction: column;
   align-items: center;
-  padding: 15px 0;
+  margin-bottom: 20px;
 }
 
-.checkin .card-info p {
-  margin: 5px 0;
+.weather-icon-container {
+  margin-bottom: 10px; /* 图标与日期选择器之间的间距 */
+}
+
+.weather-icon-container ion-icon {
+  color: #ffcc00; /* 太阳图标的颜色 */
 }
 ion-card {
     border-radius: 15px; /* 圆角边框 */

+ 40 - 30
TFPower-app/src/app/tab2/tab2.page.ts

@@ -1,7 +1,7 @@
 import { Component, OnInit, ChangeDetectorRef } from '@angular/core';
 import { Router } from '@angular/router';
 import { addIcons } from 'ionicons';
-import { checkmarkCircle, infiniteOutline, alertCircleOutline, bicycleOutline, logoGitlab, trash, calendar, helpCircle, create } from 'ionicons/icons';
+import { checkmarkCircle, sunny, infiniteOutline, alertCircleOutline, bicycleOutline, logoGitlab, trash, calendar, helpCircle, create } from 'ionicons/icons';
 import { CommonModule } from '@angular/common';
 import { FormsModule } from '@angular/forms';
 import { IonDatetime, IonSelect, IonThumbnail, IonCardSubtitle, IonImg, IonCard, IonButtons, IonItem, IonList, IonHeader, IonIcon, IonToolbar, IonContent, IonSegment, IonSegmentButton, IonGrid, IonRow, IonCol, IonButton, IonLabel, IonBadge, IonInput, ModalController, IonCardTitle, IonCardContent, IonCardHeader, IonSelectOption } from '@ionic/angular/standalone';
@@ -59,10 +59,10 @@ export class Tab2Page implements OnInit {
   selectedTab: string = 'checkin';  // 默认选中的tab
   planList: any[] = [];
   coachList: any[] = [];
-  planUser: any;
+  planUser: any[] = [];
   currentUser: CloudUser | undefined
   constructor(private router: Router, private modalCtrl: ModalController, private cdr: ChangeDetectorRef, private alertController: AlertController) {
-    addIcons({ alertCircleOutline, checkmarkCircle, calendar, helpCircle, trash, create, logoGitlab, bicycleOutline, infiniteOutline });
+    addIcons({ alertCircleOutline, sunny, checkmarkCircle, calendar, helpCircle, trash, create, logoGitlab, bicycleOutline, infiniteOutline });
     this.currentUser = new CloudUser();
   }
   ngOnInit() {
@@ -96,44 +96,43 @@ export class Tab2Page implements OnInit {
     let query = new CloudQuery("Coach");
     this.coachList = await query.find();
   }
+  getEncouragement(bmi: number): string {
+    if (bmi < 18.5) {
+      return '您的BMI偏低,注意保持健康的饮食哦!(๑•́ ₃ •̀๑)';
+    } else if (bmi >= 18.5 && bmi < 24.9) {
+      return '您的BMI在正常范围,继续保持良好的生活方式!(。♥‿♥。)';
+    } else if (bmi >= 25 && bmi < 29.9) {
+      return '您的BMI稍微偏高,可以增加运动,控制饮食!(≧◡≦)';
+    } else {
+      return '您的BMI较高,建议积极锻炼,控制体重!(。•́︿•̀。)';
+    }
+  }
+
+
+  realDate: Date = this.correctDate(new Date());
+  days: number = 0; // 总打卡天数
+  consecutiveDays: number = 0; // 连续打卡天数
+  checkInHistory: Set<string> = new Set(); // 已打卡日期集合
+  //打卡页面
   async loadPlanUser() {
     let currentUser = new CloudUser();
     const cloudQuery = new CloudQuery("fitUser");
     if (currentUser) {
       cloudQuery.equalTo("user", currentUser.toPointer());
       this.planUser = await cloudQuery.find();
-      console.log(this.planUser);
-
       // 假设从数据库加载已打卡的日期列表
-      const checkedDays = this.planUser[0].checkeddays || [];
+      const checkedDays = this.planUser[0].get("checkeddays") || [];
       checkedDays.forEach((date: string) => {
         this.checkInHistory.add(date); // 加载已打卡日期到 checkInHistory
       });
       console.log(this.checkInHistory)
-      this.days = this.planUser[0].days; // 获取已打卡天数
+      this.days = this.planUser[0].get("days"); // 获取已打卡天数
       this.consecutiveDays = this.planUser[0].sucdays; // 获取连续打卡天数
     }
   }
-
-  selectedDate: Date = new Date();
-  days: number = 0; // 总打卡天数
-  consecutiveDays: number = 0; // 连续打卡天数
-  checkInHistory: Set<string> = new Set(); // 已打卡日期集合
-  //打卡页面
-  getEncouragement(bmi: number): string {
-    if (bmi < 18.5) {
-      return '您的BMI偏低,注意保持健康的饮食哦!(๑•́ ₃ •̀๑)';
-    } else if (bmi >= 18.5 && bmi < 24.9) {
-      return '您的BMI在正常范围,继续保持良好的生活方式!(。♥‿♥。)';
-    } else if (bmi >= 25 && bmi < 29.9) {
-      return '您的BMI稍微偏高,可以增加运动,控制饮食!(≧◡≦)';
-    } else {
-      return '您的BMI较高,建议积极锻炼,控制体重!(。•́︿•̀。)';
-    }
-  }
   // 计算连续打卡天数
   calculateConsecutiveDays() {
-    let currentDate = new Date();
+    let currentDate = this.realDate;
     let count = 0;
     for (let i = 0; i < 30; i++) {
       let date = new Date(currentDate);
@@ -149,11 +148,24 @@ export class Tab2Page implements OnInit {
   }
 
   formatDate(date: Date): string {
+
     return date.toISOString().split('T')[0];
   }
+  correctDate(date: string | Date): Date {
+    // 如果传入的已经是一个 Date 对象
+    if (date instanceof Date) {
+      return new Date(date.getTime() - date.getTimezoneOffset() * 60000);
+    }
+    // 如果传入的是字符串(来自 ion-datetime),我们先将它转换为 Date 对象
+    else if (typeof date === 'string') {
+      const parsedDate = new Date(date);
+      return new Date(parsedDate.getTime() - parsedDate.getTimezoneOffset() * 60000);
+    }
+    throw new Error("Invalid date format");
+  }
 
   async markAttendance() {
-    const currentDate = new Date();
+    const currentDate = this.realDate;
     const formattedDate = this.formatDate(currentDate);
     if (!this.checkInHistory.has(formattedDate)) {
       this.checkInHistory.add(formattedDate);
@@ -166,7 +178,7 @@ export class Tab2Page implements OnInit {
       const userData = await cloudQuery.find();
       if (userData.length > 0) {
         const user = userData[0];
-        let checkedDays = user.checkeddays || [];
+        let checkedDays = user.get("checkeddays") || [];
         checkedDays.push(formattedDate); // 添加新的打卡日期
 
         // 更新数据库中的 checkeddays 字段
@@ -181,10 +193,8 @@ export class Tab2Page implements OnInit {
 
   // 处理日期变更
   onDateChange(event: any) {
-    this.selectedDate = event.detail.value;
+    this.realDate = new Date(this.correctDate(event.detail.value));
   }
-
-
   async login() {
     let user = await openUserLoginModal(this.modalCtrl);
     if (user?.id) {