Просмотр исходного кода

修复:后台编辑文章时不显示封面图

萤火科技 5 лет назад
Родитель
Сommit
4605641629

+ 3 - 5
app/api/controller/Article.php

@@ -36,15 +36,13 @@ class Article extends Controller
 
     /**
      * 文章详情
-     * @param $articleId
-     * @return array
+     * @param int $articleId
+     * @return array|\think\response\Json
      * @throws \app\common\exception\BaseException
-     * @throws \think\Exception
      */
     public function detail(int $articleId)
     {
-        $detail = ArticleModel::detail($articleId);
+        $detail = ArticleModel::getDetail($articleId);
         return $this->renderSuccess(compact('detail'));
     }
-
 }

+ 22 - 10
app/api/model/Article.php

@@ -8,7 +8,7 @@
 // +----------------------------------------------------------------------
 // | Author: 萤火科技 <admin@yiovo.com>
 // +----------------------------------------------------------------------
-declare (strict_types = 1);
+declare (strict_types=1);
 
 namespace app\api\model;
 
@@ -45,19 +45,31 @@ class Article extends ArticleModel
 
     /**
      * 获取文章详情并累计阅读次数
-     * @param int $articleId
-     * @return ArticleModel|null
+     * @param int $articleId 文章ID
+     * @return static|null
      * @throws BaseException
-     * @throws \think\Exception
      */
-    public static function detail(int $articleId)
+    public static function getDetail(int $articleId)
     {
-        if (!$model = parent::detail($articleId)) {
-            throwError('文章不存在');
+        // 获取文章详情
+        $detail = parent::detail($articleId, ['image']);
+        if (empty($detail) || $detail['is_delete']) {
+            throwError('很抱歉,当前文章不存在');
         }
-        // 累积实际阅读数
-        $model->setInc($articleId, 'actual_views', 1);
-        return $model;
+        // 累积文章实际阅读数
+        static::setIncActualViews($articleId);
+        return $detail;
+    }
+
+    /**
+     * 累积文章实际阅读数
+     * @param int $articleId 文章ID
+     * @param int $num 递增的数量
+     * @return mixed
+     */
+    private static function setIncActualViews(int $articleId, int $num = 1)
+    {
+        return (new static)->setInc($articleId, 'actual_views', $num);
     }
 
     /**

+ 4 - 4
app/common/model/Article.php

@@ -8,7 +8,7 @@
 // +----------------------------------------------------------------------
 // | Author: 萤火科技 <admin@yiovo.com>
 // +----------------------------------------------------------------------
-declare (strict_types = 1);
+declare (strict_types=1);
 
 namespace app\common\model;
 
@@ -72,11 +72,11 @@ class Article extends BaseModel
     /**
      * 文章详情
      * @param int $articleId
+     * @param array $with
      * @return array|null|static
      */
-    public static function detail(int $articleId)
+    public static function detail(int $articleId, array $with = [])
     {
-        return self::get($articleId, ['image', 'category']);
+        return self::get($articleId, $with);
     }
-
 }

+ 2 - 0
app/store/controller/content/Article.php

@@ -42,6 +42,8 @@ class Article extends Controller
     public function detail(int $articleId)
     {
         $detail = ArticleModel::detail($articleId);
+        // 获取image (这里不能用with因为编辑页需要image对象)
+        !empty($detail) && $detail['image'];
         return $this->renderSuccess(compact('detail'));
     }