CommentTransformer.php 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. 'uid' => $val->uid,
  51. 'username' => $userComment['username'],
  52. 'avatar' => $userComment['avatar'],
  53. 'reply_username' => $replyUsername,
  54. 'content' => $val->is_delete?'该评论已被删除':$val->content,
  55. 'created_at' => Carbon::parse($val->created_at)->diffForHumans(),
  56. 'is_delete' => $val->is_delete,
  57. 'is_like' => $replyLike['is_like'],
  58. 'like_count' => $replyLike['like_count'],
  59. ];
  60. $redisReply[] = [
  61. 'uid' => $val->uid,
  62. 'username' => $userComment['username'],
  63. 'avatar' => $userComment['avatar'],
  64. 'reply_username' => $replyUsername,
  65. 'content' => $val->is_delete?'该评论已被删除':$val->content,
  66. 'created_at' => $val->created_at,
  67. 'is_delete' => $val->is_delete,
  68. 'is_like' => $replyLike['is_like'],
  69. 'like_count' => $replyLike['like_count'],
  70. ];
  71. }
  72. Redis::SET($replyKey, json_encode($redisReply));
  73. Redis::EXPIRE($replyKey, 604800);
  74. }
  75. $user = $this->userInfo($postComment['uid']);
  76. $commentLike = $this->getCommentLike($this->postId, $postComment['id'], $this->uid);
  77. return [
  78. 'id' => $postComment['id'],
  79. 'uid' => $postComment['uid'],
  80. 'username' => $user['username'],
  81. 'avatar' => $user['avatar'],
  82. 'content' => $postComment['is_delete']?'该评论已被删除':$postComment['content'],
  83. 'created_at' => Carbon::parse($postComment['created_at'])->diffForHumans(),
  84. 'reply_count' => $postComment['reply_count'],
  85. 'reply' => $reply,
  86. 'replys' => $reply,
  87. 'is_delete' => $postComment['is_delete'],
  88. 'is_like' => $commentLike['is_like'],
  89. 'like_count' => $commentLike['like_count'],
  90. ];
  91. }
  92. }