TopicPostTransformer.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. 'is_fine' => $post['is_fine'],
  56. 'title' => $post['title'],
  57. 'content' => subtext($post['content'], 100),
  58. 'location' => $post['location'],
  59. 'img' => $post['img'],
  60. 'imgs' => $imgs,
  61. 'video' => $post['video'],
  62. 'pv' => getNumber($post->data->pv),
  63. 'praise_count' => $post->data->praise_count,
  64. 'comment_count' => $post->data->comment_count,
  65. 'will_collect_bean' => $post->data->will_collect_bean + 3 * $post->data->pv,
  66. 'is_like' => $isLike,
  67. 'is_dislike' => $isDislike,
  68. 'is_collect' => $isCollect,
  69. 'comment' => $this->getNewComment($post['id'], $this->uid),
  70. 'is_follow' => $isFollow,
  71. 'h5url' => config('customer.share_post_h5url') . "?post_id={$post['id']}&invite_code={$this->invite_code}",
  72. 'desc_url' => $post['type'] == 'html' ? config('customer.app_service_url') . '/community/fragment/detail/' . $post['id'] : '',
  73. ];
  74. }
  75. }