|
@@ -1,59 +1,66 @@
|
|
|
import { Component } from '@angular/core';
|
|
|
import { IonicModule } from '@ionic/angular';
|
|
|
-import { FormsModule } from '@angular/forms';
|
|
|
-import { FmodeChatCompletion } from 'fmode-ng';
|
|
|
-import { CommonModule } from '@angular/common';
|
|
|
+import { FormsModule } from '@angular/forms'; // 导入 FormsModule
|
|
|
+import { CommonModule } from '@angular/common'; // 导入 CommonModule
|
|
|
+import { FmodeChatCompletion,MarkdownPreviewModule } from 'fmode-ng'; // 你的外部 API 相关导入
|
|
|
+
|
|
|
@Component({
|
|
|
- selector: 'app-tab2',
|
|
|
- templateUrl: 'tab2.page.html',
|
|
|
- styleUrls: ['tab2.page.scss'],
|
|
|
- standalone: true,
|
|
|
- imports: [
|
|
|
- IonicModule,
|
|
|
- FormsModule,CommonModule // 导入 FormsModule 来支持 ngModel 双向绑定
|
|
|
- ]
|
|
|
+ selector: 'app-tab2',
|
|
|
+ templateUrl: 'tab2.page.html',
|
|
|
+ styleUrls: ['tab2.page.scss'],
|
|
|
+ standalone: true,
|
|
|
+ imports: [
|
|
|
+ IonicModule,
|
|
|
+ FormsModule,
|
|
|
+ CommonModule,
|
|
|
+ MarkdownPreviewModule
|
|
|
+ ]
|
|
|
})
|
|
|
export class Tab2Page {
|
|
|
- // 用户输入数据
|
|
|
- userHeight: number = 170; // 默认身高
|
|
|
- userWeight: number = 65; // 默认体重
|
|
|
- userGoal: string = ''; // 健身目标
|
|
|
-
|
|
|
-
|
|
|
- // 生成的运动计划
|
|
|
- workoutPlan: string = '';
|
|
|
- errorMessage: string = ''; // 错误消息
|
|
|
-
|
|
|
- constructor() {}
|
|
|
+ // 用户输入的变量
|
|
|
+ userWeight: number = 70; // 默认体重
|
|
|
+ userHeight: number = 175; // 默认身高
|
|
|
+ userPreference: string = "跑步"; // 默认运动偏好
|
|
|
+ userGoal: string = "减脂"; // 默认目标
|
|
|
|
|
|
- // 处理用户输入并生成运动计划
|
|
|
- generateWorkoutPlan() {
|
|
|
- // 清空之前的错误消息和运动计划
|
|
|
- this.workoutPlan = '';
|
|
|
- this.errorMessage = '';
|
|
|
+ // 用于显示生成的运动计划
|
|
|
+ responsePlan: string = "";
|
|
|
+isComplete:boolean = false;
|
|
|
+ constructor() {}
|
|
|
|
|
|
- // 检查用户输入是否完整
|
|
|
-
|
|
|
+ ngOnInit() {}
|
|
|
|
|
|
- // 生成模拟运动计划
|
|
|
- this.workoutPlan = this.generateMockWorkoutPlan(this.userGoal);
|
|
|
+ // 当用户输入信息后,点击按钮生成运动计划
|
|
|
+ generatePlan() {
|
|
|
+ // 打印输入的信息,便于调试
|
|
|
+ console.log("用户体重:", this.userWeight);
|
|
|
+ console.log("用户身高:", this.userHeight);
|
|
|
+ console.log("用户运动偏好:", this.userPreference);
|
|
|
+ console.log("用户目标:", this.userGoal);
|
|
|
|
|
|
- if (!this.workoutPlan) {
|
|
|
- this.errorMessage = '无法生成运动计划,请稍后再试。';
|
|
|
- }
|
|
|
- }
|
|
|
+ // 创建一个包含用户输入的消息数组
|
|
|
+ let promptMessage = `根据以下信息,生成一个适合的运动计划:
|
|
|
+ 体重: ${this.userWeight}kg
|
|
|
+ 身高: ${this.userHeight}cm
|
|
|
+ 运动偏好: ${this.userPreference}
|
|
|
+ 目标: ${this.userGoal}`;
|
|
|
|
|
|
- // 模拟根据健身目标生成运动计划
|
|
|
- generateMockWorkoutPlan(goal: string): string {
|
|
|
- switch (goal) {
|
|
|
- case '增肌':
|
|
|
- return '增肌计划:每周进行3次重量训练,结合有氧运动。包括卧推、深蹲、硬拉等基础动作。';
|
|
|
- case '减脂':
|
|
|
- return '减脂计划:每周进行4次高强度间歇训练(HIIT),结合适量的有氧运动,如跑步、游泳等。';
|
|
|
- case '保持体形':
|
|
|
- return '保持体形计划:每周进行3次中等强度的力量训练和2次有氧运动,保持体型的同时增强肌肉耐力。';
|
|
|
- default:
|
|
|
- return '';
|
|
|
- }
|
|
|
- }
|
|
|
+ // 调用 AI 接口生成运动计划
|
|
|
+ let completion = new FmodeChatCompletion([
|
|
|
+ { role: "system", content: "你是一个运动专家,能够根据用户的体重、身高、运动偏好和目标,生成个性化的运动计划。" },
|
|
|
+ { role: "user", content: promptMessage }
|
|
|
+ ]);
|
|
|
+
|
|
|
+ // 发送请求并订阅生成的计划
|
|
|
+ completion.sendCompletion().subscribe((message: any) => {
|
|
|
+ // 打印生成的计划内容,便于调试
|
|
|
+ console.log("生成的运动计划:", message.content);
|
|
|
+
|
|
|
+ // 将生成的计划赋值给组件变量,用于展示
|
|
|
+ this.responsePlan = message.content;
|
|
|
+ if(message?.complete){
|
|
|
+ this.isComplete = true
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
}
|