page-aichat.page.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import { Component, OnInit, ViewChild, ElementRef, AfterViewChecked } from '@angular/core';
  2. import { trigger, transition, style, animate } from '@angular/animations';
  3. interface ChatMessage {
  4. content: string;
  5. isAI: boolean;
  6. time: string;
  7. status?: 'sending' | 'sent' | 'error';
  8. }
  9. @Component({
  10. selector: 'app-page-aichat',
  11. templateUrl: './page-aichat.page.html',
  12. styleUrls: ['./page-aichat.page.scss'],
  13. standalone: false,
  14. animations: [
  15. trigger('messageAnim', [
  16. transition(':enter', [
  17. style({ opacity: 0, transform: 'translateY(20px)' }),
  18. animate('300ms cubic-bezier(0.4, 0, 0.2, 1)',
  19. style({ opacity: 1, transform: 'translateY(0)' }))
  20. ])
  21. ])
  22. ]
  23. })
  24. export class PageAichatPage implements OnInit, AfterViewChecked {
  25. @ViewChild('messagesContainer') private messagesContainer!: ElementRef;
  26. @ViewChild('messageInput') messageInput!: ElementRef;
  27. messages: ChatMessage[] = [];
  28. newMessage = '';
  29. isInputFocused = false;
  30. get canSend(): boolean {
  31. return this.newMessage.trim().length > 0;
  32. }
  33. ngOnInit() {
  34. this.addAIMessage('您好!我是您的智能食谱助手,您可以输入食材名称,我会为您推荐相关食谱。例如:"番茄"、"鸡肉"');
  35. }
  36. ngAfterViewChecked() {
  37. this.scrollToBottom();
  38. }
  39. async sendMessage() {
  40. if (!this.canSend) return;
  41. const userMessage = this.newMessage.trim();
  42. this.addUserMessage(userMessage);
  43. // 模拟发送过程
  44. await this.simulateSendDelay();
  45. // 添加AI回复
  46. this.addAIMessage(await this.generateAIReply(userMessage));
  47. this.newMessage = '';
  48. this.messageInput.nativeElement.focus();
  49. }
  50. private addUserMessage(content: string) {
  51. this.messages.push({
  52. content,
  53. isAI: false,
  54. time: this.getCurrentTime(),
  55. status: 'sending'
  56. });
  57. // 更新发送状态
  58. setTimeout(() => {
  59. const index = this.messages.length - 1;
  60. this.messages[index].status = Math.random() > 0.1 ? 'sent' : 'error';
  61. }, 1000);
  62. }
  63. private addAIMessage(content: string) {
  64. this.messages.push({
  65. content,
  66. isAI: true,
  67. time: this.getCurrentTime()
  68. });
  69. }
  70. private scrollToBottom() {
  71. try {
  72. setTimeout(() => {
  73. this.messagesContainer.nativeElement.scrollTop =
  74. this.messagesContainer.nativeElement.scrollHeight;
  75. }, 100);
  76. } catch(err) { /* 处理滚动错误 */ }
  77. }
  78. private getCurrentTime(): string {
  79. return new Date().toLocaleTimeString([], {
  80. hour: '2-digit',
  81. minute: '2-digit'
  82. });
  83. }
  84. private async simulateSendDelay(): Promise<void> {
  85. return new Promise(resolve => setTimeout(resolve, 500));
  86. }
  87. private async generateAIReply(input: string): Promise<string> {
  88. // 模拟AI回复逻辑
  89. await new Promise(resolve => setTimeout(resolve, 800));
  90. return this.getRecipeRecommendations(input);
  91. }
  92. private getRecipeRecommendations(input: string): string {
  93. const recipes: { [key: string]: string[] } = {
  94. 番茄: [
  95. "番茄炒蛋:将番茄切块,鸡蛋炒熟后加入番茄一起翻炒,调味即可。",
  96. "番茄汤:番茄去皮切块,加水煮开后调味,可加入鸡蛋液做成番茄蛋汤。",
  97. "番茄意大利面:番茄炒成酱,加入煮好的意大利面拌匀。"
  98. ],
  99. 鸡肉: [
  100. "宫保鸡丁:鸡肉切丁,与花生米、青椒等一起炒制。",
  101. "香煎鸡胸肉:鸡胸肉用调料腌制后,放入锅中煎至两面金黄。",
  102. "鸡汤:鸡肉加水炖煮,加入姜片、葱段等调料。"
  103. ],
  104. 土豆: [
  105. "土豆丝:土豆切成丝,清炒或酸辣口味都很不错。",
  106. "土豆炖牛肉:土豆与牛肉一起炖煮,口感软糯。",
  107. "薯条:土豆切成条,炸至金黄,可搭配番茄酱食用。"
  108. ]
  109. };
  110. for (const [ingredient, recipeList] of Object.entries(recipes)) {
  111. if (input.includes(ingredient)) {
  112. const randomIndex = Math.floor(Math.random() * recipeList.length);
  113. return `为您推荐使用 ${ingredient} 的食谱:${recipeList[randomIndex]}`;
  114. }
  115. }
  116. return `很抱歉,暂时没有找到与 "${input}" 相关的食谱。您可以尝试输入其他食材。`;
  117. }
  118. }