From 2f5846225e32cde5d5d5e721055bb88937c3bb93 Mon Sep 17 00:00:00 2001 From: augushong Date: Fri, 1 May 2026 16:17:00 +0800 Subject: [PATCH] =?UTF-8?q?fix(api):=20=E4=BF=AE=E5=A4=8D=E5=88=9B?= =?UTF-8?q?=E5=BB=BA=E6=96=87=E7=AB=A0500=E9=94=99=E8=AF=AF=E5=92=8C?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=96=87=E7=AB=A0=E6=95=B0=E6=8D=AE=E8=A6=86?= =?UTF-8?q?=E7=9B=96=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/controller/Articles.php | 21 --------------------- app/model/Post.php | 20 ++++++++++++++++---- 2 files changed, 16 insertions(+), 25 deletions(-) diff --git a/app/api/controller/Articles.php b/app/api/controller/Articles.php index 3baefd7..fb7e226 100644 --- a/app/api/controller/Articles.php +++ b/app/api/controller/Articles.php @@ -271,27 +271,6 @@ class Articles extends BaseController $post_data['content_html'] = $converter->convert($post_data['content'])->getContent(); } - // 提供字段默认值,避免数据库严格模式报错 - $default_fields = [ - 'jump_to_btn_title' => '', - 'jump_to_url' => '', - 'jump_to_url_status' => 0, - 'poster' => '', - 'desc' => '', - 'author_name' => '', - 'hits' => 0, - 'is_top' => 0, - 'sort' => 0, - 'files' => '', - 'pictures' => '', - 'tpl_name' => '', - ]; - foreach ($default_fields as $field => $default) { - if (!isset($post_data[$field])) { - $post_data[$field] = $default; - } - } - $model_post->save($post_data); return json_message([], 0, '更新成功'); diff --git a/app/model/Post.php b/app/model/Post.php index 725d560..9ef5e6c 100644 --- a/app/model/Post.php +++ b/app/model/Post.php @@ -269,7 +269,13 @@ class Post extends Base public function getHitsTitleAttr() { - $cache_key = static::CACHE_KEY_HITS . $this->getAttr('id'); + // 新建对象时直接返回 0 + $id = $this->getData('id'); + if (empty($id)) { + return 0; + } + + $cache_key = static::CACHE_KEY_HITS . $id; $value = Cache::get($cache_key); @@ -277,7 +283,8 @@ class Post extends Base return $value; } - $value = $this->getData('hits'); + // 安全获取:hits 可能尚未存在于 data 中 + $value = array_key_exists('hits', $this->data) ? $this->data['hits'] : 0; Cache::set($cache_key, $value, 600); @@ -286,9 +293,14 @@ class Post extends Base public function setHitsAttr($value) { - $cache_key = static::CACHE_KEY_HITS . $this->getAttr('id'); + // 新建对象(无 id)时,直接返回值,跳过缓存操作 + $id = $this->getData('id'); + if (empty($id)) { + return $value; + } + + $cache_key = static::CACHE_KEY_HITS . $id; - $this->getAttr('hits_title'); Cache::inc($cache_key); return $value;