فهرست منبع

fix(coupon): 结算预览透传订单商品ID,商品绑定券不再误报"优惠券不可用"

根因: e27934c51 给 CouponService.apply 增加商品维度校验(productIds 集合),
下单创建(ProductOrderService:265)已传商品ID集合,但结算预览
CouponController /api/coupon/apply 仍走 4 参 apply(无商品上下文→productIds=null),
商品绑定券(如 coupon=3 菌群测评优惠券 product_id=3)在预览恒判不可用。
用户选券后提示"优惠券不可用",而实际下单创建校验可通过,前后不一致。

用户实测: userCouponId=2(family_coupon→coupon=3 菌群测评券,product_id=3),
orderType=PRODUCT, orderAmount=131400, 预览报"优惠券不可用"。

修复(与下单创建对齐):
- CouponController.apply: 从请求体透传 productIds 列表给 5 参 apply
- checkout.vue applyCouponDiscount: 收集订单商品ID(productIds)随券试算提交

验证: mvn compile EXIT=0;cfc-frontend build:mp-weixin EXIT=0。
Xiaogang Liao 1 هفته پیش
والد
کامیت
e88c9db440

+ 12 - 1
cfc-backend/src/main/java/com/etotem/cfc/controller/CouponController.java

@@ -45,7 +45,18 @@ public class CouponController {
         Long userCouponId = ParamUtils.getLong(params.get("userCouponId"));
         String orderType = (String) params.get("orderType");
         Integer orderAmount = Integer.valueOf(params.get("orderAmount").toString());
-        Integer discount = couponService.apply(userId, userCouponId, orderType, orderAmount);
+        // 结算预览:透传本次订单商品 ID 集合,与下单创建(ProductOrderService)的商品维度校验保持一致。
+        // 否则商品绑定券(如菌群测评券 product_id=3)在预览端因无商品上下文(productIds=null)恒判"不可用"。
+        List<Long> productIds = null;
+        Object rawProductIds = params.get("productIds");
+        if (rawProductIds instanceof java.util.Collection && !((java.util.Collection<?>) rawProductIds).isEmpty()) {
+            productIds = new ArrayList<>();
+            for (Object o : (java.util.Collection<?>) rawProductIds) {
+                if (o == null) continue;
+                productIds.add(o instanceof Number ? ((Number) o).longValue() : Long.valueOf(o.toString()));
+            }
+        }
+        Integer discount = couponService.apply(userId, userCouponId, orderType, orderAmount, productIds);
         if (discount == null) {
             return Result.error("优惠券不可用");
         }

+ 8 - 1
cfc-frontend/pages/shop/checkout/checkout.vue

@@ -823,10 +823,17 @@ export default {
       var applyingCouponId = this.selectedCoupon.id
       this.couponApplying = true
       try {
+        // 与下单提交(checkout.vue submitProductOrder)一致:透传订单商品 ID 集合,
+        // 商品绑定券(如菌群测评券 product_id=3)才能通过服务端商品维度校验
+        var couponProductIds = []
+        for (var ci = 0; ci < this.items.length; ci++) {
+          if (this.items[ci].productId) couponProductIds.push(this.items[ci].productId)
+        }
         var res = await applyCoupon({
           userCouponId: this.selectedCoupon.id,
           orderType: 'PRODUCT',
-          orderAmount: this.actualAmount || 0
+          orderAmount: this.actualAmount || 0,
+          productIds: couponProductIds
         })
         // 期间用户可能已改选/取消券,丢弃过期响应
         if (!this.selectedCoupon || this.selectedCoupon.id !== applyingCouponId) {