SuggestTransformer.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/6/15
  6. * Time: 11:07
  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\FollowStatusTrait;
  15. use App\Traits\UserTrait;
  16. use Carbon\Carbon;
  17. use League\Fractal\TransformerAbstract;
  18. class SuggestTransformer extends TransformerAbstract
  19. {
  20. // use UserTrait;
  21. use FollowStatusTrait;
  22. public function __construct($uid, $invite_code)
  23. {
  24. $this->uid = $uid;
  25. $this->invite_code = $invite_code;
  26. }
  27. public function transform(Post $post)
  28. {
  29. $imgs = [];
  30. foreach($post->imgs as $img){
  31. $imgs[] = $img['img'];
  32. }
  33. $comment = [];
  34. $comments = PostComment::where('post_id', $post['id'])->where('parent_id', 0)->orderBy('id', 'desc')->limit(2)->get();
  35. foreach($comments as $item){
  36. $comment[] = [
  37. 'id' => $item->id,
  38. 'username' => subtext($item->username, 10),
  39. 'content' => $item->content,
  40. ];
  41. }
  42. $topic = [];
  43. foreach($post->topic() as $key => $val){
  44. $topic[] = [
  45. 'id' => $key,
  46. 'name' => $val
  47. ];
  48. }
  49. // $isFollow = 0;
  50. // $followStatus = $this->getFollowStatus($this->uid, $post['uid']);
  51. // if($followStatus){
  52. // $isFollow = $followStatus;
  53. // }
  54. return [
  55. 'show_type' => 'post',
  56. 'id' => $post['id'],
  57. 'type' => $post['type'],
  58. 'created_at' => Carbon::parse($post['created_at'])->diffForHumans(),
  59. 'uid' => $post['uid'],
  60. 'username' => subtext($post['username'], 10),
  61. 'avatar' => $post['avatar'],
  62. 'topic' => $topic,
  63. 'title' => $post['title'],
  64. 'content' => $post['content'],
  65. 'location' => $post['location'],
  66. 'img' => $post['img'],
  67. 'imgs' => $imgs,
  68. 'video' => $post['video'],
  69. 'pv' => $post->data->pv,
  70. 'praise_count' => $post->data->praise_count,
  71. 'comment_count' => $post->data->comment_count,
  72. 'collect_count' => $post->data->collect_count,
  73. 'will_collect_bean' => $post->data->will_collect_bean + 3 * $post->data->pv,
  74. 'is_like' => PostLike::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0,
  75. 'is_dislike' => PostDislike::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0,
  76. 'is_collect' => PostCollect::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0,
  77. 'comment' => $comment,
  78. 'is_follow' => $this->followStatus($this->uid, $post['uid']),
  79. 'h5url' => config('customer.share_post_h5url')."?post_id={$post['id']}&invite_code={$this->invite_code}",
  80. 'desc_url' => $post['type'] == 'html'?config('customer.app_service_url').'/community/fragment/detail/'.$post['id']:'',
  81. ];
  82. }
  83. }