SuggestTransformer.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/6/15
  6. * Time: 11:07
  7. */
  8. namespace App\Transformers\Post;
  9. use App\Models\Post;
  10. use App\Models\PostCollect;
  11. use App\Models\PostComment;
  12. use App\Models\PostDislike;
  13. use App\Models\PostLike;
  14. use App\Traits\PostTrait;
  15. use App\Traits\UserTrait;
  16. use Carbon\Carbon;
  17. use Illuminate\Support\Facades\Redis;
  18. use League\Fractal\TransformerAbstract;
  19. class SuggestTransformer extends TransformerAbstract
  20. {
  21. use UserTrait;
  22. use PostTrait;
  23. public function __construct($uid, $invite_code)
  24. {
  25. $this->uid = $uid;
  26. $this->invite_code = $invite_code;
  27. }
  28. public function transform(Post $post)
  29. {
  30. $comment = [];
  31. $commentKey = 'post_new_comment_'.$post['id'];
  32. $commentData = Redis::GET($commentKey);
  33. if($commentData){
  34. $comment = json_decode($commentData);
  35. }else{
  36. $comments = PostComment::where('post_id', $post['id'])->where('parent_id', 0)->orderBy('is_delete', 'asc')->orderBy('id', 'desc')->limit(2)->get();
  37. foreach($comments as $item){
  38. $userComment = $this->userInfo($item->uid);
  39. $comment[] = [
  40. 'id' => $item->id,
  41. 'username' => $userComment['username'],
  42. 'content' => $item->is_delete?'该评论已被删除':$item->content,
  43. ];
  44. }
  45. Redis::SET($commentKey, json_encode($comment));
  46. }
  47. $isLike = 0;
  48. $isDislike = 0;
  49. $isCollect = 0;
  50. if($this->uid){
  51. $isLike = Redis::SISMEMBER('post_like_'.$post['id'], $this->uid);
  52. $isDislike = Redis::SISMEMBER('post_unlike_'.$post['id'], $this->uid);
  53. $isCollect = Redis::SISMEMBER('post_collect_'.$post['id'], $this->uid);
  54. }
  55. $user = $this->userInfo($post['uid']);
  56. $postInfo = $this->getPostInfo($post['id']);
  57. return [
  58. 'show_type' => 'post',
  59. 'id' => $post['id'],
  60. 'type' => $post['type'],
  61. 'created_at' => Carbon::parse($post['created_at'])->diffForHumans(),
  62. 'uid' => $post['uid'],
  63. 'username' => $user['username'],
  64. 'avatar' => $user['avatar'],
  65. 'topic' => $this->getTopic($post['topic_ids']),
  66. 'title' => $post['title'],
  67. 'content' => $post['content'],
  68. 'location' => $post['location'],
  69. 'img' => $post['img'],
  70. 'imgs' => $postInfo['imgs'],
  71. 'video' => $post['video'],
  72. 'pv' => getNumber($postInfo['pv']),
  73. 'praise_count' => $postInfo['praise_count'],
  74. 'comment_count' => $postInfo['comment_count'],
  75. 'collect_count' => $postInfo['collect_count'],
  76. 'will_collect_bean' => $postInfo['will_collect_bean'],
  77. 'is_like' => $isLike,
  78. 'is_dislike' => $isDislike,
  79. 'is_collect' => $isCollect,
  80. 'comment' => $comment,
  81. 'is_follow' => $this->getFollowStatus($this->uid, $post['uid']),
  82. 'h5url' => config('customer.share_post_h5url')."?post_id={$post['id']}&invite_code={$this->invite_code}",
  83. 'desc_url' => $post['type'] == 'html'?config('customer.app_service_url').'/community/fragment/detail/'.$post['id']:'',
  84. ];
  85. }
  86. }