wzq пре 5 година
родитељ
комит
1fed46c398

+ 5 - 3
app/Console/Commands/CalcPostWeight.php

@@ -2,6 +2,7 @@
 
 namespace App\Console\Commands;
 
+use App\Models\Post;
 use App\Models\PostData;
 use Carbon\Carbon;
 use Illuminate\Console\Command;
@@ -48,7 +49,8 @@ class CalcPostWeight extends Command
         Log::debug('权重帖子ID:' . json_encode($postIds));
         foreach ($postIds as $postId) {
             $postInfo = PostData::where("post_id", $postId)->first();
-            if(empty($postInfo)){
+            $post = Post::find($postId);
+            if(empty($postInfo) || empty($post)){
                 continue;
             }
             Log::debug('帖子:' . json_encode($postInfo));
@@ -64,8 +66,8 @@ class CalcPostWeight extends Command
             }else{
                 $score = $fresh / 86400;
             }
-            $postInfo->weight = $score;
-            $postInfo->save();
+            $post->weight = $score;
+            $post->save();
             Redis::srem($key,$postId);
             Log::debug(date("Y-m-d H:i:s") . "设置帖子" . $postInfo->post_id . "的权重分为:" . $score);
         }

+ 2 - 1
app/Http/Middleware/SignAuthMiddleware.php

@@ -3,6 +3,7 @@
 namespace App\Http\Middleware;
 
 use Closure;
+use Illuminate\Support\Facades\Log;
 
 class SignAuthMiddleware
 {
@@ -15,7 +16,7 @@ class SignAuthMiddleware
      */
     public function handle($request, Closure $next)
     {
-
+        Log::info('签名--' . generateSign($request->all(),config('customer.app_secret')));
         try {
             if (!verifySign($request->get('sign'), $request->all(), config('customer.app_secret'))) {
                 $error = [

+ 0 - 5
app/Models/Post.php

@@ -31,9 +31,4 @@ class Post extends Model
     {
         return Topic::whereIn('id', explode(',', $this->topic_ids))->pluck('name', 'id');
     }
-
-    public function comment(){
-        return $this->hasOne('App\Models\PostComment', 'post_id', 'id');
-    }
-
 }

+ 2 - 2
app/Models/PostMusicCategoryRel.php

@@ -16,12 +16,12 @@ class PostMusicCategoryRel extends Model {
     protected $guarded = [];
 
     //音乐
-    public function postMusic()
+    public function post_music()
     {
         return $this->hasOne('App\Models\PostMusic', 'id', 'mid');
     }
     //音乐分类
-    public function postCategory()
+    public function post_music_category()
     {
         return $this->hasOne('App\Models\PostMusicCategory', 'id', 'music_category_id');
     }

+ 12 - 21
app/Repositories/BeanRepository.php

@@ -1,6 +1,7 @@
 <?php
 namespace App\Repositories;
 use App\Models\Post;
+use App\Traits\PostTrait;
 use Illuminate\Support\Carbon;
 use Illuminate\Support\Facades\Auth;
 use Illuminate\Support\Facades\Log;
@@ -8,6 +9,7 @@ use Tymon\JWTAuth\Facades\JWTAuth;
 use Illuminate\Support\Facades\Redis;
 class BeanRepository
 {
+    use PostTrait;
     public function beanDetail($request)
     {
         try {
@@ -83,7 +85,7 @@ class BeanRepository
                 if ($user_data) {
                     foreach ($new_arr as $k => $v) {
                         if (!isset($user_data[$v['uid']])) continue;
-                        $new_arr[$k]['follow_status'] = $user_data[$v['uid']]['follow_status'];
+//                        $new_arr[$k]['follow_status'] = $user_data[$v['uid']]['follow_status'];
                         $new_arr[$k]['username'] = $user_data[$v['uid']]['username'];
                         $new_arr[$k]['avatar'] = $user_data[$v['uid']]['avatar'];
                     }
@@ -106,7 +108,7 @@ class BeanRepository
                     foreach ($registered_most as $k=>$v){
                         if(!isset($user_data[$v['superior_uid']])) continue;
                         $registered_most[$k]['count'] = intval($v['count']);
-                        $registered_most[$k]['follow_status'] = $user_data[$v['superior_uid']]['follow_status'];
+//                        $registered_most[$k]['follow_status'] = $user_data[$v['superior_uid']]['follow_status'];
                         $registered_most[$k]['username'] = $user_data[$v['superior_uid']]['username'];
                         $registered_most[$k]['avatar'] = $user_data[$v['superior_uid']]['avatar'];
                     }
@@ -129,7 +131,7 @@ class BeanRepository
                     foreach ($all_best_author as $k=>$v){
                         if(!isset($user_data[$v['content_author_id']])) continue;
                         $all_best_author[$k]['count'] = intval($v['count']);
-                        $all_best_author[$k]['follow_status'] = $user_data[$v['content_author_id']]['follow_status'];
+//                        $all_best_author[$k]['follow_status'] = $user_data[$v['content_author_id']]['follow_status'];
                         $all_best_author[$k]['username'] = $user_data[$v['content_author_id']]['username'];
                         $all_best_author[$k]['avatar'] = $user_data[$v['content_author_id']]['avatar'];
                     }
@@ -203,10 +205,9 @@ class BeanRepository
     //获取每日新闻
     function getNews($request) {
         try {
-            $sign = generateSign([], config('customer.app_secret'));
             $url = config("customer.app_service_url").'/config/v2/starNews/lists';
             $array = [
-                'json' => ['sign' => $sign], 'query' => [], 'http_errors' => false
+                'json' => [], 'query' => [], 'http_errors' => false
             ];
             return http($url,$array,'get');
         } catch (\Exception $e) {
@@ -218,11 +219,11 @@ class BeanRepository
     //获取平台内容
     function getPlatformContent($id) {
         try {
-            $url = config("customer.app_service_url").'/config/v2/platformContent/lists';
+            $url = config("customer.app_service_url") . '/config/v2/platformContent/lists';
             $array = [
-                'json' => ['id'=>$id], 'query' => [], 'http_errors' => false,'headers'=>['Authorization'=>"Bearer ".JWTAuth::getToken()]
+                'json' => ['id' => $id], 'query' => [], 'http_errors' => false, 'headers' => ['Authorization' => "Bearer " . JWTAuth::getToken()]
             ];
-            return http($url,$array,'get');
+            return http($url, $array, 'get');
         } catch (\Exception $e) {
             Log::debug($e->getMessage());
             return [];
@@ -244,21 +245,11 @@ class BeanRepository
         Log::debug($yesterday_quantity_issued);
         Log::debug($exchange);
 
-        $post_detail = new Post();
         if (isset($request['post_id']) && $request['post_id']){
-            $post = $post_detail->
-                join('post_data', 'post_data.post_id', '=', 'post.id')
-                ->select('post.*','post_data.pv')
-                ->find($request['post_id']);
+            $post = $this->getPostInfo($request['post_id'], 1);
             if ($post){
-                $post->created_time = Carbon::parse($post->created_at)->diffForHumans();
-                $imgs = [];
-                foreach($post->imgs as $img){
-                    $imgs[] = $img['img'];
-                }
-                unset($post->imgs);
-                $post->imgs = $imgs;
-                $post->topic = $post->topic();
+                $post['created_time'] = Carbon::parse($post['created_at'])->diffForHumans();
+                $post['topic'] = $this->getTopic($post['topic_ids']);
                 $star_detail['post'] = $post;
             }else{
                 Log::debug($request['post_id']);

+ 28 - 52
app/Repositories/FeedRepositories.php

@@ -12,6 +12,7 @@ namespace App\Repositories;
 use App\Models\Behavior;
 use App\Models\Feed;
 use App\Models\PostComment;
+use App\Traits\PostTrait;
 use Carbon\Carbon;
 use Illuminate\Support\Facades\DB;
 use Illuminate\Support\Facades\Log;
@@ -25,6 +26,7 @@ use App\Traits\UserTrait;
 class FeedRepositories
 {
     use UserTrait;
+    use PostTrait;
 
     public function __construct(PostRepositories $postRepositories, PostComment $postComment)
     {
@@ -155,7 +157,7 @@ class FeedRepositories
         if ($data) {
             foreach ($data as $key => &$value) {
                 if ($value['type'] == 6) {
-                    $post = $this->postRepositories->detail($value['relate_id']);
+                    $post = $this->getPostInfo($value['relate_id'], 1);
                     if ($post) {
                         $value['relate_data'] = $this->postDetail($post, $value['follow_uid'], $userInfo);
                     } else {
@@ -182,72 +184,46 @@ class FeedRepositories
         return $data;
     }
 
-    public function postDetail($post, $follow_uid, $userInfo)
+    public function postDetail($postInfo, $follow_uid, $userInfo)
     {
-        Log::debug('feed流内容--' . json_encode($post));
+        Log::debug('feed流内容--' . json_encode($postInfo));
         $uid = $userInfo['uid'];
-        $imgs = [];
-        foreach ($post->imgs as $img) {
-            $imgs[] = $img['img'];
-        }
-
-        $topic = [];
-        foreach ($post->topic() as $key => $val) {
-            $topic[] = [
-                'id' => $key,
-                'name' => $val
-            ];
-        }
         $isFollow = 0;
         Log::debug("内容feed关注uid{$uid}followUid{$follow_uid}");
         $followStatus = $this->getFollowStatus($uid, $follow_uid);
         if ($followStatus) {
             $isFollow = $followStatus;
         }
-        $user = $this->userInfo($post['uid']);
+        $user = $this->userInfo($postInfo['uid']);
         return [
-            'id' => $post['id'],
-            'type' => $post['type'],
-            'created_at' => Carbon::parse($post['created_at'])->diffForHumans(),
-            'uid' => $post['uid'],
+            'id' => (int) $postInfo['id'],
+            'type' => $postInfo['type'],
+            'created_at' => Carbon::parse($postInfo['created_at'])->diffForHumans(),
+            'uid' => (int) $postInfo['uid'],
             'username' => $user['username'],
             'avatar' => $user['avatar'],
-            'topic' => $topic,
-            'title' => $post['title'],
-            'content' => $post['content'],
-            'location' => $post['location'],
-            'img' => $post['img'],
-            'imgs' => $imgs,
-            'video' => $post['video'],
-            'pv' => getNumber($post->data->pv),
-            'praise_count' => $post->data->praise_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,
-            'post_comment' => $this->getPostComment($post['id']),
-            'is_like' => PostLike::where('post_id', $post['id'])->where('uid', $uid)->exists() ? 1 : 0,
-            'is_dislike' => PostDislike::where('post_id', $post['id'])->where('uid', $uid)->exists() ? 1 : 0,
-            'is_collect' => PostCollect::where('post_id', $post['id'])->where('uid', $uid)->exists() ? 1 : 0,
+            'topic' => $this->getTopic($postInfo['topic_ids']),
+            'title' => $postInfo['title'],
+            'content' => $postInfo['content'],
+            'location' => $postInfo['location'],
+            'img' => $postInfo['img'],
+            'imgs' => $postInfo['imgs'],
+            'video' => $postInfo['video'],
+            'pv' => getNumber($postInfo['pv']),
+            'praise_count' => $postInfo['praise_count'],
+            'comment_count' => $postInfo['comment_count'],
+            'available_bean' => $postInfo['available_bean'],
+            'will_collect_bean' => $postInfo['will_collect_bean'],
+            'post_comment' => $this->getNewComment($postInfo['id']),
+            'is_like' => Redis::SISMEMBER('post_like_'.$postInfo['id'], $uid),
+            'is_dislike' => Redis::SISMEMBER('post_unlike_'.$postInfo['id'], $uid),
+            'is_collect' => Redis::SISMEMBER('post_collect_'.$postInfo['id'], $uid),
             'follow_status' => $isFollow,
-            'h5url' => config('customer.share_post_h5url') . "?post_id={$post['id']}&invite_code={$userInfo['invite_code']}",
-            'desc_url' => $post['type'] == 'html' ? config('customer.app_service_url') . '/community/fragment/detail/' . $post['id'] : '',
+            'h5url' => config('customer.share_post_h5url') . "?post_id={$postInfo['id']}&invite_code={$userInfo['invite_code']}",
+            'desc_url' => $postInfo['type'] == 'html' ? config('customer.app_service_url') . '/community/fragment/detail/' . $postInfo['id'] : '',
         ];
     }
 
-    public function getPostComment($post_id)
-    {
-        $comments = $this->postComment->where(['post_id' => $post_id, 'parent_id' => 0])->select('id', 'uid', 'content', 'is_delete')->orderBy('is_delete', 'asc')->orderBy('id', 'desc')->take(2)->get();
-        foreach ($comments as &$comment) {
-            if ($comment->is_delete) {
-                $comment->content = '该评论已被删除';
-            }
-            unset($comment->is_delete);
-            $user = $this->userInfo($comment->uid);
-            $comment->username = $user['username'];
-        }
-        return $comments;
-    }
-
     /**
      *  取消关注删除对应feed
      * @param $data

+ 1 - 0
app/Repositories/MusicRepository.php

@@ -59,6 +59,7 @@ class MusicRepository
         $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
 
         $postMusicList = $this->postMusicCategoryRel
+            ->with('post_music')
             ->join('post_music_category', 'post_music_category.id', '=', 'post_music_category_rel.music_category_id')
             ->join('post_music', 'post_music.id', '=', 'post_music_category_rel.mid')
             ->select('post_music.*', 'post_music_category_rel.sort')

+ 72 - 29
app/Repositories/PostRepositories.php

@@ -136,6 +136,8 @@ class PostRepositories
             }
         }
 
+        $fresh = (Carbon::now()->timestamp) - (Carbon::parse("2019-05-01 00:00:00")->timestamp);
+        $score = $fresh / 86400;
         $data = [
             'uid' => $userInfo['uid'],
             'username' => $userInfo['username'],
@@ -150,14 +152,12 @@ class PostRepositories
             'content' => $request['content'],
             'location' => isset($request['location'])? $request['location'] : '',
             'is_suggest' => 0,
-            'is_hide' => 0
+            'is_hide' => 0,
+            'weight' => $score
         ];
 
         $date = date('Y-m-d H:i:s');
 
-        $fresh = (Carbon::now()->timestamp) - (Carbon::parse("2019-05-01 00:00:00")->timestamp);
-        $score = $fresh / 86400;
-
         DB::beginTransaction();
         try{
             $post = $this->post->create($data);
@@ -176,8 +176,7 @@ class PostRepositories
                 'collect_real_count' => 0,
                 'available_bean' => $this->availableBean(),
                 'will_collect_bean' => rand(100, 200),
-                'collect_bean' => 0,
-                'weight' => $score
+                'collect_bean' => 0
             ]);
 
             if($imgs){
@@ -198,6 +197,29 @@ class PostRepositories
             foreach($topicIds as $id){
                 Redis::zincrby('topic.user_uid'.$userInfo['uid'], 1, $id);
             }
+            Redis::HSET('post_info_'.$post->id,
+                'id', $post->id,
+                'uid', $post->uid,
+                'type', $post->type,
+                'img', $post->img,
+                'imgs', json_encode($imgs),
+                'video', $post->video,
+                'topic_ids', $post->topic_ids,
+                'title', $post->title,
+                'content', $post->content,
+                'location', $post->location,
+                'pv', $postData->pv,
+                'dislike_count', $postData->dislike_count,
+                'praise_count', $postData->praise_count,
+                'share_count', $postData->share_count,
+                'comment_count', $postData->comment_count,
+                'collect_count', $postData->collect_count,
+                'available_bean', $postData->available_bean,
+                'will_collect_bean', $postData->will_collect_bean,
+                'create_bean', $postData->create_bean,
+                'collect_bean', $postData->collect_bean,
+                'created_at', $post->created_at);
+
             return jsonSuccess([
                 'post_id' => $post->id,
                 'h5url' => config('customer.share_post_h5url')."?post_id={$post->id}&invite_code={$userInfo['invite_code']}",
@@ -278,7 +300,16 @@ class PostRepositories
         try{
             $comment = $this->postComment->create($data);
 
+            if($comment->parent_id){
+                $this->postComment->where('id', $comment->parent_id)->increment('reply_count');
+            }
+
             DB::commit();
+            if(!$comment->parent_id){
+                Redis::DEL('post_new_comment_'.$comment->post_id);
+            }else{
+                Redis::DEL('post_new_reply_'.$comment->id);
+            }
             return jsonSuccess(['id' => $comment->id], '评论成功');
 
         }catch (QueryException $exception){
@@ -316,6 +347,11 @@ class PostRepositories
             $comment->save();
 
             DB::commit();
+            if(!$comment->parent_id){
+                Redis::DEL('post_new_comment_'.$comment->post_id);
+            }else{
+                Redis::DEL('post_new_reply_'.$comment->id);
+            }
             Redis::SADD('delete_post_comment_ids', $comment->id);
             return jsonSuccess('删除评论成功');
 
@@ -335,8 +371,6 @@ class PostRepositories
         $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
 
         return $this->post
-            ->join('post_data', 'post_data.post_id', '=', 'post.id')
-            ->select('post.*')
             ->where(function($query) use ($request){
                 if(isset($request['keyword'])){
                     $query->where('title', 'like', "%{$request['keyword']}%")
@@ -348,9 +382,9 @@ class PostRepositories
                     $topicIds = json_decode($request['topic_ids'], true);
                     foreach ($topicIds as $key=>$id) {
                         if ($key==0) {
-                            $query->whereRaw('FIND_IN_SET('.$id.', post.topic_ids)');
+                            $query->whereRaw('FIND_IN_SET('.$id.', topic_ids)');
                         } else {
-                            $query->orWhereRaw('FIND_IN_SET('.$id.', post.topic_ids)');
+                            $query->orWhereRaw('FIND_IN_SET('.$id.', topic_ids)');
                         }
                     }
                 }
@@ -383,30 +417,26 @@ class PostRepositories
                 }
                 Log::debug('热门视频ids'.$ids);
                 return $this->post
-                    ->join('post_data', 'post_data.post_id', '=', 'post.id')
-                    ->select('post.*', DB::raw("IF (post.id = {$id},1,0) as sort"))
+                    ->select('*', DB::raw("IF (id = {$id},1,0) as sort"))
                     ->where($where)
-                    ->whereIn('post.id', explode(',', $ids))
+                    ->whereIn('id', explode(',', $ids))
                     ->orderBy('sort', 'desc')
-                    ->orderByRaw(DB::raw("FIELD(post.id,{$ids})"))
+                    ->orderByRaw(DB::raw("FIELD(id,{$ids})"))
                     ->paginate($perPage);
             }elseif($request['type'] == 'one'){
-                $where[] = ['post.id', $id];
+                $where[] = ['id', $id];
                 return $this->post
-                    ->join('post_data', 'post_data.post_id', '=', 'post.id')
-                    ->select('post.*')
                     ->where($where)
                     ->paginate($perPage);
             }
 
         }
         return $this->post
-            ->join('post_data', 'post_data.post_id', '=', 'post.id')
-            ->select('post.*', DB::raw("IF (post.id = {$id},1,0) as sort"))
+            ->select('*', DB::raw("IF (id = {$id},1,0) as sort"))
             ->where($where)
             ->where(function($query) use ($request){
                 if(isset($request['topic_id']) && $request['topic_id']){
-                    $query->whereRaw('FIND_IN_SET('.$request['topic_id'].', post.topic_ids)');
+                    $query->whereRaw('FIND_IN_SET('.$request['topic_id'].', topic_ids)');
                 }
             })
             ->orderBy('sort', 'desc')
@@ -437,7 +467,6 @@ class PostRepositories
             $order = 'post_share.updated_at';
         }
         return $post
-            ->join('post_data', 'post_data.post_id', '=', 'post.id')
             ->select('post.*')
             ->where($where)
             ->orderBy($order,'desc')
@@ -450,8 +479,6 @@ class PostRepositories
     public function detail($id)
     {
         return $this->post
-            ->join('post_data', 'post_data.post_id', '=', 'post.id')
-            ->select('post.*')
             ->find($id);
     }
 
@@ -471,8 +498,6 @@ class PostRepositories
         $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
 
         return $this->post
-            ->join('post_data', 'post_data.post_id', '=', 'post.id')
-            ->select('post.*')
             ->orderBy('is_suggest','desc')
             ->orderBy('weight','desc')
             ->paginate($perPage);
@@ -486,9 +511,7 @@ class PostRepositories
         $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
 
         return $this->post
-            ->join('post_data', 'post_data.post_id', '=', 'post.id')
-            ->select('post.*')
-            ->whereRaw('FIND_IN_SET(' . $request['id'] . ',post.topic_ids)')
+            ->whereRaw('FIND_IN_SET(' . $request['id'] . ',topic_ids)')
             ->orderBy('id','desc')
             ->paginate($perPage);
     }
@@ -554,7 +577,6 @@ class PostRepositories
     public function myTopicList($request)
     {
         $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
-
         $uid = 0;
         $user = $this->getUserInfo();
         if($user){
@@ -580,31 +602,45 @@ class PostRepositories
             Log::debug("非帖子类操作,不操作帖子统计数量".json_encode($request));
             return true;
         }
+        //帖子缓存信息key
+        $postInfoKey = "post_info_".$postId;
         $post = PostData::where('post_id', $postId)->first();
         if(!$post) return true;
         if (isset($request['behavior_flag']) && $request['behavior_flag'] == 'read') {
             $post->pv += 1;
             $post->pv_real += 1;
+            Redis::HINCRBY($postInfoKey,'pv',1);
             Log::debug("帖子:".$postId."被阅读,pv +1");
         } elseif (isset($request['behavior_flag']) && $request['behavior_flag'] == 'unlike') {
+            //用户不喜欢帖子key
+            $postUnLikeKey = "post_unlike_".$postId;
             $post->dislike_count += 1;
             PostDislike::create(['uid'=>$request['target_id'],'post_id'=>$request['post_id']]);
+            Redis::sadd($postUnLikeKey,$request['target_id']);
+            Redis::HINCRBY($postInfoKey,'dislike_count',1);
             Log::debug("帖子:".$postId."被不喜欢,unlike +1");
         } elseif (isset($request['behavior_flag']) && $request['behavior_flag'] == 'like') {
+            //用户点赞帖子
+            $postLikeKey = "post_like_".$postId;
             if($request['behavior_value']){
                 $post->praise_count += 1;
                 $post->praise_real_count += 1;
                 PostLike::create(['uid'=>$request['target_id'],'post_id'=>$request['post_id']]);
+                Redis::sadd($postLikeKey,$request['target_id']);
+                Redis::HINCRBY($postInfoKey,'praise_count',1);
                 Log::debug("帖子:".$postId."被点赞,praise_count +1");
             }else{
                 $post->praise_count -= 1;
                 $post->praise_real_count -= 1;
                 PostLike::where(['uid'=>$request['target_id'],'post_id'=>$request['post_id']])->delete();
+                Redis::srem($postLikeKey,$request['target_id']);
+                Redis::HINCRBY($postInfoKey,'praise_count',-1);
                 Log::debug("帖子:".$postId."被取消点赞,praise_count -1");
             }
         } elseif (isset($request['behavior_flag']) && $request['behavior_flag'] == 'forward') {
             $post->share_count += 1;
             $post->share_real_count += 1;
+            Redis::HINCRBY($postInfoKey,'share_count',1);
             $shareRow = PostShare::where(['uid'=>$request['target_id'],'post_id'=>$request['post_id']])->first();
             if($shareRow){
                 PostShare::where(['uid'=>$request['target_id'],'post_id'=>$request['post_id']])->update(['uid'=>$request['target_id'],'post_id'=>$request['post_id']]);
@@ -614,17 +650,24 @@ class PostRepositories
             Log::debug("帖子:".$postId."被分享,share_count +1");
         } elseif (isset($request['behavior_flag']) && $request['behavior_flag'] == 'comment') {
             $post->comment_count += 1;
+            Redis::HINCRBY($postInfoKey,'comment_count',1);
             Log::debug("帖子:".$postId."被评论,comment_count +1");
         } elseif (isset($request['behavior_flag']) && $request['behavior_flag'] == 'collect') {
+            //用户收藏帖子
+            $postCollectKey = "post_collect_".$postId;
             if($request['behavior_value']) {
                 $post->collect_count += 1;
                 $post->collect_real_count += 1;
                 PostCollect::create(['uid'=>$request['target_id'],'post_id'=>$request['post_id']]);
+                Redis::sadd($postCollectKey,$request['target_id']);
+                Redis::HINCRBY($postInfoKey,'collect_count',1);
                 Log::debug("帖子:".$postId."被收藏,collect_count +1");
             }else{
                 $post->collect_count -= 1;
                 $post->collect_real_count -= 1;
                 PostCollect::where(['uid'=>$request['target_id'],'post_id'=>$request['post_id']])->delete();
+                Redis::srem($postCollectKey,$request['target_id']);
+                Redis::HINCRBY($postInfoKey,'collect_count',-1);
                 Log::debug("帖子:".$postId."被取消收藏,collect_count -1");
             }
         }

+ 65 - 4
app/Traits/PostTrait.php

@@ -9,14 +9,11 @@
 namespace App\Traits;
 
 use App\Models\Post;
+use App\Models\PostComment;
 use Illuminate\Support\Facades\Redis;
 
 trait PostTrait
 {
-//    public function __construct(Post $post) {
-//        $this->post = $post;
-//    }
-
     //预计可获得U米数
     public function availableBean()
     {
@@ -43,4 +40,68 @@ trait PostTrait
             ->select('post.*')
             ->find($id);
     }
+
+    //获取内容话题
+    public function getTopic($topic_ids)
+    {
+        $ids = explode(',', $topic_ids);
+        $topic = [];
+        foreach($ids as $id){
+            $name = $topicNameArray = Redis::ZRANGEBYSCORE('topic.name', $id, $id);
+            if($name && isset($name[0])){
+                $topic[] = [
+                    'id' => intval($id),
+                    'name' => $name[0],
+                ];
+            }
+        }
+        return $topic;
+    }
+
+    //获取内容详情
+    public function getPostInfo($id, $type = 0)
+    {
+        if($type){
+            $res = Redis::SISMEMBER('delete_post_ids', $id);
+            if($res){
+                return [];
+            }
+        }
+        $data =  Redis::HGETALL('post_info_'.$id);
+        if($data){
+            $data['praise_count'] = intval($data['praise_count']);
+            $data['comment_count'] = intval($data['comment_count']);
+            $data['collect_count'] = intval($data['collect_count']);
+            $data['available_bean'] = intval($data['available_bean']);
+            $data['collect_bean'] = intval($data['collect_bean']);
+            $data['will_collect_bean'] = $data['will_collect_bean'] + 3 * $data['pv'];
+            $data['imgs'] = json_decode($data['imgs'], true);
+        }
+        return $data;
+    }
+
+    //获取内容最新评论
+    public function getNewComment($id)
+    {
+        $comment = [];
+        $commentKey = 'post_new_comment_'.$id;
+        $commentData = Redis::GET($commentKey);
+        if($commentData){
+            $comment = json_decode($commentData);
+        }else{
+            $comments = PostComment::where('post_id', $id)->where('parent_id', 0)->orderBy('is_delete', 'asc')->orderBy('id', 'desc')->limit(2)->get();
+            foreach($comments as $item){
+                $userComment = $this->userInfo($item->uid);
+                $comment[] = [
+                    'id' => $item->id,
+                    'username' => $userComment['username'],
+                    'content' => $item->is_delete?'该评论已被删除':$item->content,
+                    'is_delete' => $item->is_delete,
+                ];
+            }
+            Redis::SET($commentKey, json_encode($comment));
+            Redis::EXPIRE($commentKey, 300);
+        }
+        return $comment;
+    }
 }

+ 26 - 17
app/Transformers/Post/CommentTransformer.php

@@ -10,6 +10,7 @@ namespace  App\Transformers\Post;
 use App\Models\PostComment;
 use App\Traits\UserTrait;
 use Carbon\Carbon;
+use Illuminate\Support\Facades\Redis;
 use League\Fractal\TransformerAbstract;
 
 class CommentTransformer extends TransformerAbstract
@@ -17,24 +18,32 @@ class CommentTransformer extends TransformerAbstract
     use UserTrait;
     public function transform(PostComment $postComment)
     {
-        $replies = PostComment::where('parent_id', $postComment['id'])->orderBy('id', 'desc')->limit(2)->get();
         $reply = [];
-        foreach($replies as $val){
-            $userComment = $this->userInfo($val->uid);
-            $replyUsername = '';
-            if($val->reply_uid){
-                $userReply = $this->userInfo($val->reply_uid);
-                $replyUsername = $userReply['username'];
+        $replyKey = 'post_new_reply_'.$postComment['id'];
+        $replyData = Redis::GET($replyKey);
+        if($replyData){
+            $reply = json_decode($replyData);
+        }else{
+            $replies = PostComment::where('parent_id', $postComment['id'])->orderBy('id', 'desc')->limit(2)->get();
+            foreach($replies as $val){
+                $userComment = $this->userInfo($val->uid);
+                $replyUsername = '';
+                if($val->reply_uid){
+                    $userReply = $this->userInfo($val->reply_uid);
+                    $replyUsername = $userReply['username'];
+                }
+                $reply[] = [
+                    'uid' => $val->uid,
+                    'username' => $userComment['username'],
+                    'avatar' => $userComment['avatar'],
+                    'reply_username' => $replyUsername,
+                    'content' => $val->is_delete?'该评论已被删除':$val->content,
+                    'created_at' => Carbon::parse($val->created_at)->diffForHumans(),
+                    'is_delete' => $val->is_delete,
+                ];
             }
-            $reply[] = [
-                'uid' => $val->uid,
-                'username' => $userComment['username'],
-                'avatar' => $userComment['avatar'],
-                'reply_username' => $replyUsername,
-                'content' => $val->is_delete?'该评论已被删除':$val->content,
-                'created_at' => Carbon::parse($val->created_at)->diffForHumans(),
-                'is_delete' => $val->is_delete,
-            ];
+            Redis::SET($replyKey, json_encode($reply));
+            Redis::EXPIRE($replyKey, 300);
         }
         $user = $this->userInfo($postComment['uid']);
         return [
@@ -44,7 +53,7 @@ class CommentTransformer extends TransformerAbstract
             'avatar' => $user['avatar'],
             'content' => $postComment['is_delete']?'该评论已被删除':$postComment['content'],
             'created_at' => Carbon::parse($postComment['created_at'])->diffForHumans(),
-            'reply_count' => $postComment->reply->count(),
+            'reply_count' => $postComment['reply_count'],
             'reply' => $reply,
             'is_delete' => $postComment['is_delete'],
         ];

+ 15 - 24
app/Transformers/Post/DetailTransformer.php

@@ -12,13 +12,16 @@ use App\Models\PostCollect;
 use App\Models\PostComment;
 use App\Models\PostDislike;
 use App\Models\PostLike;
+use App\Traits\PostTrait;
 use App\Traits\UserTrait;
 use Carbon\Carbon;
+use Illuminate\Support\Facades\Redis;
 use League\Fractal\TransformerAbstract;
 
 class DetailTransformer extends TransformerAbstract
 {
     use UserTrait;
+    use PostTrait;
     public function __construct($uid, $invite_code)
     {
         $this->uid = $uid;
@@ -26,33 +29,21 @@ class DetailTransformer extends TransformerAbstract
     }
     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
-            ];
-        }
-
         $isLike = 0;
         $isDislike = 0;
         $isCollect = 0;
         $isFollow = 0;
         if($this->uid){
-            $isLike = PostLike::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0;
-            $isDislike = PostDislike::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0;
-            $isCollect = PostCollect::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0;
+            $isLike = Redis::SISMEMBER('post_like_'.$post['id'], $this->uid);
+            $isDislike = Redis::SISMEMBER('post_unlike_'.$post['id'], $this->uid);
+            $isCollect = Redis::SISMEMBER('post_collect_'.$post['id'], $this->uid);
             $followStatus = $this->getFollowStatus($this->uid, $post['uid']);
             if($followStatus){
                 $isFollow = $followStatus;
             }
         }
         $user = $this->userInfo($post['uid']);
+        $postInfo = $this->getPostInfo($post['id']);
         return [
             'id' => $post['id'],
             'type' => $post['type'],
@@ -60,20 +51,20 @@ class DetailTransformer extends TransformerAbstract
             'uid' => $post['uid'],
             'username' => $user['username'],
             'avatar' => $user['avatar'],
-            'topic' => $topic,
+            'topic' => $this->getTopic($post['topic_ids']),
             'topic_ids' => $post['topic_ids'],
             'title' => $post['title'],
             'content' => $post['content'],
             'location' => $post['location'],
             'img' => $post['img'],
-            'imgs' => $imgs,
+            'imgs' => $postInfo['imgs'],
             'video' => $post['video'],
-            'pv' => getNumber($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,
+            'pv' => getNumber($postInfo['pv']),
+            'praise_count' => $postInfo['praise_count'],
+            'collect_count' => $postInfo['collect_count'],
+            'comment_count' => $postInfo['comment_count'],
+            'available_bean' => $postInfo['available_bean'],
+            'will_collect_bean' => $postInfo['will_collect_bean'],
             'is_like' => $isLike,
             'is_dislike' => $isDislike,
             'is_collect' => $isCollect,

+ 6 - 2
app/Transformers/Post/ListTransformer.php

@@ -9,12 +9,15 @@ namespace  App\Transformers\Post;
 
 use App\Models\Post;
 use App\Models\PostLike;
+use App\Traits\PostTrait;
 use App\Traits\UserTrait;
+use Illuminate\Support\Facades\Redis;
 use League\Fractal\TransformerAbstract;
 
 class ListTransformer extends TransformerAbstract
 {
     use UserTrait;
+    use PostTrait;
     public function __construct($uid, $invite_code)
     {
         $this->uid = $uid;
@@ -24,9 +27,10 @@ class ListTransformer extends TransformerAbstract
     {
         $isLike = 0;
         if($this->uid){
-            $isLike = PostLike::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0;
+            $isLike = Redis::SISMEMBER('post_like_'.$post['id'], $this->uid);
         }
         $user = $this->userInfo($post['uid']);
+        $postInfo = $this->getPostInfo($post['id']);
         return [
             'id' => $post['id'],
             'type' => $post['type'],
@@ -36,7 +40,7 @@ class ListTransformer extends TransformerAbstract
             'title' => $post['title'],
             'content' => subtext($post['content'], 100),
             'img' => $post['img'],
-            'praise_count' => $post->data->praise_count,
+            'praise_count' => $postInfo['praise_count'],
             'is_like' => $isLike,
             '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']:'',

+ 4 - 1
app/Transformers/Post/MyTransformer.php

@@ -8,17 +8,20 @@
 namespace  App\Transformers\Post;
 
 use App\Models\Post;
+use App\Traits\PostTrait;
 use League\Fractal\TransformerAbstract;
 
 class MyTransformer extends TransformerAbstract
 {
+    use PostTrait;
     public function transform(Post $post)
     {
+        $postInfo = $this->getPostInfo($post['id']);
         return [
             'id' => $post['id'],
             'img' => $post['deleted_at']?config('customer.post_delete_image'):$post['img'],
             'type' => $post['type'],
-            'collect_bean' => $post->data->collect_bean > 0 ? $post->data->collect_bean : 0,
+            'collect_bean' => $postInfo['collect_bean'] > 0 ? $postInfo['collect_bean'] : 0,
         ];
     }
 }

+ 11 - 20
app/Transformers/Post/PostTransformer.php

@@ -12,6 +12,7 @@ use App\Models\PostCollect;
 use App\Models\PostComment;
 use App\Models\PostDislike;
 use App\Models\PostLike;
+use App\Traits\PostTrait;
 use App\Traits\UserTrait;
 use Carbon\Carbon;
 use League\Fractal\TransformerAbstract;
@@ -19,24 +20,14 @@ use League\Fractal\TransformerAbstract;
 class PostTransformer extends TransformerAbstract
 {
     use UserTrait;
+    use PostTrait;
     public function __construct()
     {
     }
     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
-            ];
-        }
         $user = $this->userInfo($post['uid']);
+        $postInfo = $this->getPostInfo($post['id']);
         return [
             'id' => $post['id'],
             'type' => $post['type'],
@@ -44,19 +35,19 @@ class PostTransformer extends TransformerAbstract
             'uid' => $post['uid'],
             'username' => $user['username'],
             'avatar' => $user['avatar'],
-            'topic' => $topic,
+            'topic' => $this->getTopic($post['topic_ids']),
             'title' => $post['title'],
             'content' => $post['content'],
             'location' => $post['location'],
             'img' => $post['img'],
-            'imgs' => $imgs,
+            'imgs' => $postInfo['imgs'],
             'video' => $post['video'],
-            'pv' => getNumber($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,
+            'pv' => getNumber($postInfo['pv']),
+            'praise_count' => $postInfo['praise_count'],
+            'collect_count' => $postInfo['collect_count'],
+            'comment_count' => $postInfo['comment_count'],
+            'available_bean' => $postInfo['available_bean'],
+            'will_collect_bean' => $postInfo['will_collect_bean'],
         ];
     }
 }

+ 17 - 36
app/Transformers/Post/SuggestTransformer.php

@@ -9,17 +9,16 @@
 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\PostTrait;
 use App\Traits\UserTrait;
 use Carbon\Carbon;
+use Illuminate\Support\Facades\Redis;
 use League\Fractal\TransformerAbstract;
 
 class SuggestTransformer extends TransformerAbstract
 {
     use UserTrait;
+    use PostTrait;
     public function __construct($uid, $invite_code)
     {
         $this->uid = $uid;
@@ -27,36 +26,18 @@ class SuggestTransformer extends TransformerAbstract
     }
     public function transform(Post $post)
     {
-        $imgs = [];
-        foreach($post->imgs as $img){
-            $imgs[] = $img['img'];
-        }
-        $comment = [];
-        $comments = PostComment::where('post_id', $post['id'])->where('parent_id', 0)->orderBy('is_delete', 'asc')->orderBy('id', 'desc')->limit(2)->get();
-        foreach($comments as $item){
-            $userComment = $this->userInfo($item->uid);
-            $comment[] = [
-                'id' => $item->id,
-                'username' => $userComment['username'],
-                'content' => $item->is_delete?'该评论已被删除':$item->content,
-            ];
-        }
-        $topic = [];
-        foreach($post->topic() as $key => $val){
-            $topic[] = [
-                'id' => $key,
-                'name' => $val
-            ];
-        }
+
+
         $isLike = 0;
         $isDislike = 0;
         $isCollect = 0;
         if($this->uid){
-            $isLike = PostLike::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0;
-            $isDislike = PostDislike::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0;
-            $isCollect = PostCollect::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0;
+            $isLike = Redis::SISMEMBER('post_like_'.$post['id'], $this->uid);
+            $isDislike = Redis::SISMEMBER('post_unlike_'.$post['id'], $this->uid);
+            $isCollect = Redis::SISMEMBER('post_collect_'.$post['id'], $this->uid);
         }
         $user = $this->userInfo($post['uid']);
+        $postInfo = $this->getPostInfo($post['id']);
         return [
             'show_type' => 'post',
             'id' => $post['id'],
@@ -65,22 +46,22 @@ class SuggestTransformer extends TransformerAbstract
             'uid' => $post['uid'],
             'username' => $user['username'],
             'avatar' => $user['avatar'],
-            'topic' => $topic,
+            'topic' => $this->getTopic($post['topic_ids']),
             'title' => $post['title'],
             'content' => $post['content'],
             'location' => $post['location'],
             'img' => $post['img'],
-            'imgs' => $imgs,
+            'imgs' => $postInfo['imgs'],
             'video' => $post['video'],
-            'pv' => getNumber($post->data->pv),
-            'praise_count' => $post->data->praise_count,
-            'comment_count' => $post->data->comment_count,
-            'collect_count' => $post->data->collect_count,
-            'will_collect_bean' => $post->data->will_collect_bean + 3 * $post->data->pv,
+            'pv' => getNumber($postInfo['pv']),
+            'praise_count' => $postInfo['praise_count'],
+            'comment_count' => $postInfo['comment_count'],
+            'collect_count' => $postInfo['collect_count'],
+            'will_collect_bean' => $postInfo['will_collect_bean'],
             'is_like' => $isLike,
             'is_dislike' => $isDislike,
             'is_collect' => $isCollect,
-            'comment' => $comment,
+            'comment' => $this->getNewComment($post['id']),
             'is_follow' => $this->getFollowStatus($this->uid, $post['uid']),
             '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']:'',

+ 12 - 14
app/Transformers/Post/VideoTransformer.php

@@ -10,12 +10,15 @@ namespace  App\Transformers\Post;
 use App\Models\Post;
 use App\Models\PostCollect;
 use App\Models\PostLike;
+use App\Traits\PostTrait;
 use App\Traits\UserTrait;
+use Illuminate\Support\Facades\Redis;
 use League\Fractal\TransformerAbstract;
 
 class VideoTransformer extends TransformerAbstract
 {
     use UserTrait;
+    use PostTrait;
     public function __construct($uid, $invite_code)
     {
         $this->uid = $uid;
@@ -27,21 +30,16 @@ class VideoTransformer extends TransformerAbstract
         $isCollect = 0;
         $isFollow = 0;
         if($this->uid){
-            $isLike = PostLike::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0;
-            $isCollect = PostCollect::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0;
+            $isLike = Redis::SISMEMBER('post_like_'.$post['id'], $this->uid);
+            $isCollect = Redis::SISMEMBER('post_collect_'.$post['id'], $this->uid);
             $followStatus = $this->getFollowStatus($this->uid, $post['uid']);
             if($followStatus){
                 $isFollow = $followStatus;
             }
         }
-        $topic = [];
-        foreach($post->topic() as $key => $val){
-            $topic[] = [
-                'id' => $key,
-                'name' => $val
-            ];
-        }
+
         $user = $this->userInfo($post['uid']);
+        $postInfo = $this->getPostInfo($post['id']);
         return [
             'id' => $post['id'],
             'type' => $post['type'],
@@ -52,11 +50,11 @@ class VideoTransformer extends TransformerAbstract
             'content' => $post['content'],
             'img' => $post['img'],
             'video' => $post['video'],
-            'topic' => $topic,
-            'praise_count' => $post->data->praise_count,
-            'collect_count' => $post->data->collect_count,
-            'comment_count' => $post->data->comment_count,
-            'will_collect_bean' => $post->data->will_collect_bean + 3 * $post->data->pv,
+            'topic' => $this->getTopic($post['topic_ids']),
+            'praise_count' => $postInfo['praise_count'],
+            'collect_count' => $postInfo['collect_count'],
+            'comment_count' => $postInfo['comment_count'],
+            'will_collect_bean' => $postInfo['will_collect_bean'],
             'is_like' => $isLike,
             'is_collect' => $isCollect,
             'is_follow' => $isFollow,

+ 1 - 1
routes/api.php

@@ -101,10 +101,10 @@ $api->version('v1', [
         //优秀居民信息获取
         $api->get('excellentResidents', 'BeanDetailController@excellentResidents');
         //排行榜
-        $api->get('rankingList', 'BeanDetailController@rankingList');
         //后院首页
         $api->get('starHome', 'BeanDetailController@starHome');
     });
+    $api->get('rankingList', 'BeanDetailController@rankingList');
 
     //分享/邀请首页
     $api->get('starDetail', 'BeanDetailController@starDetail');