|
@@ -1,4 +1,16 @@
|
|
|
-import { Component } from '@angular/core';
|
|
|
+import { Component, ViewChild, ElementRef } from '@angular/core';
|
|
|
+import { HttpClient } from '@angular/common/http';
|
|
|
+import { AnimationController, IonContent } from '@ionic/angular';
|
|
|
+import { Haptics } from '@capacitor/haptics';
|
|
|
+
|
|
|
+// interface ChatMessage {
|
|
|
+// content: string;
|
|
|
+// isAI: boolean;
|
|
|
+// time: Date;
|
|
|
+// suggestion?: string;
|
|
|
+// }
|
|
|
+
|
|
|
+
|
|
|
|
|
|
@Component({
|
|
|
selector: 'app-ai',
|
|
@@ -8,6 +20,137 @@ import { Component } from '@angular/core';
|
|
|
})
|
|
|
export class AiPage {
|
|
|
|
|
|
- constructor() {}
|
|
|
+ // @ViewChild(IonContent) contentArea!: IonContent;
|
|
|
+ // @ViewChild('messageContainer') msgContainer!: ElementRef;
|
|
|
+
|
|
|
+ // // 对话数据
|
|
|
+ // chatHistory: ChatMessage[] = [
|
|
|
+ // {
|
|
|
+ // content: '你好,我是你的心灵守护者小安,随时愿意倾听你的感受~',
|
|
|
+ // isAI: true,
|
|
|
+ // time: new Date()
|
|
|
+ // }
|
|
|
+ // ];
|
|
|
+
|
|
|
+ // userInput = '';
|
|
|
+ // showEmergency = false;
|
|
|
+ // currentMood = {
|
|
|
+ // level: 7.2,
|
|
|
+ // avatar: 'assets/ai-avatar/normal.webp'
|
|
|
+ // };
|
|
|
+
|
|
|
+ constructor(
|
|
|
+ //private http: HttpClient,
|
|
|
+ //private animationCtrl: AnimationController
|
|
|
+ ) {}
|
|
|
+
|
|
|
+ // // 发送消息
|
|
|
+ // async sendMessage() {
|
|
|
+ // if (!this.userInput.trim()) return;
|
|
|
+
|
|
|
+ // // 用户消息
|
|
|
+ // this.addUserMessage(this.userInput);
|
|
|
+
|
|
|
+ // // AI回复
|
|
|
+ // const aiResponse = await this.generateAIResponse(this.userInput);
|
|
|
+ // this.addAIMessage(aiResponse);
|
|
|
+
|
|
|
+ // // 滚动到底部
|
|
|
+ // this.scrollToBottom();
|
|
|
+ // this.userInput = '';
|
|
|
+ // }
|
|
|
+
|
|
|
+ // private async scrollToBottom() {
|
|
|
+ // if (this.contentArea) {
|
|
|
+ // await this.contentArea.scrollToBottom(300); // 300ms 滚动动画
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+
|
|
|
+ // // 生成AI回复
|
|
|
+ // private async generateAIResponse(text: string): Promise<string> {
|
|
|
+ // try {
|
|
|
+ // // 调用本地LLM接口
|
|
|
+ // const response = await this.http.post<{ reply?: string }>('/api/ai/chat', {
|
|
|
+ // message: text,
|
|
|
+ // mood: this.currentMood.level
|
|
|
+ // }).toPromise();
|
|
|
+ // // 安全访问响应数据
|
|
|
+ // const reply = response?.reply ?? '我现在好像有点困惑,可以再说一次吗?';
|
|
|
+
|
|
|
+ // // 触发干预检测
|
|
|
+ // this.checkIntervention(reply);
|
|
|
+
|
|
|
+ // return reply;
|
|
|
+ // } catch (error) {
|
|
|
+ // console.error('AI回复失败:', error);
|
|
|
+ // return '我现在好像有点困惑,可以再说一次吗?';
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ // // 添加用户消息
|
|
|
+ // private addUserMessage(text: string) {
|
|
|
+ // this.chatHistory.push({
|
|
|
+ // content: text,
|
|
|
+ // isAI: false,
|
|
|
+ // time: new Date()
|
|
|
+ // });
|
|
|
+ // this.playMessageAnimation();
|
|
|
+ // }
|
|
|
+
|
|
|
+ // // 添加AI消息
|
|
|
+ // private addAIMessage(text: string) {
|
|
|
+ // this.chatHistory.push({
|
|
|
+ // content: text,
|
|
|
+ // isAI: true,
|
|
|
+ // time: new Date(),
|
|
|
+ // suggestion: this.getSuggestionTag(text)
|
|
|
+ // });
|
|
|
+ // this.playMessageAnimation();
|
|
|
+ // }
|
|
|
+
|
|
|
+ // // 新增建议标签生成
|
|
|
+ // private getSuggestionTag(text: string): string {
|
|
|
+ // const suggestions = [
|
|
|
+ // { keywords: ['呼吸', '放松'], tag: '呼吸练习' },
|
|
|
+ // { keywords: ['运动', '活动'], tag: '身体调节' },
|
|
|
+ // { keywords: ['记录', '写下'], tag: '情绪日记' },
|
|
|
+ // { keywords: ['冥想', '正念'], tag: '正念练习' }
|
|
|
+ // ];
|
|
|
+
|
|
|
+ // return suggestions.find(s =>
|
|
|
+ // s.keywords.some(kw => text.includes(kw))
|
|
|
+ // )?.tag || '';
|
|
|
+ // }
|
|
|
+
|
|
|
+ // // 消息入场动画
|
|
|
+ // private playMessageAnimation() {
|
|
|
+ // const bubbles = this.msgContainer.nativeElement.querySelectorAll('.message');
|
|
|
+ // const lastBubble = bubbles[bubbles.length - 1];
|
|
|
+
|
|
|
+ // this.animationCtrl.create()
|
|
|
+ // .addElement(lastBubble)
|
|
|
+ // .duration(500)
|
|
|
+ // .easing('cubic-bezier(0.4, 0, 0.2, 1)')
|
|
|
+ // .beforeStyles({
|
|
|
+ // opacity: 0,
|
|
|
+ // transform: 'translateY(20px)'
|
|
|
+ // })
|
|
|
+ // .fromTo('transform', 'translateY(20px)', 'translateY(0)')
|
|
|
+ // .fromTo('opacity', '0', '1')
|
|
|
+ // .play();
|
|
|
+ // }
|
|
|
+
|
|
|
+ // // 紧急状态检测
|
|
|
+ // private checkIntervention(response: string) {
|
|
|
+ // const dangerKeywords = ['自杀', '自伤', '极端'];
|
|
|
+ // const isEmergency = dangerKeywords.some(kw => response.includes(kw));
|
|
|
+
|
|
|
+ // if (isEmergency) {
|
|
|
+ // this.showEmergency = true;
|
|
|
+ // Haptics.vibrate({ duration: 1000 });
|
|
|
+ // }
|
|
|
+ // }
|
|
|
|
|
|
}
|