VideoTransformer.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. $isLike = 0;
  25. $isCollect = 0;
  26. $isFollow = 0;
  27. if($this->uid){
  28. $isLike = PostLike::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0;
  29. $isCollect = PostCollect::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0;
  30. $followStatus = $this->getFollowStatus($this->uid, $post['uid']);
  31. if($followStatus){
  32. $isFollow = $followStatus;
  33. }
  34. }
  35. $topic = [];
  36. foreach($post->topic() as $key => $val){
  37. $topic[] = [
  38. 'id' => $key,
  39. 'name' => $val
  40. ];
  41. }
  42. $user = $this->userInfo($post['uid']);
  43. return [
  44. 'id' => $post['id'],
  45. 'type' => $post['type'],
  46. 'uid' => $post['uid'],
  47. 'username' => $user['username'],
  48. 'avatar' => $user['avatar'],
  49. 'title' => $post['title'],
  50. 'content' => $post['content'],
  51. 'img' => $post['img'],
  52. 'video' => $post['video'],
  53. 'topic' => $topic,
  54. 'praise_count' => $post->data->praise_count,
  55. 'collect_count' => $post->data->collect_count,
  56. 'comment_count' => $post->data->comment_count,
  57. 'will_collect_bean' => $post->data->will_collect_bean + 3 * $post->data->pv,
  58. 'is_like' => $isLike,
  59. 'is_collect' => $isCollect,
  60. 'is_follow' => $isFollow,
  61. 'h5url' => config('customer.share_post_h5url')."?post_id={$post['id']}&invite_code={$this->invite_code}",
  62. 'desc_url' => $post['type'] == 'html'?config('customer.app_service_url').'/community/fragment/detail/'.$post['id']:'',
  63. ];
  64. }
  65. }