import { CommonModule } from '@angular/common'; import { Component, OnInit } from '@angular/core'; import { Router } from '@angular/router'; import { IonCard, IonItem, IonLabel, IonList } from '@ionic/angular/standalone'; import { IonButton, IonButtons, IonContent, IonHeader, IonTitle, IonToolbar, ModalController } from '@ionic/angular/standalone'; import { CloudObject, CloudQuery, CloudUser } from 'src/lib/ncloud'; // 定义 ContentItem 接口 interface ContentItem { role: string; content: string; createdAt?: string; // 可选属性 hidden?: boolean; // 可选属性 complete?: boolean; // 可选属性 cid?: string; // 可选属性 } @Component({ selector: 'app-report-modal', templateUrl: './report-modal.component.html', styleUrls: ['./report-modal.component.scss'], standalone: true, imports: [IonHeader,IonToolbar,IonTitle,CommonModule, IonButtons,IonButton,IonContent,IonCard,IonList,IonItem,IonLabel ], }) export class ReportModalComponent implements OnInit { constructor(private modalCtrl: ModalController,private router: Router) {} userFeelings: Array<{ content: string, score: number, label: string }> = []; // 存储用户内容和情感分析结果 closeModal() { this.router.navigate(['tabs/tab3']); } chatrecordList:Array=[] async loadChatRecordList() { // 获取当前用户的 ObjectId const currentUser = new CloudUser(); await currentUser.current(); // 确保用户信息已加载 const userId = currentUser.id; // 获取当前用户的 ObjectId // 创建查询并添加过滤条件 let query = new CloudQuery("ChatRecord"); query.equalTo("user", currentUser.toPointer()); // 过滤条件,确保只获取当前用户的记录 this.chatrecordList = await query.find(); // 遍历 chatrecordList,去除每个记录中 user 的第一条 content this.chatrecordList.forEach(chatrecord => { const contentArray = chatrecord.get('content'); if (contentArray && contentArray.length > 0) { // 找到第一条 role 为 user 的内容并移除 const userIndex = contentArray.findIndex((item: ContentItem) => item.role === 'user'); if (userIndex !== -1) { contentArray.splice(userIndex, 1); // 移除第一条 user 内容 } } }); } analyzeUserFeeling(content: string): { score: number; label: string } { const moodScores = { positive: { words: ['开心', '高兴', '喜欢', '快乐', '棒', '兴奋', '满足', '美好','喜悦'], score: 1 }, neutral: { words: ['一般', '还好', '可以', '无所谓', '正常'], score: 0 }, negative: { words: ['难过', '生气', '失望', '讨厌', '悲伤', '烦恼', '痛苦', '沮丧'], score: -1 } }; let totalScore = 0; // 分析内容并计算分数 const contentWords = content.split(/\s+/); contentWords.forEach(word => { for (const mood in moodScores) { if ((moodScores as any)[mood].words.includes(word)) { totalScore += (moodScores as any)[mood].score; } } }); // 判断心情状态 let label = '中性'; if (totalScore > 0) { label = '积极'; } else if (totalScore < 0) { label = '消极'; } return { score: totalScore, label }; } async ngOnInit() { await this.loadChatRecordList(); // 在这里可以遍历 chatrecordList 中的用户内容并进行情感分析 this.chatrecordList.forEach(chatrecord => { const contentArray = chatrecord.get('content'); contentArray.forEach((contentItem: ContentItem) => { if (contentItem.role === 'user') { const analysis = this.analyzeUserFeeling(contentItem.content); this.userFeelings.push({ content: contentItem.content, score: analysis.score, label: analysis.label }); } }); }); } }