select('post.*') ->find($id); } //获取内容话题 public function getTopic($topic_ids) { $ids = explode(',', $topic_ids); $topic = []; foreach($ids as $id){ $name = Redis::ZRANGEBYSCORE('topic.name', $id, $id); if($name && isset($name[0])){ $topic[] = [ 'id' => intval($id), 'name' => $name[0], ]; } } return $topic; } //获取内容详情 public function getPostInfo($id, $type = 0) { if($type){ $res = Redis::SISMEMBER('delete_post_ids', $id); if($res){ return []; } } $data = Redis::HGETALL('post_info_'.$id); if($data){ $data['praise_count'] = intval($data['praise_count']); $data['comment_count'] = intval($data['comment_count']); $data['collect_count'] = intval($data['collect_count']); $data['available_bean'] = intval($data['available_bean']); $data['collect_bean'] = intval($data['collect_bean']); $data['will_collect_bean'] = $data['will_collect_bean'] + 3 * $data['pv']; $data['imgs'] = json_decode($data['imgs'], true); } return $data; } //获取内容最新评论 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); 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); } return $comment; } //获取评论点赞数、点赞状态 public function getCommentLike($postId, $commentId, $uid = 0) { $key = 'comment_like_'.$postId; $isLike = 0; if($uid){ $isLike = Redis::ZSCORE($key, $uid.'_'.$commentId) ? 1 : 0; } $likeCount = Redis::ZCOUNT($key, $commentId, $commentId); return [ 'is_like' => $isLike, 'like_count' => $likeCount ]; } }