uid = $uid; } public function transform(InterestCircleMessage $interestCircleMessage) { $user = $this->formatUser($this->userInfo($interestCircleMessage['uid'])); $imgs = []; foreach ($interestCircleMessage->imgs as $img) { $imgs[] = $img['image']; } 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'], 'action' => $this->getAction($this->uid,$interestCircleMessage['id']), 'comment_count' => $interestCircleMessage['comment_count'], 'comment' => $this->getNewComment($interestCircleMessage['id']), ]; } public function getAction($uid, $msgId) { $info = InterestCircleMessageRecord::where([['uid', $uid], ['msg_id', $msgId]])->first(); $status = 0; if ($info && $info->action == 1) { $status = 1; } elseif ($info && $info->action == -1) { $status = -1; } return $status; } //获取内容最新评论 public function getNewComment($id, $uid = 0) { $comment = []; $commentKey = 'circle_message_new_comment_' . $id; $commentData = Redis::GET($commentKey); if ($commentData) { $comment = json_decode($commentData, true); foreach ($comment as &$item) { $item['uid'] = intval($item['uid']); $item['reply_count'] = 0; $item['reply'] = []; } } else { $comments = InterestCircleMessageComment::where('msg_id', $id)->where('parent_id', 0)->where('is_delete', 0)->orderBy('id', 'desc')->limit(2)->get(); foreach ($comments as $item) { $userComment = $this->userInfo($item->uid); $comment[] = [ 'id' => $item->id, 'uid' => intval($userComment['uid']), 'username' => $userComment['username'], 'content' => $item->is_delete ? '该评论已被删除' : $item->content, 'is_delete' => $item->is_delete, 'reply_count' => 0, 'reply' => [], ]; } Redis::SET($commentKey, json_encode($comment)); Redis::EXPIRE($commentKey, 300); } return $comment; } }