SuggestTransformer.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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\UserTrait;
  15. use Carbon\Carbon;
  16. use League\Fractal\TransformerAbstract;
  17. class SuggestTransformer 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. $comment = [];
  32. $comments = PostComment::where('post_id', $post['id'])->where('parent_id', 0)->orderBy('id', 'desc')->limit(2)->get();
  33. foreach($comments as $item){
  34. $comment[] = [
  35. 'id' => $item->id,
  36. 'username' => subtext($item->username, 10),
  37. 'content' => $item->content,
  38. ];
  39. }
  40. $topic = [];
  41. foreach($post->topic() as $key => $val){
  42. $topic[] = [
  43. 'id' => $key,
  44. 'name' => $val
  45. ];
  46. }
  47. $isFollow = 0;
  48. $followStatus = $this->getFollowStatus($this->uid, $post['uid']);
  49. if($followStatus){
  50. $isFollow = $followStatus;
  51. }
  52. return [
  53. 'show_type' => 'post',
  54. 'id' => $post['id'],
  55. 'type' => $post['type'],
  56. 'created_at' => Carbon::parse($post['created_at'])->diffForHumans(),
  57. 'uid' => $post['uid'],
  58. 'username' => subtext($post['username'], 10),
  59. 'avatar' => $post['avatar'],
  60. 'topic' => $topic,
  61. 'title' => $post['title'],
  62. 'content' => $post['content'],
  63. 'location' => $post['location'],
  64. 'img' => $post['img'],
  65. 'imgs' => $imgs,
  66. 'video' => $post['video'],
  67. 'pv' => $post->data->pv,
  68. 'praise_count' => $post->data->praise_count,
  69. 'comment_count' => $post->data->comment_count,
  70. 'collect_count' => $post->data->collect_count,
  71. 'will_collect_bean' => $post->data->will_collect_bean + 3 * $post->data->pv,
  72. 'is_like' => PostLike::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0,
  73. 'is_dislike' => PostDislike::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0,
  74. 'is_collect' => PostCollect::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0,
  75. 'comment' => $comment,
  76. 'is_follow' => $isFollow,
  77. 'h5url' => config('customer.share_post_h5url')."?post_id={$post['id']}&invite_code={$this->invite_code}",
  78. 'desc_url' => $post['type'] == 'html'?config('customer.app_service_url').'/community/fragment/detail/'.$post['id']:'',
  79. ];
  80. }
  81. }