post-comment.component.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { CommonModule, NgFor } from '@angular/common';
  2. import { Component, Input, OnInit } from '@angular/core';
  3. import { FormsModule } from '@angular/forms';
  4. import { IonList,IonButton, IonButtons, IonContent, IonHeader, IonInput, IonItem, IonLabel, IonTitle, IonToolbar, ModalController, IonToast, ToastController, IonAvatar } from '@ionic/angular/standalone';
  5. import { CloudComment, CloudObject, CloudQuery, CloudUser } from 'src/lib/ncloud';
  6. @Component({
  7. selector: 'app-post-comment',
  8. templateUrl: './post-comment.component.html',
  9. styleUrls: ['./post-comment.component.scss'],
  10. standalone: true,
  11. imports: [
  12. IonContent,IonList,IonItem,NgFor,IonItem,IonLabel,IonInput,IonButton,
  13. IonHeader,IonToolbar,IonButtons,IonTitle,FormsModule,IonToast,IonAvatar,CommonModule,
  14. ]
  15. })
  16. export class PostCommentComponent implements OnInit {
  17. commentList: any[] = []; // 存储评论
  18. @Input() postId!: string; // 接收帖子 ID
  19. currentUser: CloudUser; // 当前用户实例
  20. avatarUrl: string = 'assets/img/default.png'; // 默认头像路径
  21. usersMap = new Map<string, CloudObject[]>();
  22. constructor( private toastController: ToastController) {
  23. this.currentUser = new CloudUser(); // 初始化当前用户
  24. }
  25. ngOnInit() {
  26. this.loadComments(); // 页面加载时获取评论
  27. }
  28. // 加载评论
  29. async loadComments() {
  30. const commentQuery = new CloudQuery('FilmPostComment');
  31. commentQuery.equalTo('post', { "__type": "Pointer", "className": "FilmPost", "objectId": this.postId });
  32. this.commentList = await commentQuery.find();
  33. // 获取每个评论对应的用户信息
  34. for (const comment of this.commentList) {
  35. const userId = comment?.get('user')?.objectId;
  36. console.log(userId);
  37. if (userId) {
  38. const query= new CloudQuery('_User');
  39. query.equalTo('objectId', userId);
  40. try {
  41. const results: Array<CloudObject> = await query.find();
  42. console.log(results);
  43. this.usersMap.set(userId, results);
  44. } catch (error) {
  45. console.error('查询出错:', error);
  46. }
  47. }
  48. }
  49. }
  50. getuserByComment(comment: CloudObject): CloudObject | undefined {
  51. const userId = comment?.get('user')?.objectId;
  52. const users = this.usersMap.get(userId);
  53. return users ? users[0] : undefined; // 假设每个一个帖子对应userId
  54. }
  55. commentData:any={
  56. }
  57. commentDataChange(key:string,env:any){
  58. this.commentData[key] = env.detail.value; // 更新 commentData 对象
  59. console.log(this.commentData);
  60. }
  61. // 发送评论
  62. async sendComment() {
  63. if (!this.commentData['content']) {
  64. console.error('评论内容不能为空');
  65. return; // 如果评论内容为空,直接返回
  66. }
  67. this.commentData['postId']=this.postId;
  68. let comment = new CloudComment();
  69. try{
  70. // 创建评论
  71. await comment.createComment(this.commentData, this.currentUser.id);
  72. this.commentData['content'] = ''; // 清空输入框
  73. // 创建并显示提示
  74. const toast = await this.toastController.create({
  75. message: '评论发送成功!',
  76. duration: 2000, // 持续时间(毫秒)
  77. position: 'middle', // 显示位置
  78. });
  79. toast.present(); // 显示提示
  80. await comment.updatePostCommentCount(this.postId,1);
  81. await this.loadComments(); // 重新加载评论
  82. }catch (error) {
  83. console.error('评论失败:', error);
  84. }
  85. }
  86. }
  87. export async function openPostCommentModal(modalCtrl: ModalController, id: string | null | undefined):Promise<CloudUser|null>{
  88. if(!id) {
  89. console.error('postId is null or undefined');
  90. return null;
  91. }
  92. const modal = await modalCtrl.create({
  93. component: PostCommentComponent,
  94. componentProps: { postId: id }, // 传递 postId 到 PostCommentComponent
  95. breakpoints: [0.5, 1.0],
  96. initialBreakpoint: 1.0});
  97. modal.present();
  98. const { data, role } = await modal.onWillDismiss();
  99. if (role === 'confirm') {
  100. return data;
  101. }
  102. return null
  103. }