wzq 5 rokov pred
rodič
commit
3a9cbda713

+ 58 - 0
app/Console/Commands/Tem/DelPostNewComment.php

@@ -0,0 +1,58 @@
+<?php
+namespace App\Console\Commands\Tem;
+
+
+use App\Models\Post;
+use Illuminate\Console\Command;
+use Illuminate\Support\Facades\Redis;
+
+class DelPostNewComment extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'post:del_post_new_comment';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = '删除内容最火评论缓存';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct(Post $post)
+    {
+        parent::__construct();
+        $this->post = $post;
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return mixed
+     */
+    public function handle()
+    {
+        $this->line("开始删除内容最火评论缓存");
+
+        $count = $this->post->withTrashed()->count();
+        $bar = $this->output->createProgressBar($count);
+        $this->post->withTrashed()->chunk(100, function ($posts) use ($bar){
+            foreach($posts as $post){
+                Redis::DEL('post_new_comment_'.$post->id);
+                $bar->advance();
+            }
+            usleep(50000);
+        });
+        $bar->finish();
+        $this->line("\n删除内容最火评论缓存结束");
+
+    }
+}

+ 2 - 0
app/Console/Kernel.php

@@ -10,6 +10,7 @@ use App\Console\Commands\ContentFeedDelete;
 use App\Console\Commands\DelPostNewReply;
 use App\Console\Commands\ExcellentResidents;
 use App\Console\Commands\RankingList;
+use App\Console\Commands\Tem\DelPostNewComment;
 use Illuminate\Console\Scheduling\Schedule;
 use Laravel\Lumen\Console\Kernel as ConsoleKernel;
 
@@ -28,6 +29,7 @@ class Kernel extends ConsoleKernel
         ContentFeedDelete::class,
         ExcellentResidents::class,
         DelPostNewReply::class,
+        DelPostNewComment::class,
         RankingList::class
     ];
 

+ 1 - 1
app/Repositories/FeedRepositories.php

@@ -225,7 +225,7 @@ class FeedRepositories
             'comment_count' => $postInfo['comment_count'],
             'available_bean' => $postInfo['available_bean'],
             'will_collect_bean' => $postInfo['will_collect_bean'],
-            'post_comment' => $this->getNewComment($postInfo['id']),
+            'post_comment' => $this->getNewComment($postInfo['id'], $uid),
             '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),

+ 19 - 3
app/Traits/PostTrait.php

@@ -10,6 +10,7 @@ namespace App\Traits;
 
 use App\Models\Post;
 use App\Models\PostComment;
+use Illuminate\Support\Facades\Log;
 use Illuminate\Support\Facades\Redis;
 
 trait PostTrait
@@ -81,26 +82,41 @@ trait PostTrait
     }
 
     //获取内容最新评论
-    public function getNewComment($id)
+    public function getNewComment($id, $uid = 0)
     {
         $comment = [];
+        $key = 'comment_like_'.$id;
         $commentKey = 'post_new_comment_'.$id;
         $commentData = Redis::GET($commentKey);
         if($commentData){
             $comment = json_decode($commentData);
+            foreach($comment as &$item){
+                $isLike = 0;
+                if($uid){
+                    $isLike = Redis::ZSCORE($key, $uid.'_'.$item->id) ? 1 : 0;
+                }
+                $item->is_like = $isLike;
+            }
         }else{
-            $comments = PostComment::where('post_id', $id)->where('parent_id', 0)->orderBy('is_delete', 'asc')->orderBy('id', 'desc')->limit(2)->get();
+            $comments = PostComment::where('post_id', $id)->where('parent_id', 0)->orderBy('like_count', 'desc')->orderBy('is_delete', 'asc')->orderBy('id', 'desc')->limit(2)->get();
             foreach($comments as $item){
                 $userComment = $this->userInfo($item->uid);
+                $isLike = 0;
+                if($uid){
+                    $isLike = Redis::ZSCORE($key, $uid.'_'.$item->id) ? 1 : 0;
+                }
+                $likeCount = Redis::ZCOUNT($key, $item->id, $item->id);
                 $comment[] = [
                     'id' => $item->id,
                     'username' => $userComment['username'],
                     'content' => $item->is_delete?'该评论已被删除':$item->content,
                     'is_delete' => $item->is_delete,
+                    'is_like' => $isLike,
+                    'like_count' => $likeCount,
                 ];
             }
             Redis::SET($commentKey, json_encode($comment));
-            Redis::EXPIRE($commentKey, 604800);
+            Redis::EXPIRE($commentKey, 3);
         }
         return $comment;
     }

+ 1 - 1
app/Transformers/Post/SuggestTransformer.php

@@ -61,7 +61,7 @@ class SuggestTransformer extends TransformerAbstract
             'is_like' => $isLike,
             'is_dislike' => $isDislike,
             'is_collect' => $isCollect,
-            'comment' => $this->getNewComment($post['id']),
+            'comment' => $this->getNewComment($post['id'], $this->uid),
             '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']:'',