SuggestTransformer.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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('is_delete', 'asc')->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->is_delete?'该评论已被删除':$item->content,
  38. ];
  39. }
  40. $topic = [];
  41. foreach($post->topic() as $key => $val){
  42. $topic[] = [
  43. 'id' => $key,
  44. 'name' => $val
  45. ];
  46. }
  47. return [
  48. 'show_type' => 'post',
  49. 'id' => $post['id'],
  50. 'type' => $post['type'],
  51. 'created_at' => Carbon::parse($post['created_at'])->diffForHumans(),
  52. 'uid' => $post['uid'],
  53. 'username' => subtext($post['username'], 10),
  54. 'avatar' => $post['avatar'],
  55. 'topic' => $topic,
  56. 'title' => $post['title'],
  57. 'content' => $post['content'],
  58. 'location' => $post['location'],
  59. 'img' => $post['img'],
  60. 'imgs' => $imgs,
  61. 'video' => $post['video'],
  62. 'pv' => $post->data->pv,
  63. 'praise_count' => $post->data->praise_count,
  64. 'comment_count' => $post->data->comment_count,
  65. 'collect_count' => $post->data->collect_count,
  66. 'will_collect_bean' => $post->data->will_collect_bean + 3 * $post->data->pv,
  67. 'is_like' => PostLike::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0,
  68. 'is_dislike' => PostDislike::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0,
  69. 'is_collect' => PostCollect::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0,
  70. 'comment' => $comment,
  71. 'is_follow' => $this->getFollowStatus($this->uid, $post['uid']),
  72. 'h5url' => config('customer.share_post_h5url')."?post_id={$post['id']}&invite_code={$this->invite_code}",
  73. 'desc_url' => $post['type'] == 'html'?config('customer.app_service_url').'/community/fragment/detail/'.$post['id']:'',
  74. ];
  75. }
  76. }