فهرست منبع

fix(goods): 修正规格商品详情图文取值

- 商城商品详情按主商品查询商品图文和默认分类图文,避免规格商品缺少关联内容
- 花店端商城商品详情保留统一商品说明字段,并统一商品变量命名
- 整理商品使用场景和多规格同步代码,移除未使用的服务层透传方法
API: 商城商品详情对规格商品的 picTextGoods/picText 取值改为跟随主商品
shizhongqi 1 هفته پیش
والد
کامیت
f5a968a23c

+ 7 - 4
app-hd/controllers/GoodsController.php

@@ -458,12 +458,15 @@ class GoodsController extends BaseController
     public function actionMallDetail()
     {
         $id = Yii::$app->request->get('id', 0);
-        $info = GoodsClass::getGoodsInfo($id);
-        GoodsService::valid($info, $this->mainId);
+        $goods = GoodsClass::getGoodsInfo($id);
+        GoodsService::valid($goods, $this->mainId);
+
+        // 取商家商品统一配置表中的商品说明
         $setting = GoodsSettingService::getByCondition(['mainId' => $this->mainId]);
         $commonIntro = isset($setting['goodsIntroduce']) ? $setting['goodsIntroduce'] : '';
-        $info['commonIntro'] = $commonIntro;
-        util::success($info);
+        $goods['commonIntro'] = $commonIntro;
+
+        util::success($goods);
     }
 
     public function actionIndex()

+ 28 - 20
app-mall/controllers/GoodsController.php

@@ -17,44 +17,52 @@ class GoodsController extends BaseController
     public function actionDetail()
     {
         $id = Yii::$app->request->get('id', 0);
-        $categoryId = intval(Yii::$app->request->get('categoryId'));
-        if ($categoryId <= 0 && $id > 0) {
-            $categoryMap = GoodsCategoryClass::getGoodsHasCategoryIdsBatch([$id]);
-            $categoryIds = $categoryMap[$id] ?? [];
-            if (!empty($categoryIds)) {
-                $categoryId = intval($categoryIds[0]);
-            }
-        }
+
         $custom = $this->custom;
         if (empty($custom)) {
             util::fail('用户信息缺失');
         }
         $shop = $this->shop;
         $params = [];
-        $info = GoodsClass::getGoodsInfo($id, $shop, $custom, $params);
+        $goods = GoodsClass::getGoodsInfo($id, $shop, $custom, $params);
 
-        if ($info['mainId'] != $this->mainId) {
+        if ($goods['mainId'] != $this->mainId) {
             util::fail('不是你的商品');
         }
-        $info = \bizHd\goods\classes\GoodsClass::appendUseCasesAndSpecs($info, $this->mainId);
+        $goods = \bizHd\goods\classes\GoodsClass::appendUseCasesAndSpecs($goods, $this->mainId);
 
-        $setting = GoodsSettingClass::getByCondition(['mainId' => $this->mainId]);
-        $intro = $setting['goodsIntroduce'] ?? '';
-        $info['commonIntro'] = $intro;
+        // $setting = GoodsSettingClass::getByCondition(['mainId' => $this->mainId]);
+        // $intro = $setting['goodsIntroduce'] ?? '';
+        // $info['commonIntro'] = $intro;
 
-        $pixTextGoods = PicTextGoodsClass::getByCondition(['mainId' => $this->mainId, 'goodsId' => $id], true);
-        $info['picTextGoods'] = $pixTextGoods;
+        // 商品详情图文
+        if (empty($goods['masterId'])) {
+            $picTextGoodsId = $id;
+        } else {
+            $picTextGoodsId = $goods['masterId'];
+        }
+        $pixTextGoods = PicTextGoodsClass::getByCondition(['mainId' => $this->mainId, 'goodsId' => $picTextGoodsId], true);
+        $goods['picTextGoods'] = $pixTextGoods;
 
-        $info['picText'] = [];
+        $categoryId = intval(Yii::$app->request->get('categoryId'));
+        if ($categoryId <= 0 && $id > 0) {
+            $categoryMap = GoodsCategoryClass::getGoodsHasCategoryIdsBatch([$picTextGoodsId]);
+            $categoryIds = $categoryMap[$picTextGoodsId] ?? [];
+            if (!empty($categoryIds)) {
+                $categoryId = intval($categoryIds[0]);
+            }
+        }
+        // 商品所属分类图文
+        $goods['picText'] = [];
         $category = CategoryClass::getById($categoryId);
         if (!empty($category)) {
             $picTextId = $category['picTextId'];
             $picText = PicTextClass::getById($picTextId, true);
-            $info['picText'] = $picText;
+            $goods['picText'] = $picText;
         }
-        $info['customId'] = $this->customId;
+        $goods['customId'] = $this->customId;
 
-        util::success($info);
+        util::success($goods);
     }
 
 }

+ 34 - 12
biz-hd/goods/classes/GoodsClass.php

@@ -9,8 +9,6 @@ use biz\stat\classes\StatCgPlantClass;
 use biz\stat\classes\StatOutClass;
 use bizHd\goods\services\GoodsCategoryService;
 use bizHd\message\classes\ChatClass;
-use bizHd\order\classes\OrderClass;
-use bizHd\work\classes\WorkClass;
 use bizHd\work\classes\WorkItemClass;
 use common\components\business;
 use common\components\dict;
@@ -33,14 +31,18 @@ class GoodsClass extends BaseClass
     {
         $ids = array_values(array_unique(array_filter(array_map('intval', is_array($ids) ? $ids : []))));
         $valid = UseCaseClass::getAllByCondition(['mainId'=>$mainId, 'delStatus'=>0, 'status'=>1], null, 'id', 'id');
-        foreach ($ids as $id) if (!isset($valid[$id])) util::fail('使用场景不可用');
+        foreach ($ids as $id) {
+            if (!isset($valid[$id])) util::fail('使用场景不可用');
+        }
         GoodsUseCaseClass::deleteByCondition(['goodsId'=>$goodsId]);
-        foreach ($ids as $id) GoodsUseCaseClass::add(['mainId'=>$mainId, 'goodsId'=>$goodsId, 'useCaseId'=>$id]);
+        foreach ($ids as $id) {
+            GoodsUseCaseClass::add(['mainId'=>$mainId, 'goodsId'=>$goodsId, 'useCaseId'=>$id]);
+        }
     }
 
     public static function appendUseCasesAndSpecs($goods, $mainId)
     {
-        $id = $goods['id'] ?? 0;
+        $id = $goods['id'];
         $relations = GoodsUseCaseClass::getAllByCondition(['goodsId'=>$id], null, 'useCaseId');
         $goods['useCaseIdList'] = array_map('intval', array_column($relations, 'useCaseId'));
         $specs = self::getAllByCondition(['masterId'=>$id, 'delStatus'=>0], 'id ASC', '*');
@@ -58,11 +60,13 @@ class GoodsClass extends BaseClass
 
     public static function syncSpecs($master, $data)
     {
-        $masterId = is_array($master) ? ($master['id'] ?? 0) : ($master->id ?? 0);
-        $enabled = (int)($data['specEnabled'] ?? 0);
+        $masterId = $master->id;
+        $enabled = (int)($data['specEnabled']);
         $old = self::getAllByCondition(['masterId'=>$masterId, 'delStatus'=>0], null, '*', 'id');
         if (!$enabled) {
-            if (!empty($old)) self::updateByCondition(['masterId'=>$masterId], ['delStatus'=>1]);
+            if (!empty($old)) {
+                self::updateByCondition(['masterId'=>$masterId], ['delStatus'=>1]);
+            }
             return;
         }
         $base = self::getById($masterId);
@@ -76,19 +80,36 @@ class GoodsClass extends BaseClass
             $id = (int)($spec['id'] ?? 0); $kept[] = $id;
             $fields = $base;
             unset($fields['createTime']);
-            $fields = array_merge($fields, ['specName'=>trim($spec['specName'] ?? ''),'price'=>(float)($spec['price'] ?? 0),'priceType'=>(int)($spec['priceType'] ?? 0),'stock'=>(int)($spec['stock'] ?? 0),'stockSet'=>(int)($spec['stockSet'] ?? 0),'flowerNum'=>(int)($spec['flowerNum'] ?? 0),'weight'=>(float)($spec['weight'] ?? 1),'sold'=>(int)($spec['sold'] ?? 0),'masterId'=>$masterId,'status'=>(int)($data['status'] ?? 1),'delStatus'=>0]);
+            $fields = array_merge($fields, [
+                    'specName'=>trim($spec['specName'] ?? ''),
+                    'price'=>(float)($spec['price'] ?? 0),
+                    'priceType'=>(int)($spec['priceType'] ?? 0),
+                    'stock'=>(int)($spec['stock'] ?? 0),
+                    'stockSet'=>(int)($spec['stockSet'] ?? 0),
+                    'flowerNum'=>(int)($spec['flowerNum'] ?? 0),
+                    'weight'=>(float)($spec['weight'] ?? 1),
+                    'sold'=>(int)($spec['sold'] ?? 0),
+                    'masterId'=>$masterId,
+                    'status'=>(int)($data['status'] ?? 1),
+                    'delStatus'=>0
+                ]
+            );
             $specImg = $spec['shopImg'] ?? [];
             if (is_array($specImg) && !empty($specImg)) {
                 $fields['shopImg'] = json_encode($specImg);
                 $fields['cover'] = !empty($spec['cover']) && in_array($spec['cover'], $specImg, true) ? $spec['cover'] : $specImg[0];
             }
-            if ($id && isset($old[$id])) self::updateById($id, $fields);
-            else {
+            if ($id && isset($old[$id])) {
+                self::updateById($id, $fields);
+            } else {
                 $child = $fields; $child['sn'] = orderSn::getGoodsSn($child); $child['createTime']=date('Y-m-d H:i:s'); self::add($child, true);
             }
         }
+
         $remove = array_diff(array_keys($old), array_filter($kept));
-        if ($remove) \bizHd\goods\models\Goods::updateAll(['delStatus'=>1], ['and', ['masterId'=>$masterId], ['in', 'id', $remove]]);
+        if ($remove) {
+            \bizHd\goods\models\Goods::updateAll(['delStatus'=>1], ['and', ['masterId'=>$masterId], ['in', 'id', $remove]]);
+        }
     }
 
     public static function orderCreateGoods($data)
@@ -509,6 +530,7 @@ class GoodsClass extends BaseClass
      * 批量判断主商品是否启用多规格(masterId 指向该商品且未删除)
      * @param array $goodsIds 主商品 id 列表
      * @return array [goodsId => 1|0]
+     * @throws \Exception
      */
     public static function getSpecEnabledMap($goodsIds)
     {

+ 0 - 6
biz-hd/goods/services/GoodsService.php

@@ -37,12 +37,6 @@ class GoodsService extends BaseService
         }
     }
 
-    //获取商品完整详情,包括分类 ssh 2019.12.3
-    public static function getGoodsInfo($id)
-    {
-        return GoodsClass::getGoodsInfo($id);
-    }
-
     //删除商品 ssh 2019.12.7
     public static function deleteGoods($goods)
     {