| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- import { CommonModule, NgFor } from '@angular/common';
- import { Component, Input, OnInit } from '@angular/core';
- import { FormsModule } from '@angular/forms';
- import { IonList,IonButton, IonButtons, IonContent, IonHeader, IonInput, IonItem, IonLabel, IonTitle, IonToolbar, ModalController, IonToast, ToastController, IonAvatar } from '@ionic/angular/standalone';
- import { CloudComment, CloudObject, CloudQuery, CloudUser } from 'src/lib/ncloud';
- @Component({
- selector: 'app-post-comment',
- templateUrl: './post-comment.component.html',
- styleUrls: ['./post-comment.component.scss'],
- standalone: true,
- imports: [
- IonContent,IonList,IonItem,NgFor,IonItem,IonLabel,IonInput,IonButton,
- IonHeader,IonToolbar,IonButtons,IonTitle,FormsModule,IonToast,IonAvatar,CommonModule,
- ]
- })
- export class PostCommentComponent implements OnInit {
- commentList: any[] = []; // 存储评论
- @Input() postId!: string; // 接收帖子 ID
- currentUser: CloudUser; // 当前用户实例
- avatarUrl: string = 'assets/img/default.png'; // 默认头像路径
- usersMap = new Map<string, CloudObject[]>();
- constructor( private toastController: ToastController) {
- this.currentUser = new CloudUser(); // 初始化当前用户
- }
- ngOnInit() {
- this.loadComments(); // 页面加载时获取评论
- }
- // 加载评论
- async loadComments() {
- const commentQuery = new CloudQuery('FilmPostComment');
- commentQuery.equalTo('post', { "__type": "Pointer", "className": "FilmPost", "objectId": this.postId });
- this.commentList = await commentQuery.find();
-
- // 获取每个评论对应的用户信息
- for (const comment of this.commentList) {
- const userId = comment?.get('user')?.objectId;
- console.log(userId);
- if (userId) {
- const query= new CloudQuery('_User');
- query.equalTo('objectId', userId);
- try {
- const results: Array<CloudObject> = await query.find();
- console.log(results);
- this.usersMap.set(userId, results);
- } catch (error) {
- console.error('查询出错:', error);
- }
- }
- }
- }
- getuserByComment(comment: CloudObject): CloudObject | undefined {
- const userId = comment?.get('user')?.objectId;
- const users = this.usersMap.get(userId);
- return users ? users[0] : undefined; // 假设每个一个帖子对应userId
- }
- commentData:any={
- }
- commentDataChange(key:string,env:any){
- this.commentData[key] = env.detail.value; // 更新 commentData 对象
- console.log(this.commentData);
- }
-
- // 发送评论
- async sendComment() {
- if (!this.commentData['content']) {
- console.error('评论内容不能为空');
- return; // 如果评论内容为空,直接返回
- }
- this.commentData['postId']=this.postId;
- let comment = new CloudComment();
- try{
- // 创建评论
- await comment.createComment(this.commentData, this.currentUser.id);
-
- this.commentData['content'] = ''; // 清空输入框
- // 创建并显示提示
- const toast = await this.toastController.create({
- message: '评论发送成功!',
- duration: 2000, // 持续时间(毫秒)
- position: 'middle', // 显示位置
- });
- toast.present(); // 显示提示
- await comment.updatePostCommentCount(this.postId,1);
- await this.loadComments(); // 重新加载评论
- }catch (error) {
- console.error('评论失败:', error);
-
- }
- }
- }
- export async function openPostCommentModal(modalCtrl: ModalController, id: string | null | undefined):Promise<CloudUser|null>{
- if(!id) {
- console.error('postId is null or undefined');
- return null;
- }
- const modal = await modalCtrl.create({
- component: PostCommentComponent,
- componentProps: { postId: id }, // 传递 postId 到 PostCommentComponent
- breakpoints: [0.5, 1.0],
- initialBreakpoint: 1.0});
-
- modal.present();
- const { data, role } = await modal.onWillDismiss();
- if (role === 'confirm') {
- return data;
- }
- return null
- }
|