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

fix(group-buy): 细分团购活动失效提示

- 落地页和开团核价按活动开始、结束及商品下架状态返回明确提示
- 保持原有有效商品查询兼容,并在严格校验模式下统一处理失效原因

API: 团购落地页与开团接口的业务错误提示更精确
shizhongqi 1 день назад
Родитель
Сommit
57283fd0fc

+ 2 - 8
app-mall/controllers/GroupBuyController.php

@@ -46,10 +46,7 @@ class GroupBuyController extends BaseController
         if ($goodsId <= 0) {
             util::fail('请选择商品');
         }
-        $row = GroupBuyActivityClass::getActiveGoodsRowByGoodsId($mainId, intval($this->shopId), $goodsId);
-        if (empty($row)) {
-            util::fail('团购活动已结束或商品已下架');
-        }
+        $row = GroupBuyActivityClass::requireActiveGoodsRowByGoodsId($mainId, intval($this->shopId), $goodsId);
         $goods = GoodsClass::getById($goodsId, true);
         $cover = strval($row['cover'] ?? '');
         $coverUrl = $cover;
@@ -183,10 +180,7 @@ class GroupBuyController extends BaseController
         $activityGoodsId = intval($form->activityGoodsId);
         $goodsNum = max(1, intval($form->goodsNum));
 
-        $row = GroupBuyActivityClass::getActiveGoodsRowById($mainId, intval($this->shopId), $activityGoodsId);
-        if (empty($row)) {
-            util::fail('团购活动已结束或商品已下架');
-        }
+        $row = GroupBuyActivityClass::requireActiveGoodsRowById($mainId, intval($this->shopId), $activityGoodsId);
         GroupBuyClass::assertPurchaseLimit($mainId, $customId, $row, $goodsNum);
 
         $connection = Yii::$app->db;

+ 75 - 4
biz-hd/groupBuy/classes/GroupBuyActivityClass.php

@@ -288,22 +288,32 @@ class GroupBuyActivityClass extends BaseClass
      * @param int $mainId
      * @param int $shopId
      * @param int $activityGoodsId
+     * @param bool $strict true 时按原因抛错:活动未开始/已结束、商品已下架
      * @return array|null
      */
-    public static function getActiveGoodsRowById($mainId, $shopId, $activityGoodsId)
+    public static function getActiveGoodsRowById($mainId, $shopId, $activityGoodsId, $strict = false)
     {
         $mainId = intval($mainId);
         $shopId = intval($shopId);
         $activityGoodsId = intval($activityGoodsId);
         if ($mainId <= 0 || $activityGoodsId <= 0) {
+            if ($strict) {
+                util::fail('请选择团购商品');
+            }
             return null;
         }
         $row = GroupBuyGoodsClass::getById($activityGoodsId, true);
-        if (empty($row) || intval($row->mainId) !== $mainId || intval($row->delStatus) !== 0 || intval($row->status) !== 1) {
+        if (empty($row) || intval($row->mainId) !== $mainId) {
+            if ($strict) {
+                util::fail('商品已下架');
+            }
             return null;
         }
         $activity = self::getById(intval($row->activityId), true);
         if (empty($activity) || intval($activity->mainId) !== $mainId || intval($activity->delStatus) !== 0) {
+            if ($strict) {
+                util::fail('团购活动已结束');
+            }
             return null;
         }
         $enabled = HomePageConfigClass::getModuleEnabled($mainId, $shopId, 'groupBuy');
@@ -313,6 +323,15 @@ class GroupBuyActivityClass extends BaseClass
             $enabled
         );
         if ($status !== 1) {
+            if ($strict) {
+                self::failIfActivityNotRunning($status);
+            }
+            return null;
+        }
+        if (intval($row->delStatus) !== 0 || intval($row->status) !== 1) {
+            if ($strict) {
+                util::fail('商品已下架');
+            }
             return null;
         }
         return [
@@ -341,24 +360,44 @@ class GroupBuyActivityClass extends BaseClass
         ];
     }
 
+    /**
+     * 开团/落地页核价:取有效商品行,活动未开始/已结束、商品已下架分别报错
+     *
+     * @param int $mainId
+     * @param int $shopId
+     * @param int $activityGoodsId
+     * @return array
+     */
+    public static function requireActiveGoodsRowById($mainId, $shopId, $activityGoodsId)
+    {
+        return self::getActiveGoodsRowById($mainId, $shopId, $activityGoodsId, true);
+    }
+
     /**
      * 按商品 id 取当前进行中活动的有效商品行
      *
      * @param int $mainId
      * @param int $shopId
      * @param int $goodsId
+     * @param bool $strict true 时按原因抛错:活动未开始/已结束、商品已下架
      * @return array|null
      */
-    public static function getActiveGoodsRowByGoodsId($mainId, $shopId, $goodsId)
+    public static function getActiveGoodsRowByGoodsId($mainId, $shopId, $goodsId, $strict = false)
     {
         $mainId = intval($mainId);
         $shopId = intval($shopId);
         $goodsId = intval($goodsId);
         if ($mainId <= 0 || $goodsId <= 0) {
+            if ($strict) {
+                util::fail('请选择商品');
+            }
             return null;
         }
         $activity = self::getCurrentActivity($mainId);
         if (empty($activity)) {
+            if ($strict) {
+                util::fail('团购活动已结束');
+            }
             return null;
         }
         $enabled = HomePageConfigClass::getModuleEnabled($mainId, $shopId, 'groupBuy');
@@ -368,6 +407,9 @@ class GroupBuyActivityClass extends BaseClass
             $enabled
         );
         if ($status !== 1) {
+            if ($strict) {
+                self::failIfActivityNotRunning($status);
+            }
             return null;
         }
         $row = GroupBuyGoodsClass::getByCondition([
@@ -378,8 +420,37 @@ class GroupBuyActivityClass extends BaseClass
             'delStatus' => 0,
         ], true);
         if (empty($row)) {
+            if ($strict) {
+                util::fail('商品已下架');
+            }
             return null;
         }
-        return self::getActiveGoodsRowById($mainId, $shopId, intval($row->id));
+        return self::getActiveGoodsRowById($mainId, $shopId, intval($row->id), $strict);
+    }
+
+    /**
+     * 落地页核价:按商品 id 取有效行,活动未开始/已结束、商品已下架分别报错
+     *
+     * @param int $mainId
+     * @param int $shopId
+     * @param int $goodsId
+     * @return array
+     */
+    public static function requireActiveGoodsRowByGoodsId($mainId, $shopId, $goodsId)
+    {
+        return self::getActiveGoodsRowByGoodsId($mainId, $shopId, $goodsId, true);
+    }
+
+    /**
+     * 活动非进行中时给出明确错误
+     *
+     * @param int $status 0未开始 1进行中 2已结束
+     */
+    private static function failIfActivityNotRunning($status)
+    {
+        if (intval($status) === 0) {
+            util::fail('团购活动未开始');
+        }
+        util::fail('团购活动已结束');
     }
 }