12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2019/6/15
- * Time: 16:40
- */
- namespace App\Transformers\Post;
- use App\Models\PostComment;
- use App\Traits\PostTrait;
- use App\Traits\UserTrait;
- use Carbon\Carbon;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Redis;
- use League\Fractal\TransformerAbstract;
- class CommentTransformer extends TransformerAbstract
- {
- use UserTrait;
- use PostTrait;
- public function __construct($postId, $uid)
- {
- $this->postId = $postId;
- $this->uid = $uid;
- }
- public function transform(PostComment $postComment)
- {
- $reply = [];
- $replyKey = 'post_new_reply_'.$postComment['id'];
- $replyData = Redis::GET($replyKey);
- if($replyData){
- $reply = json_decode($replyData);
- foreach($reply as &$item){
- $item->created_at = Carbon::parse($item->created_at)->diffForHumans();
- $replyLike = $this->getCommentLike($this->postId, $item->id, $this->uid);
- $item->is_like = $replyLike['is_like'];
- $item->like_count = $replyLike['like_count'];
- }
- }else{
- $replies = PostComment::where('parent_id', $postComment['id'])->orderBy('id', 'desc')->limit(2)->get();
- $redisReply = [];
- foreach($replies as $val){
- $replyLike = $this->getCommentLike($this->postId, $val->id, $this->uid);
- $userComment = $this->userInfo($val->uid);
- $replyUsername = '';
- if($val->reply_uid){
- $userReply = $this->userInfo($val->reply_uid);
- $replyUsername = $userReply['username'];
- }
- $reply[] = [
- 'id' => $val->id,
- 'uid' => $val->uid,
- 'username' => $userComment['username'],
- 'avatar' => $userComment['avatar'],
- 'reply_username' => $replyUsername,
- 'content' => $val->is_delete?'该评论已被删除':$val->content,
- 'created_at' => Carbon::parse($val->created_at)->diffForHumans(),
- 'is_delete' => $val->is_delete,
- 'is_like' => $replyLike['is_like'],
- 'like_count' => $replyLike['like_count'],
- ];
- $redisReply[] = [
- 'id' => $val->id,
- 'uid' => $val->uid,
- 'username' => $userComment['username'],
- 'avatar' => $userComment['avatar'],
- 'reply_username' => $replyUsername,
- 'content' => $val->is_delete?'该评论已被删除':$val->content,
- 'created_at' => $val->created_at,
- 'is_delete' => $val->is_delete,
- 'is_like' => $replyLike['is_like'],
- 'like_count' => $replyLike['like_count'],
- ];
- }
- Redis::SET($replyKey, json_encode($redisReply));
- Redis::EXPIRE($replyKey, 604800);
- }
- $user = $this->userInfo($postComment['uid']);
- $commentLike = $this->getCommentLike($this->postId, $postComment['id'], $this->uid);
- return [
- 'id' => $postComment['id'],
- 'uid' => $postComment['uid'],
- 'username' => $user['username'],
- 'avatar' => $user['avatar'],
- 'content' => $postComment['is_delete']?'该评论已被删除':$postComment['content'],
- 'created_at' => Carbon::parse($postComment['created_at'])->diffForHumans(),
- 'reply_count' => $postComment['reply_count'],
- 'reply' => $reply,
- 'replys' => $reply,
- 'is_delete' => $postComment['is_delete'],
- 'is_like' => $commentLike['is_like'],
- 'like_count' => $commentLike['like_count'],
- ];
- }
- }
|