CommentTransformer.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/6/15
  6. * Time: 16:40
  7. */
  8. namespace App\Transformers\Post;
  9. use App\Models\PostComment;
  10. use App\Traits\PostTrait;
  11. use App\Traits\UserTrait;
  12. use Carbon\Carbon;
  13. use Illuminate\Support\Facades\Log;
  14. use Illuminate\Support\Facades\Redis;
  15. use League\Fractal\TransformerAbstract;
  16. class CommentTransformer extends TransformerAbstract
  17. {
  18. use UserTrait;
  19. use PostTrait;
  20. public function __construct($postId, $uid)
  21. {
  22. $this->postId = $postId;
  23. $this->uid = $uid;
  24. }
  25. public function transform(PostComment $postComment)
  26. {
  27. $reply = [];
  28. $replyKey = 'post_new_reply_'.$postComment['id'];
  29. $replyData = Redis::GET($replyKey);
  30. if($replyData){
  31. $reply = json_decode($replyData);
  32. foreach($reply as &$item){
  33. $item->created_at = Carbon::parse($item->created_at)->diffForHumans();
  34. $replyLike = $this->getCommentLike($this->postId, $item->id, $this->uid);
  35. $item->is_like = $replyLike['is_like'];
  36. $item->like_count = $replyLike['like_count'];
  37. }
  38. }else{
  39. $replies = PostComment::where('parent_id', $postComment['id'])->orderBy('id', 'desc')->limit(2)->get();
  40. $redisReply = [];
  41. foreach($replies as $val){
  42. $replyLike = $this->getCommentLike($this->postId, $val->id, $this->uid);
  43. $userComment = $this->userInfo($val->uid);
  44. $replyUsername = '';
  45. if($val->reply_uid){
  46. $userReply = $this->userInfo($val->reply_uid);
  47. $replyUsername = $userReply['username'];
  48. }
  49. $reply[] = [
  50. 'id' => $val->id,
  51. 'uid' => $val->uid,
  52. 'username' => $userComment['username'],
  53. 'avatar' => $userComment['avatar'],
  54. 'reply_username' => $replyUsername,
  55. 'content' => $val->is_delete?'该评论已被删除':$val->content,
  56. 'created_at' => Carbon::parse($val->created_at)->diffForHumans(),
  57. 'is_delete' => $val->is_delete,
  58. 'is_like' => $replyLike['is_like'],
  59. 'like_count' => $replyLike['like_count'],
  60. ];
  61. $redisReply[] = [
  62. 'id' => $val->id,
  63. 'uid' => $val->uid,
  64. 'username' => $userComment['username'],
  65. 'avatar' => $userComment['avatar'],
  66. 'reply_username' => $replyUsername,
  67. 'content' => $val->is_delete?'该评论已被删除':$val->content,
  68. 'created_at' => $val->created_at,
  69. 'is_delete' => $val->is_delete,
  70. 'is_like' => $replyLike['is_like'],
  71. 'like_count' => $replyLike['like_count'],
  72. ];
  73. }
  74. Redis::SET($replyKey, json_encode($redisReply));
  75. Redis::EXPIRE($replyKey, 604800);
  76. }
  77. $user = $this->userInfo($postComment['uid']);
  78. $commentLike = $this->getCommentLike($this->postId, $postComment['id'], $this->uid);
  79. return [
  80. 'id' => $postComment['id'],
  81. 'uid' => $postComment['uid'],
  82. 'username' => $user['username'],
  83. 'avatar' => $user['avatar'],
  84. 'content' => $postComment['is_delete']?'该评论已被删除':$postComment['content'],
  85. 'created_at' => Carbon::parse($postComment['created_at'])->diffForHumans(),
  86. 'reply_count' => $postComment['reply_count'],
  87. 'reply' => $reply,
  88. 'is_delete' => $postComment['is_delete'],
  89. 'is_like' => $commentLike['is_like'],
  90. 'like_count' => $commentLike['like_count'],
  91. ];
  92. }
  93. }