123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2019/6/17
- * Time: 16:11
- */
- namespace App\Transformers\Post;
- use App\Models\Post;
- use App\Models\PostCollect;
- use App\Models\PostComment;
- use App\Models\PostDislike;
- use App\Models\PostLike;
- use App\Traits\UserTrait;
- use Carbon\Carbon;
- use League\Fractal\TransformerAbstract;
- class DetailTransformer extends TransformerAbstract
- {
- use UserTrait;
- public function __construct($uid, $invite_code)
- {
- $this->uid = $uid;
- $this->invite_code = $invite_code;
- }
- public function transform(Post $post)
- {
- $imgs = [];
- foreach($post->imgs as $img){
- $imgs[] = $img['img'];
- }
- $topic = [];
- foreach($post->topic() as $key => $val){
- $topic[] = [
- 'id' => $key,
- 'name' => $val
- ];
- }
- $isFollow = 0;
- $followStatus = $this->getFollowStatus($this->uid, $post['uid']);
- if($followStatus){
- $isFollow = $followStatus;
- }
- return [
- 'id' => $post['id'],
- 'type' => $post['type'],
- 'created_at' => Carbon::parse($post['created_at'])->diffForHumans(),
- 'uid' => $post['uid'],
- 'username' => subtext($post['username'], 10),
- 'avatar' => $post['avatar'],
- 'topic' => $topic,
- 'topic_ids' => $post['topic_ids'],
- 'title' => $post['title'],
- 'content' => $post['content'],
- 'location' => $post['location'],
- 'img' => $post['img'],
- 'imgs' => $imgs,
- 'video' => $post['video'],
- 'pv' => $post->data->pv,
- 'praise_count' => $post->data->praise_count,
- 'collect_count' => $post->data->collect_count,
- 'comment_count' => $post->data->comment_count,
- 'available_bean' => $post->data->available_bean,
- 'will_collect_bean' => $post->data->will_collect_bean + 3 * $post->data->pv,
- 'is_like' => PostLike::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0,
- 'is_dislike' => PostDislike::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0,
- 'is_collect' => PostCollect::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0,
- 'is_follow' => $isFollow,
- 'h5url' => config('customer.share_post_h5url')."?post_id={$post['id']}&invite_code={$this->invite_code}",
- 'desc_url' => $post['type'] == 'html'?config('customer.app_service_url').'/community/fragment/detail/'.$post['id']:'',
- ];
- }
- }
|