VideoTransformer.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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\PostTrait;
  13. use App\Traits\UserTrait;
  14. use Illuminate\Support\Facades\Redis;
  15. use League\Fractal\TransformerAbstract;
  16. class VideoTransformer extends TransformerAbstract
  17. {
  18. use UserTrait;
  19. use PostTrait;
  20. public function __construct($uid, $invite_code)
  21. {
  22. $this->uid = $uid;
  23. $this->invite_code = $invite_code;
  24. }
  25. public function transform(Post $post)
  26. {
  27. $isLike = 0;
  28. $isCollect = 0;
  29. $isFollow = 0;
  30. if($this->uid){
  31. $isLike = Redis::SISMEMBER('post_like_'.$post['id'], $this->uid);
  32. $isCollect = Redis::SISMEMBER('post_collect_'.$post['id'], $this->uid);
  33. $followStatus = $this->getFollowStatus($this->uid, $post['uid']);
  34. if($followStatus){
  35. $isFollow = $followStatus;
  36. }
  37. }
  38. $user = $this->userInfo($post['uid']);
  39. $postInfo = $this->getPostInfo($post['id']);
  40. return [
  41. 'id' => $post['id'],
  42. 'type' => $post['type'],
  43. 'uid' => $post['uid'],
  44. 'username' => $user['username'],
  45. 'avatar' => $user['avatar'],
  46. 'title' => $post['title'],
  47. 'content' => $post['content'],
  48. 'img' => $post['img'],
  49. 'video' => $post['video'],
  50. 'topic' => $this->getTopic($post['topic_ids']),
  51. 'praise_count' => $postInfo['praise_count'],
  52. 'collect_count' => $postInfo['collect_count'],
  53. 'comment_count' => $postInfo['comment_count'],
  54. 'will_collect_bean' => $postInfo['will_collect_bean'],
  55. 'is_like' => $isLike,
  56. 'is_collect' => $isCollect,
  57. 'is_follow' => $isFollow,
  58. 'h5url' => config('customer.share_post_h5url')."?post_id={$post['id']}&invite_code={$this->invite_code}",
  59. 'desc_url' => $post['type'] == 'html'?config('customer.app_service_url').'/community/fragment/detail/'.$post['id']:'',
  60. ];
  61. }
  62. }