|
@@ -4,10 +4,10 @@ import { FormsModule } from '@angular/forms';
|
|
|
import { CommonModule } from '@angular/common';
|
|
|
import { TagInputComponent } from '../tag-input/tag-input.component';
|
|
|
import { addIcons } from 'ionicons';
|
|
|
-import { barbellOutline, personOutline, square, alarmOutline, arrowBack } from 'ionicons/icons';
|
|
|
+import { barbellOutline, closeCircleOutline, checkmarkCircleOutline, fitness, personOutline, square, alarmOutline, arrowBack } from 'ionicons/icons';
|
|
|
import { DalleOptions, FmodeChatCompletion, MarkdownPreviewModule } from 'fmode-ng';
|
|
|
import { LoadingController } from '@ionic/angular';
|
|
|
-import { AlertController, IonThumbnail, IonBadge, IonButton, IonButtons, IonCard, IonCardContent, IonCardHeader, IonCardSubtitle, IonCardTitle, IonCol, IonContent, IonGrid, IonHeader, IonIcon, IonImg, IonInput, IonItem, IonLabel, IonList, IonRow, IonSegment, IonSegmentButton, IonSelect, IonSelectOption, IonToolbar, IonTitle, IonSpinner, IonTextarea } from '@ionic/angular/standalone';
|
|
|
+import { AlertController, IonThumbnail, IonBadge, IonButton, IonButtons, IonCard, IonCardContent, IonCardHeader, IonCardSubtitle, IonCardTitle, IonCol, IonContent, IonGrid, IonHeader, IonIcon, IonImg, IonInput, IonItem, IonLabel, IonList, IonRow, IonSegment, IonSegmentButton, IonSelect, IonSelectOption, IonToolbar, IonTitle, IonSpinner, IonTextarea, IonProgressBar } from '@ionic/angular/standalone';
|
|
|
import { NavController } from '@ionic/angular';
|
|
|
import { Router } from '@angular/router';
|
|
|
import { CloudObject, CloudQuery, CloudUser } from 'src/lib/ncloud';
|
|
@@ -51,7 +51,8 @@ import { IonBackButton } from '@ionic/angular/standalone';
|
|
|
IonLabel,
|
|
|
IonBadge,
|
|
|
IonInput,
|
|
|
- TagInputComponent
|
|
|
+ TagInputComponent,
|
|
|
+ IonProgressBar
|
|
|
] // 添加 CommonModule
|
|
|
})
|
|
|
export class TestPageComponent implements OnInit {
|
|
@@ -64,7 +65,9 @@ export class TestPageComponent implements OnInit {
|
|
|
goalDescription: string = '';
|
|
|
generatedPlan: string = ''; // 用来存储生成的健身计划
|
|
|
isGenerating: boolean = false; // 是否正在生成计划
|
|
|
+ isRevert: boolean = false
|
|
|
messageList: any[] = []; // 存储消息列表
|
|
|
+ planReady: boolean = false; // 是否可以显示确认按钮
|
|
|
constructor(private toastController: ToastController,
|
|
|
private loadingController: LoadingController,
|
|
|
private alertController: AlertController,
|
|
@@ -72,7 +75,7 @@ export class TestPageComponent implements OnInit {
|
|
|
private router: Router,
|
|
|
private navCtrl: NavController
|
|
|
) {
|
|
|
- addIcons({ personOutline, barbellOutline, alarmOutline, square, arrowBack });
|
|
|
+ addIcons({ fitness, personOutline, closeCircleOutline, barbellOutline, alarmOutline, square, arrowBack, checkmarkCircleOutline, });
|
|
|
}
|
|
|
ngOnInit() { }
|
|
|
|
|
@@ -123,15 +126,10 @@ export class TestPageComponent implements OnInit {
|
|
|
await this.showToast('身高或体重为空,无法生成个人健身计划');
|
|
|
return; // 退出函数,不继续生成健身计划
|
|
|
}
|
|
|
- const loading = await this.loadingController.create({
|
|
|
- message: '生成计划中...',
|
|
|
- duration: 30000 // 加载时长可以根据实际需求调整
|
|
|
- });
|
|
|
- await loading.present();
|
|
|
this.generatedPlan = '';
|
|
|
this.messageList = [];
|
|
|
this.isGenerating = true;
|
|
|
-
|
|
|
+ this.isRevert = true
|
|
|
console.log('生成健身计划', {
|
|
|
tags: this.selectedTags,
|
|
|
exercisePreference: this.exercisePreference,
|
|
@@ -143,7 +141,7 @@ export class TestPageComponent implements OnInit {
|
|
|
});
|
|
|
|
|
|
let prompt = `您作为一名专业的健身计划定制大师,请帮我根据以下情况制定健身计划(健身计划请给每天的运动标上序号)。关键词:${this.selectedTags.join(",")},目标描述:${this.goalDescription},运动偏好:${this.exercisePreference},
|
|
|
- 健身频率每周:${this.workoutFrequency},身高:${this.height}cm,体重:${this.weight}kg,年龄:${this.age},注意:只需给出训练日计划,训练日只做四个运动`;
|
|
|
+ 健身频率每周:${this.workoutFrequency},身高:${this.height}cm,体重:${this.weight}kg,年龄:${this.age},注意:训练部位给出具体部位(如胸部,腿部),具体次数,组数。一般一天一个部位。具体计划只需给出训练日计划,训练日只做四个运动`;
|
|
|
|
|
|
let messageList = new FmodeChatCompletion([
|
|
|
{ role: "system", content: '' },
|
|
@@ -153,11 +151,9 @@ export class TestPageComponent implements OnInit {
|
|
|
messageList.sendCompletion().subscribe((message: any) => {
|
|
|
console.log(message.content);
|
|
|
this.generatedPlan = message.content;
|
|
|
-
|
|
|
if (message?.complete) {
|
|
|
this.isGenerating = false;
|
|
|
this.getJson()
|
|
|
- loading.dismiss();
|
|
|
}
|
|
|
});
|
|
|
}
|
|
@@ -168,13 +164,14 @@ export class TestPageComponent implements OnInit {
|
|
|
planArray: any
|
|
|
JSONcomplete: boolean = false
|
|
|
JSONdes = ""
|
|
|
+
|
|
|
JSONobject: { [key: string]: string } = {}
|
|
|
getJson() {
|
|
|
let promt = `请你以以下json格式分每天来整合文段内容:
|
|
|
{
|
|
|
"date":"周几运动",
|
|
|
"srcId": "plan001等等",
|
|
|
- "trainingPart":"训练部位",
|
|
|
+ "trainingPart":"训练部位如(胸部,腿部)",
|
|
|
"trainingItems":"四个运动项目,以下面形式给出,形式如下"[
|
|
|
{
|
|
|
"item": "引体向上",
|
|
@@ -213,8 +210,9 @@ export class TestPageComponent implements OnInit {
|
|
|
console.log("json:", this.JSONdes);
|
|
|
this.generatedPlan = message.content;
|
|
|
this.planArray = extractAllJsonFromString(this.JSONdes);
|
|
|
+ this.isRevert = false
|
|
|
+ this.planReady = true;
|
|
|
console.log(this.planArray)
|
|
|
- this.showConfirmationAlert();
|
|
|
// planArray.forEach((plan: any) => {
|
|
|
// plan.detail = this.generatedPlan; // 添加额外的计划内容
|
|
|
// console.log(plan);
|
|
@@ -243,63 +241,38 @@ export class TestPageComponent implements OnInit {
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
- async showConfirmationAlert() {
|
|
|
- const alert = await this.alertController.create({
|
|
|
- header: '确认生成计划',
|
|
|
- message: ` <div style="white-space: pre-wrap;">
|
|
|
- <!-- 使用 fm-markdown-preview 渲染 Markdown 格式的内容 -->
|
|
|
- <fm-markdown-preview class="content-style" [content]="generatedPlan"></fm-markdown-preview>
|
|
|
- </div>`,
|
|
|
- buttons: [
|
|
|
- {
|
|
|
- text: '放弃',
|
|
|
- role: 'cancel',
|
|
|
- handler: () => {
|
|
|
- console.log('用户选择放弃');
|
|
|
- }
|
|
|
- },
|
|
|
- {
|
|
|
- text: '确认',
|
|
|
- handler: () => {
|
|
|
- // 这里可以调用 API 将数据存到数据库
|
|
|
- console.log('用户确认', this.generatedPlan);
|
|
|
- this.savePlanToDatabase(this.generatedPlan);
|
|
|
- }
|
|
|
- }
|
|
|
- ]
|
|
|
- });
|
|
|
- await alert.present();
|
|
|
+ confirmPlan() {
|
|
|
+ this.savePlanToDatabase(this.generatedPlan);
|
|
|
+ }
|
|
|
+ discardPlan() {
|
|
|
+ this.generatedPlan = " "
|
|
|
}
|
|
|
async savePlanToDatabase(plan: string) {
|
|
|
|
|
|
- let currentUser = new CloudUser().toPointer(); // 获取当前用户实例
|
|
|
+ let currentUser = new CloudUser().toPointer();
|
|
|
console.log(currentUser)
|
|
|
console.log('计划保存到数据库', plan);
|
|
|
-
|
|
|
- // 查询当前用户的所有计划
|
|
|
const cloudQuery = new CloudQuery("fitPlan");
|
|
|
- // 过滤出与当前用户关联的所有计划
|
|
|
cloudQuery.equalTo("user", currentUser)
|
|
|
try {
|
|
|
- const userPlans = await cloudQuery.find(); // 获取当前用户的所有计划
|
|
|
+ const userPlans = await cloudQuery.find();
|
|
|
console.log("找到的计划数量:", userPlans.length);
|
|
|
|
|
|
// 删除当前用户的所有计划
|
|
|
for (const planObj of userPlans) {
|
|
|
- await planObj.destroy(); // 删除每个计划
|
|
|
+ await planObj.destroy();
|
|
|
console.log("已删除计划:", planObj);
|
|
|
}
|
|
|
this.planArray.forEach((plan: any) => {
|
|
|
- plan.detail = this.generatedPlan; // 添加额外的计划内容
|
|
|
+ plan.detail = this.generatedPlan;
|
|
|
console.log(plan);
|
|
|
-
|
|
|
// 准备插入数据库的数据对象
|
|
|
let newPlan = {
|
|
|
"date": plan['date'],
|
|
|
"srcId": plan['srcId'],
|
|
|
"trainingPart": plan['trainingPart'],
|
|
|
"trainingItems": plan['trainingItems'],
|
|
|
- "user": currentUser, // 将当前用户指针关联到计划
|
|
|
+ "user": currentUser,
|
|
|
};
|
|
|
// 创建新的 CloudObject 实例
|
|
|
let newObject = new CloudObject("fitPlan");
|
|
@@ -307,6 +280,7 @@ export class TestPageComponent implements OnInit {
|
|
|
newObject.save()
|
|
|
.then(() => {
|
|
|
console.log("计划保存成功");
|
|
|
+ this.goToPage("tab2")
|
|
|
})
|
|
|
.catch((error: any) => {
|
|
|
console.error("保存计划时发生错误", error);
|
|
@@ -316,7 +290,6 @@ export class TestPageComponent implements OnInit {
|
|
|
} catch (error) {
|
|
|
console.error("查询或删除计划时发生错误", error);
|
|
|
}
|
|
|
- this.goToPage("tab2")
|
|
|
}
|
|
|
|
|
|
goBack() {
|