ListTransformer.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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\PostTrait;
  12. use App\Traits\UserTrait;
  13. use Illuminate\Support\Facades\Redis;
  14. use League\Fractal\TransformerAbstract;
  15. class ListTransformer extends TransformerAbstract
  16. {
  17. use UserTrait;
  18. use PostTrait;
  19. public function __construct($uid, $invite_code)
  20. {
  21. $this->uid = $uid;
  22. $this->invite_code = $invite_code;
  23. }
  24. public function transform(Post $post)
  25. {
  26. $isLike = 0;
  27. if($this->uid){
  28. $isLike = Redis::SISMEMBER('post_like_'.$post['id'], $this->uid);
  29. }
  30. $user = $this->userInfo($post['uid']);
  31. $postInfo = $this->getPostInfo($post['id']);
  32. return [
  33. 'id' => $post['id'],
  34. 'type' => $post['type'],
  35. 'uid' => $post['uid'],
  36. 'username' => $user['username'],
  37. 'avatar' => $user['avatar'],
  38. 'title' => $post['title'],
  39. 'content' => subtext($post['content'], 100),
  40. 'img' => $post['img'],
  41. 'topic' => $this->getTopic($post['topic_ids']),
  42. 'praise_count' => $postInfo['praise_count'],
  43. 'is_like' => $isLike,
  44. 'h5url' => config('customer.share_post_h5url')."?post_id={$post['id']}&invite_code={$this->invite_code}",
  45. 'desc_url' => $post['type'] == 'html'?config('customer.app_service_url').'/community/fragment/detail/'.$post['id']:'',
  46. ];
  47. }
  48. }