From cc83df7fb9143a80737afe6576426bb58b4f13a0 Mon Sep 17 00:00:00 2001 From: augushong Date: Sun, 3 Jan 2021 22:02:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E8=AF=84=E8=AE=BA=E7=BB=9F?= =?UTF-8?q?=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++++ app/admin/controller/PostComment.php | 3 +++ app/index/controller/PostComment.php | 7 ++++++- app/model/Post.php | 10 ++++++++++ app/model/PostComment.php | 20 ++++++++++++++++++++ 5 files changed, 43 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2db73a3..a8e6ac8 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,10 @@ admin.域名->访问后台应用 api.域名->访问接口应用 ``` +#### 用户体系 + +本站实现了用户中心的授权登陆,如果需要登陆的话,需要额外搭建一个用户中心站点,具体请看:https://gitee.com/ulthon/user_hub + #### 重置密码 diff --git a/app/admin/controller/PostComment.php b/app/admin/controller/PostComment.php index 6db4901..ba7c193 100644 --- a/app/admin/controller/PostComment.php +++ b/app/admin/controller/PostComment.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace app\admin\controller; use app\model\PostComment as ModelPostComment; +use think\facade\Cache; use think\facade\View; use think\Request; @@ -96,6 +97,8 @@ class PostComment extends Common ModelPostComment::destroy($id); + ModelPostComment::clearCountCache(); + return json_message(); } } diff --git a/app/index/controller/PostComment.php b/app/index/controller/PostComment.php index 94a9138..bbd17a4 100644 --- a/app/index/controller/PostComment.php +++ b/app/index/controller/PostComment.php @@ -58,12 +58,15 @@ class PostComment extends Common } $post_data['type'] = 3; - + $post_data['user_uid'] = $user_uid; ModelPostComment::create($post_data); + ModelPostComment::clearCountCache(); + + return json_message(); } @@ -113,6 +116,8 @@ class PostComment extends Common ModelPostComment::destroy($id); + ModelPostComment::clearCountCache(); + return json_message(); } } diff --git a/app/model/Post.php b/app/model/Post.php index efb3e5f..ab9a2d2 100644 --- a/app/model/Post.php +++ b/app/model/Post.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace app\model; +use think\facade\Cache; use think\Model; use think\model\concern\SoftDelete; use think\Paginator; @@ -39,6 +40,15 @@ class Post extends Model return $this->hasMany(PostComment::class,'post_id'); } + + public function getCommentCountAttr() + { + + + + return PostComment::getPostCommentsCount($this->getData('id')); + } + public function setPublishTimeAttr($value) { return strtotime($value); diff --git a/app/model/PostComment.php b/app/model/PostComment.php index 75ab487..463ceb6 100644 --- a/app/model/PostComment.php +++ b/app/model/PostComment.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace app\model; use app\index\controller\Common; +use think\facade\Cache; use think\Model; /** @@ -32,4 +33,23 @@ class PostComment extends Model return $read_url; } + + public static function getPostCommentsCount($post_id) + { + $cache_key = 'post_comment_count_' . $post_id; + $count = Cache::get($cache_key); + + if (is_null($count)) { + $count = PostComment::where('post_id', $post_id)->count(); + + Cache::tag('comment')->set($cache_key, $count); + } + + return $count; + } + + public static function clearCountCache() + { + Cache::tag('comment')->clear(); + } }