tab1.page.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. import { Component, OnInit } from '@angular/core';
  2. import { Router } from '@angular/router';
  3. import { CloudSeUser } from 'src/lib/cloudSeuser'; // 引入 CloudSeUser 类
  4. import { FmodeChatCompletion } from 'fmode-ng'; // 引入 FmodeChatCompletion
  5. import { addIcons } from 'ionicons';
  6. import { albumsOutline, documentOutline, leafOutline, scanOutline, storefrontOutline } from 'ionicons/icons';
  7. import { IonButton, IonCard, IonCardContent, IonCardHeader, IonCol, IonContent, IonHeader, IonIcon, IonInput, IonRow, IonTextarea, IonTitle, IonToolbar, IonGrid, IonCardTitle, IonSearchbar } from '@ionic/angular/standalone'; // 导入 Ionic 组件
  8. import { CommonModule } from '@angular/common'; // 导入 CommonModule
  9. import { ImagePopupComponent } from '../image-popup/image-popup.component'; // 导入弹窗组件
  10. import { ModalController, NavController } from '@ionic/angular/standalone';
  11. import { Image1PopupComponent } from '../image-popup/image1-popup/image1-popup.component';
  12. import { Image2PopupComponent } from '../image-popup/image2-popup/image2-popup.component';
  13. import { Image3PopupComponent } from '../image-popup/image3-popup/image3-popup.component';
  14. import { Image4PopupComponent } from '../image-popup/image4-popup/image4-popup.component';
  15. @Component({
  16. selector: 'app-tab1',
  17. templateUrl: 'tab1.page.html',
  18. styleUrls: ['tab1.page.scss'],
  19. standalone: true, // 使用 standalone 组件
  20. imports: [
  21. CommonModule, IonContent, IonHeader, IonTitle, IonToolbar,
  22. IonButton, IonTextarea, IonInput, IonCard, IonCardContent, IonGrid, IonRow, IonCol, IonIcon,
  23. IonCardHeader, IonCardTitle, IonSearchbar,
  24. ],
  25. })
  26. export class Tab1Page implements OnInit {
  27. private cloudSeUser: CloudSeUser; // 引入 CloudSeUser 实例
  28. userInfo: any = null; // 用户信息
  29. responseMsg: string = ""; // 用于存储 AI 生成的饮食建议
  30. recipeMsg: string = ""; // 用于存储 AI 生成的推荐食谱
  31. // 存储图片的数组
  32. images = [
  33. 'https://app.fmode.cn/dev/jxnu/202226701038/ssbt2022.jpg',
  34. 'https://app.fmode.cn/dev/jxnu/202226701038/ssbt2021.jpg',
  35. '',
  36. ''
  37. ];
  38. // 每张图片的描述
  39. descriptions = [
  40. '<p><strong>第一张图片</strong></p>',
  41. '<p><strong>第二张图片</strong></p>',
  42. '<p><strong>第三张图片</strong></p>',
  43. '<p><strong>第四张图片</strong></p>'
  44. ];
  45. // 当前显示的幻灯片索引
  46. currentSlide: number = 0;
  47. constructor(private router: Router, private modalCtrl: ModalController) {
  48. addIcons({ scanOutline, documentOutline, storefrontOutline, albumsOutline, leafOutline });
  49. this.cloudSeUser = new CloudSeUser();
  50. }
  51. ngOnInit(): void {
  52. this.loadUserData(); // 页面初始化时加载用户数据
  53. }
  54. // 从 seUser 表加载当前用户数据
  55. async loadUserData() {
  56. try {
  57. const userData = await this.cloudSeUser.getCurrentUserInfo();
  58. if (userData) {
  59. // 使用 get 方法逐个提取字段
  60. this.userInfo = {
  61. name: userData.get('name') || '',
  62. email: userData.get('email') || '',
  63. phone: userData.get('phone') || '',
  64. age: userData.get('age') || null,
  65. gender: userData.get('gender') || '',
  66. height: userData.get('height') || null,
  67. weight: userData.get('weight') || null,
  68. activityLevel: userData.get('activityLevel') || '',
  69. dietPreference: userData.get('dietPreference') || '',
  70. dietGroup: userData.get('dietGroup') || '',
  71. avatar: userData.get('avatar') || null,
  72. allergies: userData.get('allergies') || '',
  73. };
  74. }
  75. } catch (error) {
  76. console.error('加载用户数据失败', error);
  77. }
  78. }
  79. // 获取健康建议
  80. async goHealthTips() {
  81. if (!this.userInfo) return;
  82. const { height, weight, activityLevel, dietPreference, dietGroup } = this.userInfo;
  83. const newPrompt = `
  84. 你是一名专业的饮食营养规划师。根据以下用户信息,请提供今日的饮食建议:
  85. 身高:${height} cm
  86. 体重:${weight} kg
  87. 活动水平:${activityLevel}
  88. 饮食偏好:${dietPreference}
  89. 饮食类型:${dietGroup}
  90. `;
  91. const completion = new FmodeChatCompletion([
  92. { role: "system", content: "" },
  93. { role: "user", content: newPrompt }
  94. ]);
  95. completion.sendCompletion().subscribe((message: any) => {
  96. console.log(message.content);
  97. this.responseMsg = message.content; // 更新健康建议
  98. });
  99. }
  100. // 查看推荐食谱
  101. async viewRecommendedRecipes() {
  102. if (!this.userInfo) return;
  103. const { height, weight, activityLevel, dietPreference, dietGroup } = this.userInfo;
  104. const newPrompt = `
  105. 你是一名专业的饮食营养规划师。根据以下用户信息,请推荐今日的食谱:
  106. 身高:${height} cm
  107. 体重:${weight} kg
  108. 活动水平:${activityLevel}
  109. 饮食偏好:${dietPreference}
  110. 饮食类型:${dietGroup}
  111. `;
  112. const completion = new FmodeChatCompletion([
  113. { role: "system", content: "" },
  114. { role: "user", content: newPrompt }
  115. ]);
  116. completion.sendCompletion().subscribe((message: any) => {
  117. console.log(message.content);
  118. this.recipeMsg = message.content; // 更新推荐食谱
  119. });
  120. }
  121. // 打开弹窗
  122. async openImagePopup(imageUrl: string, description: string) {
  123. let modal;
  124. // 根据图片的 URL 或其他参数动态加载弹窗组件
  125. if (imageUrl === this.images[0]) {
  126. modal = await this.modalCtrl.create({
  127. component: Image1PopupComponent,
  128. componentProps: {
  129. imageUrl: imageUrl,
  130. description: description
  131. }
  132. });
  133. } else if (imageUrl === this.images[1]) {
  134. modal = await this.modalCtrl.create({
  135. component: Image2PopupComponent,
  136. componentProps: {
  137. imageUrl: imageUrl,
  138. description: description
  139. }
  140. });
  141. } else if (imageUrl === this.images[2]) {
  142. modal = await this.modalCtrl.create({
  143. component: Image3PopupComponent,
  144. componentProps: {
  145. imageUrl: imageUrl,
  146. description: description
  147. }
  148. });
  149. } else if (imageUrl === this.images[3]) {
  150. modal = await this.modalCtrl.create({
  151. component: Image4PopupComponent,
  152. componentProps: {
  153. imageUrl: imageUrl,
  154. description: description
  155. }
  156. });
  157. }
  158. if (modal) {
  159. await modal.present();
  160. }
  161. }
  162. // 点击轮播图
  163. async onImageClick(index: number) {
  164. const descriptions = [
  165. '<p><strong>第一张图片</strong></p>',
  166. '<p><strong>第二张图片</strong></p>',
  167. '<p><strong>第三张图片</strong></p>',
  168. '<p><strong>第四张图片</strong></p>'
  169. ];
  170. // 打开弹窗并传递描述
  171. await this.openImagePopup(this.images[index], descriptions[index]);
  172. }
  173. // 前一个幻灯片
  174. prevSlide() {
  175. if (this.currentSlide > 0) {
  176. this.currentSlide--;
  177. } else {
  178. this.currentSlide = this.images.length - 1; // 如果是第一个幻灯片,跳转到最后一个
  179. }
  180. }
  181. // 后一个幻灯片
  182. nextSlide() {
  183. if (this.currentSlide < this.images.length - 1) {
  184. this.currentSlide++;
  185. } else {
  186. this.currentSlide = 0; // 如果是最后一个幻灯片,跳转到第一个
  187. }
  188. }
  189. // 跳转到指定的幻灯片
  190. goToSlide(index: number) {
  191. this.currentSlide = index;
  192. }
  193. // 跳转到饮食建议页面
  194. goHealthTipsPage() {
  195. this.router.navigate([`/tabs/tips`]);
  196. }
  197. }