ListTransformer.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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 League\Fractal\TransformerAbstract;
  12. class ListTransformer extends TransformerAbstract
  13. {
  14. public function __construct($uid, $invite_code)
  15. {
  16. $this->uid = $uid;
  17. $this->invite_code = $invite_code;
  18. }
  19. public function transform(Post $post)
  20. {
  21. return [
  22. 'id' => $post['id'],
  23. 'type' => $post['type'],
  24. 'uid' => $post['uid'],
  25. 'username' => $post['username'],
  26. 'avatar' => $post['avatar'],
  27. 'title' => $post['title'],
  28. 'content' => subtext($post['content'], 100),
  29. 'img' => $post['img'],
  30. 'praise_count' => $post->data->praise_count,
  31. 'is_like' => PostLike::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0,
  32. 'h5url' => config('customer.share_post_h5url')."?post_id={$post['id']}&invite_code={$this->invite_code}",
  33. 'desc_url' => $post['type'] == 'html'?config('customer.app_service_url').'/community/fragment/detail/'.$post['id']:'',
  34. ];
  35. }
  36. }