SuggestTransformer.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. Redis::del($commentKey);
  34. if($commentData){
  35. $comment = json_decode($commentData);
  36. }else{
  37. $comments = PostComment::where('post_id', $post['id'])->where('parent_id', 0)->orderBy('is_delete', 'asc')->orderBy('id', 'desc')->limit(2)->get();
  38. foreach($comments as $item){
  39. $userComment = $this->userInfo($item->uid);
  40. $comment[] = [
  41. 'id' => $item->id,
  42. 'username' => $userComment['username'],
  43. 'content' => $item->is_delete?'该评论已被删除':$item->content,
  44. ];
  45. }
  46. // Redis::SET($commentKey, json_encode($comment));
  47. }
  48. $isLike = 0;
  49. $isDislike = 0;
  50. $isCollect = 0;
  51. if($this->uid){
  52. $isLike = Redis::SISMEMBER('post_like_'.$post['id'], $this->uid);
  53. $isDislike = Redis::SISMEMBER('post_unlike_'.$post['id'], $this->uid);
  54. $isCollect = Redis::SISMEMBER('post_collect_'.$post['id'], $this->uid);
  55. }
  56. $user = $this->userInfo($post['uid']);
  57. $postInfo = $this->getPostInfo($post['id']);
  58. return [
  59. 'show_type' => 'post',
  60. 'id' => $post['id'],
  61. 'type' => $post['type'],
  62. 'created_at' => Carbon::parse($post['created_at'])->diffForHumans(),
  63. 'uid' => $post['uid'],
  64. 'username' => $user['username'],
  65. 'avatar' => $user['avatar'],
  66. 'topic' => $this->getTopic($post['topic_ids']),
  67. 'title' => $post['title'],
  68. 'content' => $post['content'],
  69. 'location' => $post['location'],
  70. 'img' => $post['img'],
  71. 'imgs' => $postInfo['imgs'],
  72. 'video' => $post['video'],
  73. 'pv' => getNumber($postInfo['pv']),
  74. 'praise_count' => $postInfo['praise_count'],
  75. 'comment_count' => $postInfo['comment_count'],
  76. 'collect_count' => $postInfo['collect_count'],
  77. 'will_collect_bean' => $postInfo['will_collect_bean'],
  78. 'is_like' => $isLike,
  79. 'is_dislike' => $isDislike,
  80. 'is_collect' => $isCollect,
  81. 'comment' => $comment,
  82. 'is_follow' => $this->getFollowStatus($this->uid, $post['uid']),
  83. 'h5url' => config('customer.share_post_h5url')."?post_id={$post['id']}&invite_code={$this->invite_code}",
  84. 'desc_url' => $post['type'] == 'html'?config('customer.app_service_url').'/community/fragment/detail/'.$post['id']:'',
  85. ];
  86. }
  87. }