Explorar o código

新增:商品分类模板(分类+商品)

萤火科技 %!s(int64=4) %!d(string=hai) anos
pai
achega
d944816748

+ 35 - 5
app/api/controller/Goods.php

@@ -23,10 +23,10 @@ class Goods extends Controller
 {
     /**
      * 商品列表
-     * @return array
+     * @return \think\response\Json
      * @throws \think\db\exception\DbException
      */
-    public function list()
+    public function list(): \think\response\Json
     {
         // 获取列表数据
         $model = new GoodsModel;
@@ -37,13 +37,13 @@ class Goods extends Controller
     /**
      * 获取商品详情
      * @param int $goodsId
-     * @return array|\think\response\Json
-     * @throws \app\common\exception\BaseException
+     * @return \think\response\Json
+     * @throws \cores\exception\BaseException
      * @throws \think\db\exception\DataNotFoundException
      * @throws \think\db\exception\DbException
      * @throws \think\db\exception\ModelNotFoundException
      */
-    public function detail(int $goodsId)
+    public function detail(int $goodsId): \think\response\Json
     {
         // 商品详情
         $model = new GoodsModel;
@@ -51,4 +51,34 @@ class Goods extends Controller
         return $this->renderSuccess(['detail' => $goodsInfo]);
     }
 
+    /**
+     * 获取商品详情(基础信息)
+     * @param int $goodsId 商品ID
+     * @param bool $verifyStatus 是否验证商品状态(上架)
+     * @return Json
+     * @throws BaseException
+     */
+    public function basic(int $goodsId, bool $verifyStatus = true): Json
+    {
+        // 获取商品详情
+        $model = new GoodsModel;
+        $detail = $model->getBasic($goodsId, $verifyStatus);
+        return $this->renderSuccess(compact('detail'));
+    }
+
+    /**
+     * 获取商品规格数据
+     * @param int $goodsId 商品ID
+     * @return \think\response\Json
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public function specData(int $goodsId): Json
+    {
+        // 获取商品详情
+        $model = new GoodsModel;
+        $specData = $model->getSpecData($goodsId);
+        return $this->renderSuccess(compact('specData'));
+    }
 }

+ 64 - 11
app/api/model/Goods.php

@@ -14,6 +14,7 @@ namespace app\api\model;
 
 use app\api\service\Goods as GoodsService;
 use app\api\service\user\Grade as UserGradeService;
+use app\api\model\GoodsSku as GoodsSkuModel;
 use app\api\model\GoodsSpecRel as GoodsSpecRelModel;
 use app\common\model\Goods as GoodsModel;
 use app\common\enum\goods\Status as GoodsStatusEnum;
@@ -91,28 +92,80 @@ class Goods extends GoodsModel
 
     /**
      * 获取商品详情 (详细数据用于页面展示)
-     * @param int $goodsId 商品id
+     * @param int $goodsId 商品ID
+     * @param bool $verifyStatus 是否验证商品状态(上架)
      * @return mixed
      * @throws BaseException
      * @throws \think\db\exception\DataNotFoundException
      * @throws \think\db\exception\DbException
      * @throws \think\db\exception\ModelNotFoundException
      */
-    public function getDetails(int $goodsId)
+    public function getDetails(int $goodsId, bool $verifyStatus = true)
     {
-        // 关联查询
+        // 关联查询(商品图片、sku列表)
         $with = ['images.file', 'skuList.image', 'video', 'videoCover'];
         // 获取商品记录
-        $goodsInfo = static::detail($goodsId, $with);
-        // 判断商品的状态
-        if (empty($goodsInfo) || $goodsInfo['is_delete'] || $goodsInfo['status'] != GoodsStatusEnum::ON_SALE) {
-            throwError('很抱歉,商品信息不存在或已下架');
-        }
-        // 设置商品展示的数据
-        $goodsInfo = $this->setGoodsDataFromApi($goodsInfo);
+        $goodsInfo = $this->getGoodsMain($goodsId, $with, $verifyStatus);
         // 商品规格列表
         $goodsInfo['specList'] = GoodsSpecRelModel::getSpecList($goodsInfo['goods_id']);
-        return $goodsInfo;
+        return $goodsInfo->hidden(array_merge($this->hidden, ['images']));
+    }
+
+    /**
+     * 获取商品详情 (仅包含主商品信息和商品图片)
+     * @param int $goodsId 商品ID
+     * @param bool $verifyStatus 是否验证商品状态(上架)
+     * @return mixed
+     * @throws BaseException
+     */
+    public function getBasic(int $goodsId, bool $verifyStatus = true)
+    {
+        // 关联查询(商品图片)
+        $with = ['images.file'];
+        // 获取商品记录
+        return $this->getGoodsMain($goodsId, $with, $verifyStatus);
+    }
+
+    /**
+     * 获取商品规格数据
+     * @param int $goodsId
+     * @return array
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public function getSpecData(int $goodsId): array
+    {
+        $data = [];
+        // 商品SKU列表
+        $data['skuList'] = GoodsSkuModel::getSkuList($goodsId);
+        // 商品规格列表
+        $data['specList'] = GoodsSpecRelModel::getSpecList($goodsId);
+        return $data;
+    }
+
+    /**
+     * 获取商品主体信息
+     * @param int $goodsId 商品ID
+     * @param array $with 关联查询
+     * @param bool $verifyStatus 是否验证商品状态(上架)
+     * @return mixed
+     * @throws BaseException
+     */
+    private function getGoodsMain(int $goodsId, array $with = [], bool $verifyStatus = true)
+    {
+        // 获取商品记录
+        $goodsInfo = static::detail($goodsId, $with);
+        // 判断商品是否存在
+        if (empty($goodsInfo) || $goodsInfo['is_delete']) {
+            throwError('很抱歉,商品信息不存在');
+        }
+        // 判断商品状态(上架)
+        if ($verifyStatus && $goodsInfo['status'] == GoodsStatusEnum::OFF_SALE) {
+            throwError('很抱歉,当前商品已下架');
+        }
+        // 整理商品数据并返回
+        return $this->setGoodsDataFromApi($goodsInfo);
     }
 
     /**

+ 3 - 4
app/api/model/GoodsSku.php

@@ -8,7 +8,7 @@
 // +----------------------------------------------------------------------
 // | Author: 萤火科技 <admin@yiovo.com>
 // +----------------------------------------------------------------------
-declare (strict_types = 1);
+declare (strict_types=1);
 
 namespace app\api\model;
 
@@ -22,10 +22,10 @@ use app\common\model\GoodsSku as GoodsSkuModel;
 class GoodsSku extends GoodsSkuModel
 {
     /**
-     * 规格图片
+     * 关联模型:规格图片
      * @return \think\model\relation\HasOne
      */
-    public function image()
+    public function image(): \think\model\relation\HasOne
     {
         return parent::image()->bind(['image_url' => 'preview_url']);
     }
@@ -39,5 +39,4 @@ class GoodsSku extends GoodsSkuModel
         'create_time',
         'update_time'
     ];
-
 }

+ 10 - 4
app/common/enum/store/page/category/Style.php

@@ -8,8 +8,7 @@
 // +----------------------------------------------------------------------
 // | Author: 萤火科技 <admin@yiovo.com>
 // +----------------------------------------------------------------------
-declare (strict_types = 1);
-
+declare (strict_types=1);
 
 namespace app\common\enum\store\page\category;
 
@@ -28,14 +27,17 @@ class Style extends EnumBasics
     // 一级分类[小图]
     const ONE_LEVEL_SMALL = 11;
 
-    // 二级分类
+    // 一级分类+二级分类
     const TWO_LEVEL = 20;
 
+    // 分类+商品
+    const COMMODITY = 30;
+
     /**
      * 获取类型值
      * @return array
      */
-    public static function data()
+    public static function data(): array
     {
         return [
             self::ONE_LEVEL_BIG => [
@@ -49,6 +51,10 @@ class Style extends EnumBasics
             self::TWO_LEVEL => [
                 'name' => '二级分类',
                 'value' => self::TWO_LEVEL
+            ],
+            self::COMMODITY => [
+                'name' => '分类+商品',
+                'value' => self::COMMODITY
             ]
         ];
     }

+ 20 - 7
app/common/model/GoodsSku.php

@@ -8,11 +8,12 @@
 // +----------------------------------------------------------------------
 // | Author: 萤火科技 <admin@yiovo.com>
 // +----------------------------------------------------------------------
-declare (strict_types = 1);
+declare (strict_types=1);
 
 namespace app\common\model;
 
 use cores\BaseModel;
+
 use app\common\library\helper;
 
 /**
@@ -29,10 +30,10 @@ class GoodsSku extends BaseModel
     protected $pk = 'id';
 
     /**
-     * 规格图片
+     * 关联模型:规格图片
      * @return \think\model\relation\HasOne
      */
-    public function image()
+    public function image(): \think\model\relation\HasOne
     {
         return $this->hasOne('UploadFile', 'file_id', 'image_id');
     }
@@ -40,7 +41,7 @@ class GoodsSku extends BaseModel
     /**
      * 获取器:规格值ID集
      * @param $value
-     * @return array
+     * @return array|mixed
      */
     public function getSpecValueIdsAttr($value)
     {
@@ -50,7 +51,7 @@ class GoodsSku extends BaseModel
     /**
      * 获取器:规格属性
      * @param $value
-     * @return array
+     * @return array|mixed
      */
     public function getGoodsPropsAttr($value)
     {
@@ -62,7 +63,7 @@ class GoodsSku extends BaseModel
      * @param $value
      * @return string
      */
-    public function setSpecValueIdsAttr($value)
+    public function setSpecValueIdsAttr($value): string
     {
         return helper::jsonEncode($value);
     }
@@ -72,7 +73,7 @@ class GoodsSku extends BaseModel
      * @param $value
      * @return string
      */
-    public function setGoodsPropsAttr($value)
+    public function setGoodsPropsAttr($value): string
     {
         return helper::jsonEncode($value);
     }
@@ -88,4 +89,16 @@ class GoodsSku extends BaseModel
         return static::get(['goods_id' => $goodsId, 'goods_sku_id' => $goodsSkuId]);
     }
 
+    /**
+     * 获取商品SKU列表
+     * @param int $goodsId 商品ID
+     * @return \think\Collection
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public static function getSkuList(int $goodsId): \think\Collection
+    {
+        return (new static)->where('goods_id', '=', $goodsId)->select();
+    }
 }

+ 4 - 2
app/common/model/store/Setting.php

@@ -316,8 +316,10 @@ class Setting extends BaseModel
                 'key' => SettingEnum::PAGE_CATEGORY_TEMPLATE,
                 'describe' => '分类页模板设置',
                 'values' => [
-                    'style' => PageCategoryStyleEnum::TWO_LEVEL,    // 分类页样式
-                    'shareTitle' => ''                              // 分享标题
+                    'style' => PageCategoryStyleEnum::COMMODITY,    // 分类页样式
+                    'shareTitle' => '全部分类',                      // 分享标题
+                    'showAddCart' => true,                          // 是否显示购物车按钮
+                    'cartStyle' => 1,                               // 购物车按钮样式  1-3
                 ]
             ]
         ];