|
@@ -4,7 +4,7 @@ import { addIcons } from 'ionicons';
|
|
|
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, IonItemOptions, 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, IonItemSliding, IonItemOption } from '@ionic/angular/standalone';
|
|
|
+import { IonDatetime, IonSelect, IonItemOptions, 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, IonItemSliding, IonItemOption, ToastController } from '@ionic/angular/standalone';
|
|
|
import { FmodeChatCompletion, ImagineWork, DalleOptions, ChatPanelOptions, FmodeChat, FmodeChatMessage, MarkdownPreviewModule, openChatPanelModal } from "fmode-ng";
|
|
|
import { AgentTaskStep } from './agent/agent.task';
|
|
|
import { TaskPoemPictureDesc } from './agent/tasks/poem/poem-desc';
|
|
@@ -60,6 +60,7 @@ import { TestPageComponent } from './test-page/test-page.component';
|
|
|
]
|
|
|
})
|
|
|
export class Tab2Page implements OnInit {
|
|
|
+
|
|
|
selectedTab: string = 'checkin'; // 默认选中的tab
|
|
|
planList: any[] = [];
|
|
|
coachList: any[] = [];
|
|
@@ -67,7 +68,7 @@ export class Tab2Page implements OnInit {
|
|
|
currentUser: CloudUser | undefined
|
|
|
actionTaskVisible = false;
|
|
|
healthTaskVisible = false;
|
|
|
- constructor(private router: Router, private modalCtrl: ModalController, private cdr: ChangeDetectorRef, private alertController: AlertController) {
|
|
|
+ constructor(private toastController: ToastController, private router: Router, private modalCtrl: ModalController, private cdr: ChangeDetectorRef, private alertController: AlertController) {
|
|
|
addIcons({ alertCircleOutline, sunny, checkmarkCircle, calendar, helpCircle, trash, create, logoGitlab, bicycleOutline, infiniteOutline });
|
|
|
this.currentUser = new CloudUser();
|
|
|
}
|
|
@@ -102,13 +103,33 @@ export class Tab2Page implements OnInit {
|
|
|
let query = new CloudQuery("Coach");
|
|
|
this.coachList = await query.find();
|
|
|
}
|
|
|
+
|
|
|
+ dailyReward = 5;
|
|
|
+ powerForMakup = 30;
|
|
|
+ power: number = 0;
|
|
|
+ realDate: Date = this.correctDate(new Date());
|
|
|
+ days: number = 0; // 总打卡天数
|
|
|
+ consecutiveDays: number = 0; // 连续打卡天数
|
|
|
+ checkInHistory: Set<string> = new Set(); // 已打卡日期集合
|
|
|
+
|
|
|
+ async showToast(message: string, color: string = 'success') {
|
|
|
+ const toast = await this.toastController.create({
|
|
|
+ message: message,
|
|
|
+ duration: 2000,
|
|
|
+ position: 'top',
|
|
|
+ color: color,
|
|
|
+ });
|
|
|
+ toast.present();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 计算 BMI
|
|
|
calculateBMI(height: number, weight: number): number {
|
|
|
- // 身高转换为米
|
|
|
const heightInMeters = height / 100;
|
|
|
- // 计算 BMI
|
|
|
const bmi = weight / (heightInMeters * heightInMeters);
|
|
|
return parseFloat(bmi.toFixed(2));
|
|
|
}
|
|
|
+
|
|
|
+ // 获取鼓励性话语
|
|
|
getEncouragement(bmi: number): string {
|
|
|
if (bmi < 18.5) {
|
|
|
return '您的BMI偏低,注意保持健康的饮食哦!(๑•́ ₃ •̀๑)';
|
|
@@ -121,28 +142,23 @@ export class Tab2Page implements OnInit {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- 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();
|
|
|
- // 假设从数据库加载已打卡的日期列表
|
|
|
const checkedDays = this.planUser[0].get("checkeddays") || [];
|
|
|
checkedDays.forEach((date: string) => {
|
|
|
- this.checkInHistory.add(date); // 加载已打卡日期到 checkInHistory
|
|
|
+ this.checkInHistory.add(date);
|
|
|
});
|
|
|
- console.log(this.checkInHistory)
|
|
|
- this.days = this.planUser[0].get("days"); // 获取已打卡天数
|
|
|
- this.consecutiveDays = this.planUser[0].sucdays; // 获取连续打卡天数
|
|
|
+ this.days = this.planUser[0].get("days");
|
|
|
+ this.consecutiveDays = this.planUser[0].sucdays;
|
|
|
+ this.power = this.planUser[0].get('power');
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
// 计算连续打卡天数
|
|
|
calculateConsecutiveDays() {
|
|
|
let currentDate = this.realDate;
|
|
@@ -160,49 +176,138 @@ export class Tab2Page implements OnInit {
|
|
|
this.consecutiveDays = count;
|
|
|
}
|
|
|
|
|
|
+ // 格式化日期
|
|
|
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') {
|
|
|
+ } else if (typeof date === 'string') {
|
|
|
const parsedDate = new Date(date);
|
|
|
return new Date(parsedDate.getTime() - parsedDate.getTimezoneOffset() * 60000);
|
|
|
}
|
|
|
throw new Error("Invalid date format");
|
|
|
}
|
|
|
|
|
|
+ // 判断是否今天已打卡
|
|
|
+ isCheckedInToday(): boolean {
|
|
|
+ const currentDate = this.realDate;
|
|
|
+ const formattedDate = this.formatDate(currentDate);
|
|
|
+ return this.checkInHistory.has(formattedDate);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取连续打卡奖励
|
|
|
+ getConsecutiveReward(consecutiveDays: number): number {
|
|
|
+ if (consecutiveDays % 30 === 0) {
|
|
|
+ return 30; // 第30天,奖励30
|
|
|
+ } else if (consecutiveDays % 15 === 0) {
|
|
|
+ return 20; // 第15天,奖励20
|
|
|
+ } else if (consecutiveDays % 7 === 0) {
|
|
|
+ return 10; // 第7天,奖励10
|
|
|
+ }
|
|
|
+ return 0; // 不符合任何条件时不奖励
|
|
|
+ }
|
|
|
+
|
|
|
+ // 打卡操作
|
|
|
async markAttendance() {
|
|
|
const currentDate = this.realDate;
|
|
|
const formattedDate = this.formatDate(currentDate);
|
|
|
- if (!this.checkInHistory.has(formattedDate)) {
|
|
|
- this.checkInHistory.add(formattedDate);
|
|
|
- this.days = this.checkInHistory.size;
|
|
|
- this.calculateConsecutiveDays();
|
|
|
+
|
|
|
+ // 如果今天已经打卡
|
|
|
+ if (this.checkInHistory.has(formattedDate)) {
|
|
|
+ this.showToast('今天已经打卡过了', 'warning');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果是未来日期
|
|
|
+ if (currentDate > this.correctDate(new Date())) {
|
|
|
+ this.showToast('不能打卡未来的日期', 'danger');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 正常打卡
|
|
|
+ this.checkInHistory.add(formattedDate);
|
|
|
+ this.days = this.checkInHistory.size;
|
|
|
+ this.calculateConsecutiveDays();
|
|
|
+
|
|
|
+ let currentUser = new CloudUser();
|
|
|
+ const cloudQuery = new CloudQuery("fitUser");
|
|
|
+ cloudQuery.equalTo("user", currentUser.toPointer());
|
|
|
+ const userData = await cloudQuery.find();
|
|
|
+
|
|
|
+ if (userData.length > 0) {
|
|
|
+ const user = userData[0];
|
|
|
+ let checkedDays = user.get("checkeddays") || [];
|
|
|
+ checkedDays.push(formattedDate);
|
|
|
+ user.set({ "checkeddays": checkedDays });
|
|
|
+ user.set({ "days": this.days });
|
|
|
+ user.set({ "sucdays": this.consecutiveDays });
|
|
|
+
|
|
|
+ // 计算奖励
|
|
|
+ let totalReward = this.dailyReward; // 每天签到奖励
|
|
|
+ totalReward += this.getConsecutiveReward(this.consecutiveDays); // 连续签到奖励
|
|
|
+ user.set({ "power": user.get("power") + totalReward }); // 增加总 power 奖励
|
|
|
+
|
|
|
+ await user.save();
|
|
|
+ this.loadPlanUser();
|
|
|
+ this.showToast('打卡成功,获得了 ' + totalReward + ' Power');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 补签操作
|
|
|
+ async handleMakeupSignIn(user: CloudUser): Promise<string> {
|
|
|
+ if (user.get("power") >= this.powerForMakup) {
|
|
|
+ user.set({ "power": user.get("power") - this.powerForMakup });
|
|
|
+ await user.save();
|
|
|
+ return '补签成功!';
|
|
|
+ } else {
|
|
|
+ return '补签失败,您的动能不足!';
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 补签点击事件
|
|
|
+ async handleMakeupClick() {
|
|
|
+ const confirmed = window.confirm('补签将消耗 ' + this.powerForMakup + ' 动能,确定补签吗?');
|
|
|
+ if (confirmed) {
|
|
|
let currentUser = new CloudUser();
|
|
|
const cloudQuery = new CloudQuery("fitUser");
|
|
|
cloudQuery.equalTo("user", currentUser.toPointer());
|
|
|
const userData = await cloudQuery.find();
|
|
|
+
|
|
|
if (userData.length > 0) {
|
|
|
const user = userData[0];
|
|
|
- let checkedDays = user.get("checkeddays") || [];
|
|
|
- checkedDays.push(formattedDate);
|
|
|
- user.set({ "checkeddays": checkedDays });
|
|
|
- user.set({ "days": this.days });
|
|
|
- user.set({ "sucdays": this.consecutiveDays });
|
|
|
- await user.save();
|
|
|
- this.loadPlanUser()
|
|
|
+ const resultMessage = await this.handleMakeupSignIn(user);
|
|
|
+ this.showToast(resultMessage, resultMessage.includes('成功') ? 'success' : 'danger');
|
|
|
+ this.loadPlanUser();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ // 日期变化时更新
|
|
|
onDateChange(event: any) {
|
|
|
this.realDate = new Date(this.correctDate(event.detail.value));
|
|
|
}
|
|
|
+
|
|
|
+ // 获取按钮状态(判断打卡、补签等)
|
|
|
+ getButtonState(date: Date): { isDisabled: boolean, buttonText: string } {
|
|
|
+ const formattedDate = this.formatDate(date);
|
|
|
+
|
|
|
+ if (formattedDate > this.formatDate(this.correctDate(new Date()))) {
|
|
|
+ // 未来日期
|
|
|
+ return { isDisabled: true, buttonText: '无法签到' };
|
|
|
+ } else if (this.checkInHistory.has(formattedDate)) {
|
|
|
+ // 已经打卡过
|
|
|
+ return { isDisabled: true, buttonText: '今天已经打卡' };
|
|
|
+ } else if (formattedDate < this.formatDate(this.correctDate(new Date()))) {
|
|
|
+ // 过去的日期
|
|
|
+ return { isDisabled: false, buttonText: '补签' };
|
|
|
+ }
|
|
|
+ return { isDisabled: false, buttonText: '打卡' };
|
|
|
+ }
|
|
|
+
|
|
|
async login() {
|
|
|
let user = await openUserLoginModal(this.modalCtrl);
|
|
|
if (user?.id) {
|