|
@@ -0,0 +1,88 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by PhpStorm.
|
|
|
+ * User: Administrator
|
|
|
+ * Date: 2019/6/6
|
|
|
+ * Time: 14:08
|
|
|
+ */
|
|
|
+
|
|
|
+namespace App\Transformers\Circle;
|
|
|
+
|
|
|
+use App\Models\InterestCircle;
|
|
|
+use App\Models\InterestCircleMessage;
|
|
|
+use App\Models\Post;
|
|
|
+use App\Traits\PostTrait;
|
|
|
+use App\Traits\UserTrait;
|
|
|
+use Illuminate\Support\Carbon;
|
|
|
+use Illuminate\Support\Facades\Redis;
|
|
|
+use League\Fractal\TransformerAbstract;
|
|
|
+
|
|
|
+class MessageListTransformer extends TransformerAbstract
|
|
|
+{
|
|
|
+ use UserTrait;
|
|
|
+
|
|
|
+ public function transform(InterestCircleMessage $interestCircleMessage)
|
|
|
+ {
|
|
|
+ $user = $this->userInfo($interestCircleMessage['uid']);
|
|
|
+ return [
|
|
|
+ 'id' => $interestCircleMessage['id'],
|
|
|
+ 'created_at' => Carbon::parse($interestCircleMessage['created_at'])->diffForHumans(),
|
|
|
+ 'user' => $user ? $user : new \stdClass(),
|
|
|
+ 'content' => $interestCircleMessage['content'],
|
|
|
+ 'imgs' => $postInfo['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;
|
|
|
+ }
|
|
|
+}
|