优惠券发放和展示功能优化

This commit is contained in:
xiaowang
2026-02-28 15:45:16 +08:00
parent 29168a39fb
commit e74dc3faa4
7 changed files with 84 additions and 71 deletions
+15 -3
View File
@@ -215,6 +215,7 @@ interface CartItem {
price: number
quantity: number
totalPrice: number
tags: string
selected: boolean
}
@@ -290,17 +291,28 @@ const loadAvailableCoupons = async () => {
const totalPrice = subtotal.value
// 收集商品标签
const productTags = cartItems.value
const productTagsSet = new Set<string>()
cartItems.value
.filter(item => item.selected)
.map(item => item.productTitle.split(' ')[0]) // 简单处理,取商品标题的第一个词作为标签
.join(',')
.forEach(item => {
if (item.tags) {
const tags = item.tags.split(',').map(tag => tag.trim())
tags.forEach(tag => {
if (tag) productTagsSet.add(tag)
})
}
})
const productTags = Array.from(productTagsSet).join(',')
if (totalPrice > 0) {
const coupons = await couponApi.getAvailableCoupons(productTags, totalPrice)
availableCoupons.value = coupons
} else {
availableCoupons.value = []
}
} catch (error) {
console.error('获取可用优惠券失败:', error)
availableCoupons.value = []
}
}
+17 -48
View File
@@ -137,34 +137,7 @@
</div>
</a-card>
<!-- 优惠券列表 -->
<a-card v-if="activeTab === 'coupons'" :bordered="false" class="content-card">
<h3>我的优惠券</h3>
<a-empty v-if="coupons.length === 0" description="暂无优惠券">
<a-button type="primary">去领取</a-button>
</a-empty>
<div v-else class="coupon-list">
<a-card v-for="coupon in coupons" :key="coupon.id" class="coupon-card">
<div class="coupon-content">
<div class="coupon-value">
{{ coupon.couponType === 1 ? `${coupon.discountValue}折` : `¥${coupon.reduceAmount}` }}
</div>
<div class="coupon-info">
<div class="coupon-name">{{ coupon.couponName }}</div>
<div class="coupon-condition" v-if="coupon.couponType === 2">
满{{ coupon.minAmount }}元可用
</div>
<div class="coupon-time">
{{ coupon.startTime }} 至 {{ coupon.endTime }}
</div>
<div :class="['coupon-status', coupon.status === 1 ? 'available' : 'used']">
{{ coupon.statusName }}
</div>
</div>
</div>
</a-card>
</div>
</a-card>
<div class="logout-section">
<a-button danger block @click="handleLogout">
@@ -246,10 +219,9 @@ const editModalVisible = ref(false)
const saving = ref(false)
const uploading = ref(false)
// 订单和优惠券相关状态
// 订单相关状态
const activeTab = ref('')
const orders = ref<any[]>([])
const coupons = ref<any[]>([])
const loading = ref(false)
const orderCount = ref(0)
const couponCount = ref(0)
@@ -406,24 +378,11 @@ const loadOrders = async () => {
}
}
// 加载优惠券列表
const loadCoupons = async () => {
loading.value = true
try {
const data = await couponApi.listUserCoupons()
coupons.value = data
couponCount.value = data.length
} catch (error) {
message.error('获取优惠券列表失败')
} finally {
loading.value = false
}
}
// 处理优惠券点击
const handleCouponClick = () => {
activeTab.value = 'coupons'
loadCoupons()
router.push('/coupons')
}
// 处理取消订单
@@ -450,8 +409,7 @@ const handleMenuClick = (key: string) => {
} else if (key === 'orders') {
router.push('/orders')
} else if (key === 'coupons') {
activeTab.value = 'coupons'
loadCoupons()
router.push('/coupons')
} else if (key === 'address') {
message.info('地址管理功能开发中...')
} else if (key === 'payment') {
@@ -508,8 +466,19 @@ const getRandomColor = () => {
onMounted(() => {
fetchUserInfo()
loadOrders()
loadCoupons()
// 异步加载优惠券数量
loadCouponCount()
})
// 加载优惠券数量
const loadCouponCount = async () => {
try {
const data = await couponApi.listUserCoupons()
couponCount.value = data.length
} catch (error) {
console.error('获取优惠券数量失败:', error)
}
}
</script>
<style scoped>