userInfo($interestCircleMessage['uid']); $imgs = []; foreach($interestCircleMessage->imgs as $img){ $imgs[] = $img['img']; } return [ 'id' => $interestCircleMessage['id'], 'created_at' => Carbon::parse($interestCircleMessage['created_at'])->diffForHumans(), 'user' => $user ? $user : new \stdClass(), 'content' => $interestCircleMessage['content'], 'imgs' => $imgs, 'good' => $interestCircleMessage['good'], 'bad' => $interestCircleMessage['bad'], 'comment_count' => $interestCircleMessage['comment_count'], 'comment' => $this->getNewComment($interestCircleMessage['id']), ]; } //获取内容最新评论 public function getNewComment($id, $uid = 0) { $comment = []; $key = 'comment_like_' . $id; $commentKey = 'post_new_comment_' . $id; $commentData = Redis::GET($commentKey); if ($commentData) { $comment = json_decode($commentData, true); foreach ($comment as &$item) { $isLike = 0; 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(); foreach ($comments as $item) { $userComment = $this->userInfo($item->uid); $isLike = 0; if ($uid) { $isLike = Redis::ZSCORE($key, $uid . '_' . $item->id) ? 1 : 0; } $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)); Redis::EXPIRE($commentKey, 300); } sortArrByField($comment, 'like_count', true); return $comment; } }