SuggestTransformer.php 2.4 KB

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