chat-history.component.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { CommonModule } from '@angular/common';
  2. import { Component, OnInit } from '@angular/core';
  3. import { Router } from '@angular/router';
  4. import { IonButton, IonButtons, IonCard, IonCardContent, IonCardHeader, IonCardTitle, IonContent, IonFooter, IonHeader, IonIcon, IonInput, IonItem, IonLabel, IonList, IonTitle, IonToolbar, NavController } from '@ionic/angular/standalone';
  5. import { addIcons } from 'ionicons';
  6. import { chevronBackSharp, closeCircleOutline, ellipsisHorizontal, happyOutline, micCircleOutline, paperPlane, sendOutline } from 'ionicons/icons';
  7. import { CloudQuery, CloudUser } from 'src/lib/ncloud';
  8. addIcons({ chevronBackSharp,ellipsisHorizontal,micCircleOutline,happyOutline,paperPlane,closeCircleOutline,sendOutline });
  9. @Component({
  10. selector: 'app-chat-history',
  11. templateUrl: './chat-history.component.html',
  12. styleUrls: ['./chat-history.component.scss'],
  13. standalone: true,
  14. imports: [IonHeader,IonToolbar,IonTitle,IonContent,IonList,IonItem,IonLabel,CommonModule,IonCard,IonCardHeader,IonCardTitle,
  15. IonButton,IonCardContent,IonIcon,IonButtons,IonInput,IonFooter,IonInput,CommonModule],
  16. })
  17. export class ChatHistoryComponent implements OnInit {
  18. chatHistories: any[] = []; // 存储聊天记录
  19. selectedChat: any; // 存储当前选择的聊天记录,json字符串
  20. constructor(private router: Router,private navCtrl: NavController) {}
  21. async ngOnInit() {
  22. await this.loadChatHistories(); // 加载聊天记录
  23. }
  24. async loadChatHistories() {
  25. const user = new CloudUser();
  26. const currentUser = await user.current(); // 获取当前用户信息
  27. if (currentUser) {
  28. const query = new CloudQuery("ChatHistory");
  29. query.equalTo("user", currentUser.toPointer()); // 查询当前用户的聊天记录
  30. this.chatHistories = await query.find(); // 获取聊天记录
  31. console.log("聊天记录已加载", this.chatHistories);
  32. } else {
  33. console.error("用户未登录,无法加载聊天记录");
  34. }
  35. }
  36. // 选择聊天记录
  37. selectChatHistory(chat: any) {
  38. this.selectedChat = chat.data.content; // 更新当前选择的聊天记录
  39. console.log("选择聊天记录", this.selectedChat);
  40. // 解析 JSON 字符串为对象数组
  41. this.selectedChat = JSON.parse(this.selectedChat);
  42. }
  43. // 清除选择
  44. clearSelection() {
  45. this.selectedChat = null; // 清除选择
  46. }
  47. goBack() {
  48. this.navCtrl.back(); // 返回上一页
  49. }
  50. }