فهرست منبع

fix(coupon): C3 领券中心补后端可取券接口,修复领取按钮必然失败

【COUPON-C3】独立核验发现:/api/coupon/list 返回的是家庭「已拥有 AVAILABLE 券实例」,
领券中心据此展示的券点「立即领取」会 100% 命中 claim 去重分支返回「已领取过该优惠券」。

- CouponService.listClaimable(userId):返回家庭可取券模板(ACTIVE + 有效期内 + 未领完 +
  family_coupon 中无该模板记录),口径与 claim() 校验逐项对齐,保证展示即可领取成功
- CouponController 新增 POST /api/coupon/claimable(无 DB 迁移,纯读查询)
- cfc-frontend/utils/api.js 新增 getClaimableCoupons();coupon-center.vue 改用该接口拉可取券
- API_REFERENCE.md 4.32 补充新接口条目

核验:mvn clean compile BUILD SUCCESS(1955 源文件);verify-vue-compile PASS(2/2);
audit-mp-antipatterns ERROR 0。
E2E Test Bot 3 هفته پیش
والد
کامیت
74c1d84016

+ 8 - 0
cfc-backend/src/main/java/com/etotem/cfc/controller/CouponController.java

@@ -39,6 +39,14 @@ public class CouponController {
         return Result.error(result);
     }
 
+    /**
+     * 领券中心:返回该家庭可领取的券模板列表(未拥有、未领完、有效期内、ACTIVE)
+     */
+    @PostMapping("/claimable")
+    public Result<List<Map<String, Object>>> claimable(@RequestAttribute("userId") Long userId) {
+        return Result.success(couponService.listClaimable(userId));
+    }
+
     @PostMapping("/apply")
     public Result<Map<String, Object>> apply(@RequestAttribute("userId") Long userId,
                                              @RequestBody Map<String, Object> params) {

+ 48 - 0
cfc-backend/src/main/java/com/etotem/cfc/service/CouponService.java

@@ -123,6 +123,54 @@ public class CouponService {
         return result;
     }
 
+    /**
+     * 领券中心:返回该家庭「可领取」的券模板列表。
+     * 语义与 claim() 校验保持一致,保证「展示的券必可领取成功」:
+     *   - 模板 status=ACTIVE
+     *   - 在有效期内(validFrom/validUntil 为 NULL 视为不限)
+     *   - 未领完(totalCount/usedCount 配额未耗尽)
+     *   - 家庭尚未拥有该模板(family_coupon 中无 (familyId, couponId) 记录,任意状态均视为已领过,
+     *     与 claim() 去重口径一致)
+     */
+    public List<Map<String, Object>> listClaimable(Long userId) {
+        Long familyId = getFamilyId(userId);
+        if (familyId == null) return new ArrayList<>();
+        Date now = new Date();
+        List<Long> ownedIds = familyCouponMapper.selectList(
+                new LambdaQueryWrapper<FamilyCoupon>()
+                        .eq(FamilyCoupon::getFamilyId, familyId))
+                .stream().map(FamilyCoupon::getCouponId).collect(java.util.stream.Collectors.toList());
+        List<Coupon> templates = couponMapper.selectList(
+                new LambdaQueryWrapper<Coupon>()
+                        .eq(Coupon::getStatus, "ACTIVE")
+                        .and(w -> w.isNull(Coupon::getValidFrom).or().le(Coupon::getValidFrom, now))
+                        .and(w -> w.isNull(Coupon::getValidUntil).or().ge(Coupon::getValidUntil, now))
+                        .notIn(!ownedIds.isEmpty(), Coupon::getId, ownedIds)
+                        .orderByDesc(Coupon::getValue));
+        List<Map<String, Object>> result = new ArrayList<>();
+        for (Coupon c : templates) {
+            if (c.getTotalCount() != null && c.getUsedCount() != null
+                    && c.getUsedCount() >= c.getTotalCount()) {
+                continue;
+            }
+            Map<String, Object> item = new HashMap<>();
+            item.put("id", c.getId());
+            item.put("couponId", c.getId());
+            item.put("name", c.getName());
+            item.put("type", c.getType());
+            item.put("value", c.getValue());
+            item.put("discountRate", c.getDiscountRate());
+            item.put("minSpend", c.getMinSpend());
+            item.put("applicableTo", c.getApplicableTo());
+            item.put("validFrom", c.getValidFrom());
+            item.put("validUntil", c.getValidUntil());
+            item.put("productId", c.getProductId());
+            item.put("status", "ACTIVE");
+            result.add(item);
+        }
+        return result;
+    }
+
     @Transactional
     public String claim(Long userId, Long couponId) {
         Long familyId = getFamilyId(userId);

+ 2 - 2
cfc-frontend/pages/profile-extra/coupon-center.vue

@@ -31,7 +31,7 @@
 </template>
 
 <script>
-import { getCouponList, claimCoupon } from '../../utils/api.js'
+import { getClaimableCoupons, claimCoupon } from '../../utils/api.js'
 import { parseDate } from '../../utils/format.js'
 import fontScaleMixin from '@/mixins/font-scale.js'
 
@@ -60,7 +60,7 @@ export default {
     loadCoupons() {
       var self = this
       this.loading = true
-      getCouponList().then(function(res) {
+      getClaimableCoupons().then(function(res) {
         if (res && res.code === 200) {
           self.couponList = res.data || []
         } else {

+ 4 - 0
cfc-frontend/utils/api.js

@@ -2609,6 +2609,10 @@ export const getCouponList = () => {
   return request('/api/coupon/list', 'POST')
 }
 
+export const getClaimableCoupons = () => {
+  return request('/api/coupon/claimable', 'POST')
+}
+
 export const getMyCoupons = () => {
   return request('/api/coupon/my', 'POST')
 }

+ 1 - 0
docs/superpowers/api/API_REFERENCE.md

@@ -1062,6 +1062,7 @@ find cfc-backend/src/main/java -name "*XxxService.java" -o -name "*XxxController
 | `POST /api/coupon/my` | 我的(家庭)优惠券(小程序) |
 | `POST /api/coupon/apply` | 下单抵扣(家庭券) |
 | `POST /api/coupon/claim` | 领取优惠券(发到家庭) |
+| `POST /api/coupon/claimable` | 领券中心:当前家庭可取券模板列表(未拥有/未领完/有效期内) |
 | `POST /api/coupon/checkout-list` | 结算页可兑换未拥有券 |
 
 ---