Pārlūkot izejas kodu

话题内容评论

wzq 5 gadi atpakaļ
vecāks
revīzija
14bc1412fb

+ 6 - 0
app/Traits/PostTrait.php

@@ -95,8 +95,11 @@ trait PostTrait
                 if($uid){
                     $isLike = Redis::ZSCORE($key, $uid.'_'.$item->id) ? 1 : 0;
                 }
+                $item->uid = intval($item->uid);
                 $item->is_like = $isLike;
                 $item->like_count = Redis::ZCOUNT($key, $item->id, $item->id);
+                $item->reply_count = 0;
+                $item->reply = [];
             }
         }else{
             $comments = PostComment::where('post_id', $id)->where('parent_id', 0)->where('is_delete', 0)->orderBy('like_count', 'desc')->orderBy('id', 'desc')->limit(2)->get();
@@ -109,11 +112,14 @@ trait PostTrait
                 $likeCount = Redis::ZCOUNT($key, $item->id, $item->id);
                 $comment[] = [
                     'id' => $item->id,
+                    'uid' => intval($userComment['uid']),
                     'username' => $userComment['username'],
                     'content' => $item->is_delete?'该评论已被删除':$item->content,
                     'is_delete' => $item->is_delete,
                     'is_like' => $isLike,
                     'like_count' => $likeCount,
+                    'reply_count' => 0,
+                    'reply' => [],
                 ];
             }
             Redis::SET($commentKey, json_encode($comment));

+ 3 - 29
app/Transformers/Topic/TopicPostTransformer.php

@@ -12,6 +12,7 @@ use App\Models\PostCollect;
 use App\Models\PostComment;
 use App\Models\PostDislike;
 use App\Models\PostLike;
+use App\Traits\PostTrait;
 use App\Traits\UserTrait;
 use Carbon\Carbon;
 use League\Fractal\TransformerAbstract;
@@ -19,6 +20,7 @@ use League\Fractal\TransformerAbstract;
 class TopicPostTransformer extends TransformerAbstract
 {
     use UserTrait;
+    use PostTrait;
     public function __construct($uid, $invite_code)
     {
         $this->uid = $uid;
@@ -30,34 +32,6 @@ class TopicPostTransformer extends TransformerAbstract
         foreach($post->imgs as $img){
             $imgs[] = $img['img'];
         }
-        $comment = [];
-        $comments = PostComment::where('post_id', $post['id'])->where('parent_id', 0)->orderBy('id', 'desc')->limit(2)->get();
-        foreach($comments as $item){
-            $replyCount = $item->reply->count();
-            $replies = PostComment::where('parent_id', $item->id)->orderBy('id', 'desc')->limit(2)->get();
-            $reply = [];
-            foreach($replies as $val){
-                $userComment = $this->userInfo($val->uid);
-                $replyUsername = '';
-                if($val->reply_uid){
-                    $userReply = $this->userInfo($val->reply_uid);
-                    $replyUsername = $userReply['username'];
-                }
-                $reply[] = [
-                    'username' => $userComment['username'],
-                    'reply_username' => $replyUsername,
-                    'content' => $val->content,
-                ];
-            }
-            $userComment = $this->userInfo($item->uid);
-            $comment[] = [
-                'id' => $item->id,
-                'username' => $userComment['username'],
-                'content' => $item->content,
-                'reply_count' => $replyCount,
-                'reply' => $reply,
-            ];
-        }
         $topic = [];
         foreach($post->topic() as $key => $val){
             $topic[] = [
@@ -100,7 +74,7 @@ class TopicPostTransformer extends TransformerAbstract
             'is_like' => $isLike,
             'is_dislike' => $isDislike,
             'is_collect' => $isCollect,
-            'comment' => $comment,
+            'comment' => $this->getNewComment($post['id'], $this->uid),
             'is_follow' => $isFollow,
             'h5url' => config('customer.share_post_h5url')."?post_id={$post['id']}&invite_code={$this->invite_code}",
             'desc_url' => $post['type'] == 'html'?config('customer.app_service_url').'/community/fragment/detail/'.$post['id']:'',