فهرست منبع

fix(coupon): COUPON-C3-FIX 领券中心改用可取券模板接口,修复「立即领取」必然 500

【COUPON-C3】领券中心 coupon-center.vue 原调 /api/coupon/list,该端点返回的是本家庭
「已拥有 AVAILABLE 券实例」(family_coupon),并非可取券模板,导致点「立即领取」100% 命中
claim 去重分支返回「已领取过该优惠券」(code=500),功能完全不可用。

- 后端 CouponService.listClaimable(userId):返回家庭可取券模板,口径与 claim() 逐项对齐
  (status=ACTIVE + 有效期内 validFrom/validUntil 为空不限 + 未领完 totalCount/usedCount
  + family_coupon 中无 (familyId,couponId) 记录,任意状态均视为已领取),
  保证「展示的券必可领取成功」;无 DB 迁移,纯读查询
- 后端 CouponController 新增 POST /api/coupon/claimable
- cfc-frontend/utils/api.js 新增 getClaimableCoupons();coupon-center.vue 改用该接口拉可取券
- pages.json 注册 pages/profile-extra/coupon-center(标题:领券中心)
- coupons.vue 空状态文案改为可点击跳转真实领券中心页(goToCouponCenter)
- docs/superpowers/api/API_REFERENCE.md 4.32 + docs/superpowers/PROJECT-OVERVIEW.md 登记新端点

核验:mvn -q -DskipTests compile BUILD SUCCESS;verify-vue-compile PASS(模板/样式/脚本 2/2);
audit-mp-antipatterns ERROR 0;npm run build:mp-weixin DONE(coupon-center 产物 + app.json 路由 +
vendor.js 含 coupon/claimable 均已核对;Node 24 需 util.isRegExp 兼容垫片,HBuilderX 打包不受影响)
E2E Test Bot 2 هفته پیش
والد
کامیت
484a9677ce

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

@@ -39,6 +39,15 @@ public class CouponController {
         return Result.error(result);
     }
 
+    /**
+     * 领券中心:返回当前家庭可领取的券模板列表(未拥有、未领完、有效期内、ACTIVE)。
+     * 与 /api/coupon/list(家庭已拥有的券实例)语义不同,供「领券中心」页展示可取券。
+     */
+    @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

@@ -130,6 +130,54 @@ public class CouponService {
         return result;
     }
 
+    /**
+     * 领券中心:返回该家庭「可领取」的券模板列表。
+     * 语义与 {@link #claim} 校验逐项对齐,保证「展示的券必可领取成功」:
+     *   - 模板 status=ACTIVE(claim 对非 ACTIVE 一律拒绝,NULL 视为停用)
+     *   - 在有效期内(validFrom/validUntil 为 NULL 视为不限,与 claim 判定一致)
+     *   - 未领完(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);

+ 6 - 0
cfc-frontend/pages.json

@@ -1794,6 +1794,12 @@
             "navigationBarTitleText": "我的优惠券"
           }
         },
+        {
+          "path": "coupon-center",
+          "style": {
+            "navigationBarTitleText": "领券中心"
+          }
+        },
         {
           "path": "gates",
           "style": {

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

@@ -0,0 +1,291 @@
+<template>
+  <view class="page" :style="{ '--font-scale': fontScale }">
+    <view v-if="loading" class="loading-wrap">
+      <text class="loading-text">加载中...</text>
+    </view>
+    <view v-else-if="couponList.length === 0" class="empty-state">
+      <text class="empty-icon">🎫</text>
+      <text class="empty-text">暂无可领取的优惠券</text>
+      <text class="empty-hint">平台发放的优惠券将在这里展示,敬请期待</text>
+    </view>
+    <scroll-view v-else scroll-y class="coupon-scroll">
+      <view class="coupon-list">
+        <view v-for="item in couponList" :key="item.id" class="coupon-card">
+          <view class="card-left">
+            <text class="card-value">{{ getCardValueText(item) }}</text>
+            <text class="card-type-label">{{ getTypeLabel(item.type) }}</text>
+          </view>
+          <view class="card-right">
+            <view class="card-header">
+              <text class="card-name">{{ item.name }}</text>
+            </view>
+            <text class="card-condition">{{ getConditionText(item) }}</text>
+            <text class="card-scope">{{ getScopeText(item.applicableTo) }}</text>
+            <text class="card-expiry">有效期至 {{ formatDate(item.validUntil) }}</text>
+            <button class="claim-btn" @click="handleClaim(item)">立即领取</button>
+          </view>
+        </view>
+      </view>
+    </scroll-view>
+  </view>
+</template>
+
+<script>
+import { getClaimableCoupons, claimCoupon } from '../../utils/api.js'
+import { parseDate } from '../../utils/format.js'
+import fontScaleMixin from '@/mixins/font-scale.js'
+
+export default {
+  mixins: [fontScaleMixin],
+  data() {
+    return {
+      couponList: [],
+      loading: false,
+      claiming: false
+    }
+  },
+  onLoad() {
+    this.loadCoupons()
+    this._onLoadFired = true
+  },
+  onShow() {
+    // 首次进入由 onLoad 加载,避免 onLoad/onShow 双发;返回本页时刷新
+    if (this._onLoadFired) {
+      this._onLoadFired = false
+    } else {
+      this.loadCoupons()
+    }
+  },
+  methods: {
+    // 领券中心数据源:可取券模板(未拥有/未领完/有效期内/ACTIVE),与 /api/coupon/claim 去重口径一致
+    loadCoupons() {
+      var self = this
+      this.loading = true
+      getClaimableCoupons().then(function(res) {
+        if (res && res.code === 200) {
+          self.couponList = res.data || []
+        } else {
+          self.couponList = []
+        }
+      }).catch(function(e) {
+        console.error('加载领券中心失败', e)
+        self.couponList = []
+      }).then(function() {
+        self.loading = false
+      })
+    },
+    handleClaim(item) {
+      if (this.claiming) return
+      var self = this
+      uni.showModal({
+        title: '确认领取',
+        content: '确认领取「' + (item.name || '') + '」优惠券?',
+        success: function(r) {
+          if (!r.confirm) return
+          self.claiming = true
+          // /api/coupon/claim 契约:couponId 为券模板 id(claimable 返回的 couponId 字段)
+          claimCoupon(item.couponId || item.id).then(function(res) {
+            self.claiming = false
+            if (res.code === 200) {
+              uni.showToast({ title: res.message || '领取成功', icon: 'success' })
+              self.loadCoupons()
+            } else {
+              uni.showToast({ title: res.message || '领取失败', icon: 'none' })
+            }
+          }).catch(function(e) {
+            self.claiming = false
+            uni.showToast({ title: (e && e.message) || '领取失败', icon: 'none' })
+          })
+        }
+      })
+    },
+    getCardValueText(item) {
+      if (item.type === 'DISCOUNT') {
+        return ((item.discountRate || 0) / 100).toFixed(1) + '折'
+      }
+      if (item.value !== null && item.value !== undefined) {
+        return '¥' + (item.value / 100).toFixed(2)
+      }
+      return '--'
+    },
+    getTypeLabel(type) {
+      if (type === 'FIXED') return '满减券'
+      if (type === 'CASH') return '无门槛券'
+      if (type === 'DISCOUNT') return '折扣券'
+      return type || ''
+    },
+    getConditionText(item) {
+      if (item.type === 'DISCOUNT') {
+        return '折扣券 ' + ((item.discountRate || 0) / 100).toFixed(1) + '折'
+      }
+      if (item.minSpend && item.minSpend > 0) {
+        return '满' + (item.minSpend / 100).toFixed(2) + '元可用'
+      }
+      return '无门槛'
+    },
+    getScopeText(applicableTo) {
+      if (applicableTo === 'ALL') return '全品类适用'
+      if (applicableTo === 'MEMBERSHIP') return '会员专享'
+      return applicableTo || '全品类适用'
+    },
+    formatDate(date) {
+      if (!date) return ''
+      var d = parseDate(date)
+      if (!d) return date
+      var y = d.getFullYear()
+      var m = String(d.getMonth() + 1).padStart(2, '0')
+      var day = String(d.getDate()).padStart(2, '0')
+      return y + '.' + m + '.' + day
+    }
+  }
+}
+</script>
+
+<style scoped lang="scss">
+.page {
+  min-height: 100vh;
+  background: $facade-fill-2;
+}
+
+.coupon-scroll {
+  height: 100vh;
+}
+
+.loading-wrap {
+  display: flex;
+  justify-content: center;
+  padding-top: 200rpx;
+}
+
+.loading-text {
+  font-size: calc(28rpx * var(--font-scale, 1));
+  color: $facade-text-muted;
+}
+
+.empty-state {
+  display: flex;
+  flex-direction: column;
+  align-items: center;
+  padding-top: 200rpx;
+}
+
+.empty-icon {
+  font-size: calc(80rpx * var(--font-scale, 1));
+  margin-bottom: 20rpx;
+}
+
+.empty-text {
+  font-size: calc(28rpx * var(--font-scale, 1));
+  color: $facade-text-muted;
+}
+
+.empty-hint {
+  font-size: calc(24rpx * var(--font-scale, 1));
+  color: $facade-text-ghost;
+  margin-top: 12rpx;
+}
+
+.coupon-list {
+  padding: 20rpx;
+}
+
+.coupon-card {
+  display: flex;
+  background: $app-surface;
+  border-radius: 16rpx;
+  margin-bottom: 20rpx;
+  overflow: hidden;
+  box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.06);
+}
+
+.card-left {
+  width: 200rpx;
+  display: flex;
+  flex-direction: column;
+  align-items: center;
+  justify-content: center;
+  background: linear-gradient(135deg, $brand-primary, $brand-primary-light);
+  padding: 30rpx 12rpx;
+  flex-shrink: 0;
+}
+
+.card-value {
+  font-size: calc(40rpx * var(--font-scale, 1));
+  font-weight: bold;
+  color: $app-surface;
+}
+
+.card-type-label {
+  font-size: calc(20rpx * var(--font-scale, 1));
+  color: rgba(255, 255, 255, 0.8);
+  margin-top: 8rpx;
+}
+
+.card-right {
+  flex: 1;
+  padding: 24rpx 24rpx 20rpx;
+  position: relative;
+}
+
+.card-header {
+  display: flex;
+  align-items: center;
+  justify-content: space-between;
+  margin-bottom: 10rpx;
+}
+
+.card-name {
+  font-size: calc(28rpx * var(--font-scale, 1));
+  font-weight: 600;
+  color: $facade-text-strong;
+  flex: 1;
+  overflow: hidden;
+  text-overflow: ellipsis;
+  white-space: nowrap;
+}
+
+.card-condition {
+  font-size: calc(24rpx * var(--font-scale, 1));
+  color: $brand-primary;
+  display: block;
+  margin-bottom: 6rpx;
+}
+
+.card-scope {
+  font-size: calc(22rpx * var(--font-scale, 1));
+  color: $uni-info;
+  background: $tint-sky-ghost;
+  display: inline-block;
+  padding: 2rpx 12rpx;
+  border-radius: 8rpx;
+  margin-bottom: 6rpx;
+}
+
+.card-expiry {
+  font-size: calc(22rpx * var(--font-scale, 1));
+  color: $facade-text-ghost-light;
+  display: block;
+}
+
+.claim-btn {
+  position: absolute;
+  right: 24rpx;
+  bottom: 20rpx;
+  background: linear-gradient(135deg, $brand-primary, $brand-primary-light);
+  color: $app-surface;
+  font-size: calc(24rpx * var(--font-scale, 1));
+  padding: 10rpx 28rpx;
+  border-radius: 28rpx;
+  border: none;
+  line-height: 1.4;
+  height: auto;
+}
+
+.claim-btn::after {
+  border: none;
+}
+
+.claim-btn:active {
+  opacity: 0.85;
+}
+</style>

+ 12 - 1
cfc-frontend/pages/profile-extra/coupons.vue

@@ -57,7 +57,7 @@
       <view v-else-if="filteredList.length === 0" class="empty-state">
         <text class="empty-icon">🎫</text>
         <text class="empty-text">{{ tabIndex === 0 ? '暂无可用优惠券' : tabIndex === 1 ? '暂无已使用优惠券' : '暂无已过期优惠券' }}</text>
-        <text class="empty-hint" v-if="tabIndex === 0">去领券中心看看有哪些优惠吧</text>
+        <view class="empty-hint empty-hint-link" v-if="tabIndex === 0" @click="goToCouponCenter">去领券中心看看有哪些优惠吧 ›</view>
       </view>
       <view v-else class="coupon-list">
         <view
@@ -284,6 +284,9 @@ export default {
     },
     goToMembership() {
       uni.navigateTo({ url: '/pages/membership/index' })
+    },
+    goToCouponCenter() {
+      uni.navigateTo({ url: '/pages/profile-extra/coupon-center' })
     }
   }
 }
@@ -368,6 +371,14 @@ export default {
   margin-top: 12rpx;
 }
 
+.empty-hint-link {
+  color: $brand-primary;
+}
+
+.empty-hint-link:active {
+  opacity: 0.8;
+}
+
 .coupon-list {
   padding: 20rpx;
 }

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

@@ -2619,6 +2619,11 @@ export const getCouponList = () => {
   return request('/api/coupon/list', 'POST')
 }
 
+// Coupon center: coupon templates the current family can still claim
+export const getClaimableCoupons = () => {
+  return request('/api/coupon/claimable', 'POST')
+}
+
 export const getMyCoupons = () => {
   return request('/api/coupon/my', 'POST')
 }

تفاوت فایلی نمایش داده نمی شود زیرا این فایل بسیار بزرگ است
+ 0 - 0
docs/superpowers/PROJECT-OVERVIEW.md


+ 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` | 领券中心:当前家庭可取券模板列表(未拥有/未领完/有效期内/ACTIVE) |
 | `POST /api/coupon/checkout-list` | 结算页可兑换未拥有券 |
 
 ---

برخی فایل ها در این مقایسه diff نمایش داده نمی شوند زیرا تعداد فایل ها بسیار زیاد است