DetailTransformer.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/6/17
  6. * Time: 16:11
  7. */
  8. namespace App\Transformers\Post;
  9. use App\Models\Post;
  10. use App\Models\PostCollect;
  11. use App\Models\PostComment;
  12. use App\Models\PostDislike;
  13. use App\Models\PostLike;
  14. use App\Traits\UserTrait;
  15. use Carbon\Carbon;
  16. use League\Fractal\TransformerAbstract;
  17. class DetailTransformer extends TransformerAbstract
  18. {
  19. use UserTrait;
  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. $imgs = [];
  28. foreach($post->imgs as $img){
  29. $imgs[] = $img['img'];
  30. }
  31. $topic = [];
  32. foreach($post->topic() as $key => $val){
  33. $topic[] = [
  34. 'id' => $key,
  35. 'name' => $val
  36. ];
  37. }
  38. $isFollow = 0;
  39. $followStatus = $this->getFollowStatus($this->uid, $post['uid']);
  40. if($followStatus){
  41. $isFollow = $followStatus;
  42. }
  43. return [
  44. 'id' => $post['id'],
  45. 'type' => $post['type'],
  46. 'created_at' => Carbon::parse($post['created_at'])->diffForHumans(),
  47. 'uid' => $post['uid'],
  48. 'username' => subtext($post['username'], 10),
  49. 'avatar' => $post['avatar'],
  50. 'topic' => $topic,
  51. 'title' => $post['title'],
  52. 'content' => $post['content'],
  53. 'location' => $post['location'],
  54. 'img' => $post['img'],
  55. 'imgs' => $imgs,
  56. 'video' => $post['video'],
  57. 'pv' => $post->data->pv,
  58. 'praise_count' => $post->data->praise_count,
  59. 'collect_count' => $post->data->collect_count,
  60. 'comment_count' => $post->data->comment_count,
  61. 'available_bean' => $post->data->available_bean,
  62. 'will_collect_bean' => $post->data->will_collect_bean + 3 * $post->data->pv,
  63. 'is_like' => PostLike::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0,
  64. 'is_dislike' => PostDislike::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0,
  65. 'is_collect' => PostCollect::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0,
  66. 'is_follow' => $isFollow,
  67. 'h5url' => config('customer.share_post_h5url')."?post_id={$post['id']}&invite_code={$this->invite_code}",
  68. 'desc_url' => $post['type'] == 'html'?config('customer.app_service_url').'/community/fragment/detail/'.$post['id']:'',
  69. ];
  70. }
  71. }