DetailTransformer.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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\PostTrait;
  15. use App\Traits\UserTrait;
  16. use Carbon\Carbon;
  17. use Illuminate\Support\Facades\Redis;
  18. use League\Fractal\TransformerAbstract;
  19. class DetailTransformer extends TransformerAbstract
  20. {
  21. use UserTrait;
  22. use PostTrait;
  23. public function __construct($uid, $invite_code)
  24. {
  25. $this->uid = $uid;
  26. $this->invite_code = $invite_code;
  27. }
  28. public function transform(Post $post)
  29. {
  30. $isLike = 0;
  31. $isDislike = 0;
  32. $isCollect = 0;
  33. $isFollow = 0;
  34. if($this->uid){
  35. $isLike = Redis::SISMEMBER('post_like_'.$post['id'], $this->uid);
  36. $isDislike = Redis::SISMEMBER('post_unlike_'.$post['id'], $this->uid);
  37. $isCollect = Redis::SISMEMBER('post_collect_'.$post['id'], $this->uid);
  38. $followStatus = $this->getFollowStatus($this->uid, $post['uid']);
  39. if($followStatus){
  40. $isFollow = $followStatus;
  41. }
  42. }
  43. $user = $this->userInfo($post['uid']);
  44. $postInfo = $this->getPostInfo($post['id']);
  45. return [
  46. 'id' => $post['id'],
  47. 'type' => $post['type'],
  48. 'created_at' => Carbon::parse($post['created_at'])->diffForHumans(),
  49. 'uid' => $post['uid'],
  50. 'username' => $user['username'],
  51. 'avatar' => $user['avatar'],
  52. 'topic' => $this->getTopic($post['topic_ids']),
  53. 'topic_ids' => $post['topic_ids'],
  54. 'title' => $post['title'],
  55. 'content' => $post['content'],
  56. 'location' => $post['location'],
  57. 'img' => $post['img'],
  58. 'imgs' => $postInfo['imgs'],
  59. 'video' => $post['video'],
  60. 'pv' => getNumber($postInfo['pv']),
  61. 'praise_count' => $postInfo['praise_count'],
  62. 'collect_count' => $postInfo['collect_count'],
  63. 'comment_count' => $postInfo['comment_count'],
  64. 'available_bean' => $postInfo['available_bean'],
  65. 'will_collect_bean' => $postInfo['will_collect_bean'],
  66. 'is_like' => $isLike,
  67. 'is_dislike' => $isDislike,
  68. 'is_collect' => $isCollect,
  69. 'is_follow' => $isFollow,
  70. 'h5url' => config('customer.share_post_h5url')."?post_id={$post['id']}&invite_code={$this->invite_code}",
  71. 'desc_url' => $post['type'] == 'html'?config('customer.app_service_url').'/community/fragment/detail/'.$post['id']:'',
  72. ];
  73. }
  74. }