VideoTransformer.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/6/18
  6. * Time: 10:08
  7. */
  8. namespace App\Transformers\Post;
  9. use App\Models\Post;
  10. use App\Models\PostCollect;
  11. use App\Models\PostLike;
  12. use App\Traits\UserTrait;
  13. use League\Fractal\TransformerAbstract;
  14. class VideoTransformer extends TransformerAbstract
  15. {
  16. use UserTrait;
  17. public function __construct($uid, $invite_code)
  18. {
  19. $this->uid = $uid;
  20. $this->invite_code = $invite_code;
  21. }
  22. public function transform(Post $post)
  23. {
  24. $isFollow = 0;
  25. $followStatus = $this->getFollowStatus($this->uid, $post['uid']);
  26. if($followStatus){
  27. $isFollow = $followStatus;
  28. }
  29. $topic = [];
  30. foreach($post->topic() as $key => $val){
  31. $topic[] = [
  32. 'id' => $key,
  33. 'name' => $val
  34. ];
  35. }
  36. return [
  37. 'id' => $post['id'],
  38. 'type' => $post['type'],
  39. 'uid' => $post['uid'],
  40. 'username' => subtext($post['username'], 10),
  41. 'avatar' => $post['avatar'],
  42. 'title' => $post['title'],
  43. 'content' => $post['content'],
  44. 'img' => $post['img'],
  45. 'video' => $post['video'],
  46. 'topic' => $topic,
  47. 'praise_count' => $post->data->praise_count,
  48. 'collect_count' => $post->data->collect_count,
  49. 'comment_count' => $post->data->comment_count,
  50. 'will_collect_bean' => $post->data->will_collect_bean + 3 * $post->data->pv,
  51. 'is_like' => PostLike::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0,
  52. 'is_collect' => PostCollect::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0,
  53. 'is_follow' => $isFollow,
  54. 'h5url' => config('customer.share_post_h5url')."?post_id={$post['id']}&invite_code={$this->invite_code}",
  55. 'desc_url' => $post['type'] == 'html'?config('customer.app_service_url').'/community/fragment/detail/'.$post['id']:'',
  56. ];
  57. }
  58. }