Sfoglia il codice sorgente

新增:商品信息关联主图视频

Yahoo 4 anni fa
parent
commit
53aa5c26b6

+ 1 - 1
app/api/model/Goods.php

@@ -87,7 +87,7 @@ class Goods extends GoodsModel
     public function getDetails(int $goodsId)
     {
         // 关联查询
-        $with = ['images' => ['file'], 'skuList' => ['image']];
+        $with = ['images.file', 'skuList.image', 'video', 'videoCover'];
         // 获取商品记录
         $goodsInfo = static::detail($goodsId, $with);
         // 判断商品的状态

+ 21 - 35
app/common/model/Goods.php

@@ -12,12 +12,13 @@ declare (strict_types=1);
 
 namespace app\common\model;
 
-use cores\BaseModel;
 use think\Paginator;
 use think\model\Collection;
-use app\common\library\helper;
+use think\model\relation\HasOne;
+use cores\BaseModel;
 use app\store\model\GoodsCategoryRel as GoodsCategoryRelModel;
 use app\common\enum\goods\Status as GoodsStatusEnum;
+use app\common\library\helper;
 
 /**
  * 商品模型
@@ -35,6 +36,24 @@ class Goods extends BaseModel
     // 追加字段
     protected $append = ['goods_sales'];
 
+    /**
+     * 关联模型:主图视频文件
+     * @return HasOne
+     */
+    public function video(): HasOne
+    {
+        return $this->hasOne('UploadFile', 'file_id', 'video_id');
+    }
+
+    /**
+     * 关联模型:主图视频封面图片文件
+     * @return HasOne
+     */
+    public function videoCover(): HasOne
+    {
+        return $this->hasOne('UploadFile', 'file_id', 'video_cover_id');
+    }
+
     /**
      * 计算显示销量 (初始销量 + 实际销量)
      * @param $value
@@ -328,37 +347,4 @@ class Goods extends BaseModel
     {
         return static::get($goodsId, $with);
     }
-
-//    /**
-//     * 指定的商品规格信息
-//     * @param static $goods 商品详情
-//     * @param string $goodsSkuId
-//     * @return array|bool
-//     */
-//    public static function getGoodsSku(self $goods, string $goodsSkuId)
-//    {
-//        // 获取指定的sku
-//        $goodsSku = [];
-//        foreach ($goods['skuList'] as $item) {
-//            if ($item['goods_sku_id'] == $goodsSkuId) {
-//                $goodsSku = $item;
-//                break;
-//            }
-//        }
-//        if (empty($goodsSku)) {
-//            return false;
-//        }
-//        // 多规格文字内容
-//        $goodsSku['goods_attr'] = '';
-//        if ($goods['spec_type'] == 20) {
-//            $specRelData = helper::arrayColumn2Key($goods['spec_rel'], 'spec_value_id');
-//            $attrs = explode('_', $goodsSku['goods_sku_id']);
-//            foreach ($attrs as $specValueId) {
-//                $goodsSku['goods_attr'] .= $specRelData[$specValueId]['spec']['spec_name'] . ':'
-//                    . $specRelData[$specValueId]['spec_value'] . '; ';
-//            }
-//        }
-//        return $goodsSku;
-//    }
-
 }

+ 4 - 1
app/store/controller/Goods.php

@@ -57,6 +57,7 @@ class Goods extends Controller
      * @throws \think\db\exception\DataNotFoundException
      * @throws \think\db\exception\DbException
      * @throws \think\db\exception\ModelNotFoundException
+     * @throws \cores\exception\BaseException
      */
     public function detail(int $goodsId): Json
     {
@@ -72,6 +73,7 @@ class Goods extends Controller
      * @throws \think\db\exception\DataNotFoundException
      * @throws \think\db\exception\DbException
      * @throws \think\db\exception\ModelNotFoundException
+     * @throws \cores\exception\BaseException
      */
     public function add(): Json
     {
@@ -89,6 +91,7 @@ class Goods extends Controller
      * @throws \think\db\exception\DataNotFoundException
      * @throws \think\db\exception\DbException
      * @throws \think\db\exception\ModelNotFoundException
+     * @throws \cores\exception\BaseException
      */
     public function edit(int $goodsId): Json
     {
@@ -119,7 +122,7 @@ class Goods extends Controller
     /**
      * 删除商品
      * @param array $goodsIds
-     * @return array
+     * @return Json
      */
     public function delete(array $goodsIds): Json
     {

+ 30 - 12
app/store/model/Goods.php

@@ -43,32 +43,49 @@ class Goods extends GoodsModel
      */
     public function getDetail(int $goodsId)
     {
-        // 关联查询
-        $with = ['images' => ['file'], 'skuList' => ['image']];
-        // 获取商品记录
-        $goodsInfo = static::detail($goodsId, $with);
-        empty($goodsInfo) && throwError('很抱歉,商品信息不存');
-        // 整理商品数据并返回
-        $goodsInfo = parent::setGoodsData($goodsInfo);
+        // 获取商品基础信息
+        $goodsInfo = $this->getBasic($goodsId);
         // 分类ID集
         $goodsInfo['categoryIds'] = GoodsCategoryRelModel::getCategoryIds($goodsInfo['goods_id']);
-        // 商品规格列表
-        $goodsInfo['specList'] = GoodsSpecRelModel::getSpecList($goodsInfo['goods_id']);
+        // 商品多规格属性列表
+        if ($goodsInfo['spec_type'] == SpecTypeEnum::MULTI) {
+            $goodsInfo['specList'] = GoodsSpecRelModel::getSpecList($goodsInfo['goods_id']);
+        }
         // 服务与承诺
         $goodsInfo['serviceIds'] = GoodsServiceRelModel::getServiceIds($goodsInfo['goods_id']);
-        // 商品基本信息
+        // 商品规格是否锁定(锁定状态下不允许编辑规格)
+        $goodsInfo['isSpecLocked'] = GoodsService::checkSpecLocked($goodsId);
+        // 返回商品详细信息
         return $goodsInfo;
     }
 
+    /**
+     * 获取商品基础信息
+     * @param int $goodsId
+     * @return mixed
+     * @throws BaseException
+     */
+    public function getBasic(int $goodsId)
+    {
+        // 关联查询
+        $with = ['images.file', 'skuList.image', 'video', 'videoCover'];
+        // 获取商品记录
+        $goodsInfo = static::detail($goodsId, $with);
+        empty($goodsInfo) && throwError('很抱歉,商品信息不存在');
+        // 整理商品数据并返回
+        return parent::setGoodsData($goodsInfo);
+    }
+
     /**
      * 添加商品
      * @param array $data
      * @return bool
+     * @throws \cores\exception\BaseException
      * @throws \think\db\exception\DataNotFoundException
      * @throws \think\db\exception\DbException
      * @throws \think\db\exception\ModelNotFoundException
      */
-    public function add(array $data)
+    public function add(array $data): bool
     {
         // 创建商品数据
         $data = $this->createData($data);
@@ -94,6 +111,7 @@ class Goods extends GoodsModel
      * 编辑商品
      * @param array $data
      * @return bool
+     * @throws \cores\exception\BaseException
      * @throws \think\db\exception\DataNotFoundException
      * @throws \think\db\exception\DbException
      * @throws \think\db\exception\ModelNotFoundException
@@ -176,7 +194,7 @@ class Goods extends GoodsModel
      * @throws \think\db\exception\DataNotFoundException
      * @throws \think\db\exception\DbException
      * @throws \think\db\exception\ModelNotFoundException
-     * @throws BaseException
+     * @throws \cores\exception\BaseException
      */
     private function createData(array $data): array
     {