TopicPostTransformer.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/6/17
  6. * Time: 19:26
  7. */
  8. namespace App\Transformers\Topic;
  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 League\Fractal\TransformerAbstract;
  18. class TopicPostTransformer extends TransformerAbstract
  19. {
  20. use UserTrait;
  21. use PostTrait;
  22. public function __construct($uid, $invite_code)
  23. {
  24. $this->uid = $uid;
  25. $this->invite_code = $invite_code;
  26. }
  27. public function transform(Post $post)
  28. {
  29. $imgs = [];
  30. foreach($post->imgs as $img){
  31. $imgs[] = $img['img'];
  32. }
  33. $topic = [];
  34. foreach($post->topic() as $key => $val){
  35. $topic[] = [
  36. 'id' => $key,
  37. 'name' => $val
  38. ];
  39. }
  40. $isLike = 0;
  41. $isDislike = 0;
  42. $isCollect = 0;
  43. $isFollow = 0;
  44. if($this->uid){
  45. $isLike = PostLike::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0;
  46. $isDislike = PostDislike::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0;
  47. $isCollect = PostCollect::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0;
  48. $followStatus = $this->getFollowStatus($this->uid, $post['uid']);
  49. if($followStatus){
  50. $isFollow = $followStatus;
  51. }
  52. }
  53. $user = $this->userInfo($post['uid']);
  54. return [
  55. 'id' => $post['id'],
  56. 'type' => $post['type'],
  57. 'created_at' => Carbon::parse($post['created_at'])->diffForHumans(),
  58. 'uid' => $post['uid'],
  59. 'username' => $user['username'],
  60. 'avatar' => $user['avatar'],
  61. 'topic' => $topic,
  62. 'title' => $post['title'],
  63. 'content' => $post['content'],
  64. 'location' => $post['location'],
  65. 'img' => $post['img'],
  66. 'imgs' => $imgs,
  67. 'video' => $post['video'],
  68. 'pv' => getNumber($post->data->pv),
  69. 'praise_count' => $post->data->praise_count,
  70. 'comment_count' => $post->data->comment_count,
  71. 'will_collect_bean' => $post->data->will_collect_bean + 3 * $post->data->pv,
  72. 'is_like' => $isLike,
  73. 'is_dislike' => $isDislike,
  74. 'is_collect' => $isCollect,
  75. 'comment' => $this->getNewComment($post['id'], $this->uid),
  76. 'is_follow' => $isFollow,
  77. 'h5url' => config('customer.share_post_h5url')."?post_id={$post['id']}&invite_code={$this->invite_code}",
  78. 'desc_url' => $post['type'] == 'html'?config('customer.app_service_url').'/community/fragment/detail/'.$post['id']:'',
  79. ];
  80. }
  81. }