ListTransformer.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/6/15
  6. * Time: 17:59
  7. */
  8. namespace App\Transformers\Post;
  9. use App\Models\Post;
  10. use App\Models\PostLike;
  11. use App\Traits\UserTrait;
  12. use League\Fractal\TransformerAbstract;
  13. class ListTransformer extends TransformerAbstract
  14. {
  15. use UserTrait;
  16. public function __construct($uid, $invite_code)
  17. {
  18. $this->uid = $uid;
  19. $this->invite_code = $invite_code;
  20. }
  21. public function transform(Post $post)
  22. {
  23. $isLike = 0;
  24. if($this->uid){
  25. $isLike = PostLike::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0;
  26. }
  27. $user = $this->userInfo($post['uid']);
  28. return [
  29. 'id' => $post['id'],
  30. 'type' => $post['type'],
  31. 'uid' => $post['uid'],
  32. 'username' => $user['username'],
  33. 'avatar' => $user['avatar'],
  34. 'title' => $post['title'],
  35. 'content' => subtext($post['content'], 100),
  36. 'img' => $post['img'],
  37. 'praise_count' => $post->data->praise_count,
  38. 'is_like' => $isLike,
  39. 'h5url' => config('customer.share_post_h5url')."?post_id={$post['id']}&invite_code={$this->invite_code}",
  40. 'desc_url' => $post['type'] == 'html'?config('customer.app_service_url').'/community/fragment/detail/'.$post['id']:'',
  41. ];
  42. }
  43. }