SuggestTransformer.php 3.5 KB

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