TopicPostTransformer.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. $isLike = 0;
  34. $isDislike = 0;
  35. $isCollect = 0;
  36. $isFollow = 0;
  37. if($this->uid){
  38. $isLike = PostLike::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0;
  39. $isDislike = PostDislike::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0;
  40. $isCollect = PostCollect::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0;
  41. $followStatus = $this->getFollowStatus($this->uid, $post['uid']);
  42. if($followStatus){
  43. $isFollow = $followStatus;
  44. }
  45. }
  46. $user = $this->userInfo($post['uid']);
  47. return [
  48. 'id' => $post['id'],
  49. 'type' => $post['type'],
  50. 'created_at' => Carbon::parse($post['created_at'])->diffForHumans(),
  51. 'uid' => $post['uid'],
  52. 'username' => $user['username'],
  53. 'avatar' => $user['avatar'],
  54. 'topic' => $this->getTopic($post['topic_ids']),
  55. 'title' => $post['title'],
  56. 'content' => $post['content'],
  57. 'location' => $post['location'],
  58. 'img' => $post['img'],
  59. 'imgs' => $imgs,
  60. 'video' => $post['video'],
  61. 'pv' => getNumber($post->data->pv),
  62. 'praise_count' => $post->data->praise_count,
  63. 'comment_count' => $post->data->comment_count,
  64. 'will_collect_bean' => $post->data->will_collect_bean + 3 * $post->data->pv,
  65. 'is_like' => $isLike,
  66. 'is_dislike' => $isDislike,
  67. 'is_collect' => $isCollect,
  68. 'comment' => $this->getNewComment($post['id'], $this->uid),
  69. 'is_follow' => $isFollow,
  70. 'h5url' => config('customer.share_post_h5url')."?post_id={$post['id']}&invite_code={$this->invite_code}",
  71. 'desc_url' => $post['type'] == 'html'?config('customer.app_service_url').'/community/fragment/detail/'.$post['id']:'',
  72. ];
  73. }
  74. }