Sfoglia il codice sorgente

feat: post & comments show

未来全栈 4 mesi fa
parent
commit
39463ef0f1

+ 7 - 7
TFPower-app/src/app/tab3/tab3.page.html

@@ -25,12 +25,12 @@
   <ion-list class="post-list">
     <ion-item *ngFor="let post of posts">
       <ion-avatar slot="start">
-        <img [src]="post.get('avatar')" alt="Avatar">
+        <img [src]="post?.get('user_id')?.avater" alt="Avatar">
       </ion-avatar>
       <ion-label>
-        <h2>{{ post.get('username') }}</h2>
-t.get('image_url')" alt="Post Image" style="max-width: 100%;">        <p>{{ post.get('content') }}</p>
-        <img *ngIf="post.get('image_url')" [src]="pos
+        <h2>{{ post?.get('user_id')?.username }}</h2>
+        <p>{{ post.get('content') }}</p>
+        <img *ngIf="post.get('image_url')" [src]="post?.get('image_url')" style="max-width: 100%;"/>
         <div>
           <ion-button (click)="likePost(post.get('objectId'))">
             <ion-icon [name]="post.liked ? 'thumbs-up' : 'thumbs-up-outline'"></ion-icon> 
@@ -40,16 +40,16 @@ t.get('image_url')" alt="Post Image" style="max-width: 100%;">        <p>{{ post
           </ion-button>
         </div>
         <ion-item>
-          <ion-input [(ngModel)]="commentInputs[post.get('objectId')]" name="comment-{{post.get('objectId')}}" placeholder="写评论..."></ion-input>
+          <ion-input [(ngModel)]="commentInputs[post.get('objectId')]" placeholder="写评论..."></ion-input>
           <ion-button (click)="commentOnPost(post.get('objectId'), commentInputs[post.get('objectId')])">发送</ion-button>
         </ion-item>
         <ion-list>
           <ion-item *ngFor="let comment of post.comments">
             <ion-avatar slot="start">
-              <img [src]="comment.get('avatar')" alt="Avatar">
+              <img [src]="comment?.get('user_id')?.avater" alt="Avatar">
             </ion-avatar>
             <ion-label>
-              <h2>{{ comment.get('username') }}</h2>
+              <h2>{{ comment?.get('user_id')?.username }}</h2>
               <p>{{ comment.get('content') }}</p>
             </ion-label>
           </ion-item>

+ 14 - 69
TFPower-app/src/app/tab3/tab3.page.ts

@@ -29,43 +29,27 @@ export class Tab3Page {
   }
 
   async ngOnInit() {
-    await this.loadPosts();
+    this.loadPosts();
   }
 
   async loadPosts() {
     const postQuery = new CloudQuery('Post');
+    postQuery.include("user_id")
     this.posts = await postQuery.find();
     
     console.log("获取的帖子:", this.posts); // 调试信息
-    
 
     // 获取用户信息并添加到帖子中
     for (let post of this.posts) {
-      // console.log("post",post);
-      
-      // let userQuery = new CloudQuery('User');
-      // console.log("post.get('user_id'):::",post.get('user_id'));
-      
-      // userQuery.equalTo("objectId",post.get('user_id'));
-      // let user:any = await userQuery.find()
-      // console.log("user::",user);
-      // post.username = user.get('username'); // 添加用户名
-      // post.avatar = user.get('avatar'); // 用户表中有头像字段
-      // post.liked = post.likes.includes(this.currentUser?.id); // 检查当前用户是否已点赞
-      // // 初始化评论输入
-      // this.commentInputs[post.objectId] = ''; // 以帖子的 objectId 为键
-    
-      let fituserQuery = new CloudQuery('fitUser');
-      let fituser = await fituserQuery.get(post.get('user_id').objectId); // 使用 get 方法
-      post.username = fituser.get('name'); // 使用 get 方法
-      post.avatar = fituser.get('avater'); // 使用 get 方法
-
       // 获取该帖子的点赞用户信息
-     let a = await this.getLikesForPost(post.get('objectId')) || []; 
-      console.log("获取的点赞信息:", a); // 调试信息
+      console.log(post.id)
+      this.getLikesForPost(post.id) || []; 
+      // console.log("获取的点赞信息:", a); // 调试信息
       // 获取该帖子的评论信息
-      post.comments = await this.getCommentsForPost(post.get('objectId'))|| []; // 直接将评论存储到 post 对象中
-      console.log("获取的评论信息:", post.comments); // 调试信息
+      this.getCommentsForPost(post.id).then(comments=>{
+        post.comments = comments
+      }); // 直接将评论存储到 post 对象中
+      // console.log("获取的评论信息:", post.comments); // 调试信息
      
       
      // 获取所有评论
@@ -76,48 +60,20 @@ export class Tab3Page {
     
     }
   }
+  
 
 
 
 // 获取某个帖子的评论
 async getCommentsForPost(postId: string) {
   const commentsQuery = new CloudQuery('Comments');
+  commentsQuery.include("user_id")
   commentsQuery.equalTo('post_id', postId );
   const comments = await commentsQuery.find();
 
   console.log("获取的评论:", comments); // 查看获取的评论
 
-  const commentDetails = await Promise.all(
-    comments.map(async (comment: any) => {
-
-      console.log("当前评论:", comment); // 查看每个评论的内容
-      
-      const fituserQuery = new CloudQuery('fitUser');
-      const user = await fituserQuery.get(comment.get('user_id').objectId);
-     
-      console.log("获取的用户信息:", user); // 调试信息,查看获取的用户信息
-
-
-    //   if (!userId) {
-    //      console.error("user_id 为 undefined,无法获取用户信息");
-    //     return { content: comment.content, username: null, avatar: null }; // 返回默认值
-    // }
-
-    // const user = await userQuery.get(userId); // 使用 user_id
-    // console.log("获取的用户信息:", user); // 查看获取的用户信息
-
-      return { 
-        content: comment.get('content'), 
-        username: user.get('name'), // 直接添加 username
-        avatar: user.get('avatar') // 直接添加 avatar
-      }; // 返回评论内容和用户信息
-    })
-  );
-
-  // console.log("处理后的评论:", commentDetails); // 调试信息,查看处理后的评论
-
-
-  return commentDetails; // 返回评论的详细信息
+  return comments; // 返回评论的详细信息
 }
 
  // 获取某个帖子的点赞用户信息
@@ -125,19 +81,8 @@ async getLikesForPost(postId: string) {
   const likesQuery = new CloudQuery('Likes');
   likesQuery.equalTo('post_id', postId );
   const likes = await likesQuery.find();
-
-  const likeUsers = await Promise.all(
-    likes.map(async (like: any) => {
-      const fituserQuery = new CloudQuery('fitUser');
-      const user = await fituserQuery.get(like.get('user_id').objectId);
-      return { id: user.get('objectId'),
-         username: user.get('name'), 
-         avatar: user.get('avatar') 
-        }; // 返回用户的 ID、姓名和头像
-    })
-  );
-
-  return likeUsers; // 返回点赞人的详细信息
+  return likes
+  ; // 返回点赞人的详细信息
 }
 
 async publishPost() {