优惠券发放和展示功能优化
This commit is contained in:
Vendored
+2
@@ -51,8 +51,10 @@ declare module 'vue' {
|
||||
ARadio: typeof import('ant-design-vue/es')['Radio']
|
||||
ARadioGroup: typeof import('ant-design-vue/es')['RadioGroup']
|
||||
ARow: typeof import('ant-design-vue/es')['Row']
|
||||
ASegmented: typeof import('ant-design-vue/es')['Segmented']
|
||||
ASelect: typeof import('ant-design-vue/es')['Select']
|
||||
ASelectOption: typeof import('ant-design-vue/es')['SelectOption']
|
||||
ASkeleton: typeof import('ant-design-vue/es')['Skeleton']
|
||||
ASpace: typeof import('ant-design-vue/es')['Space']
|
||||
ASpin: typeof import('ant-design-vue/es')['Spin']
|
||||
AStatistic: typeof import('ant-design-vue/es')['Statistic']
|
||||
|
||||
@@ -63,6 +63,12 @@ const router = createRouter({
|
||||
component: () => import('../views/SettingsView.vue'),
|
||||
meta: { requiresAuth: true, hideNav: true, isAdmin: false }
|
||||
},
|
||||
{
|
||||
path: '/coupons',
|
||||
name: 'coupons',
|
||||
component: () => import('../views/CouponView.vue'),
|
||||
meta: { requiresAuth: true, isAdmin: false ,hideNav: true}
|
||||
},
|
||||
{
|
||||
path: '/admin/login',
|
||||
name: 'admin-login',
|
||||
|
||||
@@ -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 = []
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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>
|
||||
|
||||
+1
@@ -93,6 +93,7 @@ public class CartServiceImpl implements CartService {
|
||||
cartVO.setPrice(product.getPrice());
|
||||
cartVO.setQuantity(cart.getQuantity());
|
||||
cartVO.setTotalPrice(product.getPrice().multiply(new BigDecimal(cart.getQuantity())));
|
||||
cartVO.setTags(product.getTags());
|
||||
cartVOList.add(cartVO);
|
||||
}
|
||||
}
|
||||
|
||||
+42
-20
@@ -287,31 +287,33 @@ public class CouponServiceImpl extends ServiceImpl<CouponMapper, CouponDO> imple
|
||||
}
|
||||
|
||||
// 检查优惠券数量是否足够
|
||||
if (couponDO.getUsedCount() + userIds.size() * quantity > couponDO.getTotalCount()) {
|
||||
int totalNeeded = userIds.size() * quantity;
|
||||
if (couponDO.getUsedCount() + totalNeeded > couponDO.getTotalCount()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 发放优惠券
|
||||
int distributedCount = 0;
|
||||
for (Long userId : userIds) {
|
||||
for (int i = 0; i < quantity; i++) {
|
||||
// 检查是否已经分配
|
||||
UserCouponDO existing = userCouponMapper.selectOne(new LambdaQueryWrapper<UserCouponDO>()
|
||||
.eq(UserCouponDO::getUserId, userId)
|
||||
.eq(UserCouponDO::getCouponId, couponId)
|
||||
.eq(UserCouponDO::getIsDelete, 0)
|
||||
);
|
||||
if (existing == null) {
|
||||
// 分配优惠券
|
||||
UserCouponDO userCouponDO = new UserCouponDO();
|
||||
userCouponDO.setUserId(userId);
|
||||
userCouponDO.setCouponId(couponId);
|
||||
userCouponDO.setStatus(1);
|
||||
userCouponDO.setObtainTime(LocalDateTime.now());
|
||||
userCouponMapper.insert(userCouponDO);
|
||||
}
|
||||
// 分配优惠券(不检查是否已存在,允许用户拥有多张相同优惠券)
|
||||
UserCouponDO userCouponDO = new UserCouponDO();
|
||||
userCouponDO.setUserId(userId);
|
||||
userCouponDO.setCouponId(couponId);
|
||||
userCouponDO.setStatus(1);
|
||||
userCouponDO.setObtainTime(LocalDateTime.now());
|
||||
userCouponMapper.insert(userCouponDO);
|
||||
distributedCount++;
|
||||
}
|
||||
}
|
||||
|
||||
// 更新优惠券使用数量
|
||||
couponDO.setUsedCount(couponDO.getUsedCount() + distributedCount);
|
||||
if (couponDO.getUsedCount() >= couponDO.getTotalCount()) {
|
||||
couponDO.setStatus(3); // 已用完
|
||||
}
|
||||
updateById(couponDO);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -378,18 +380,38 @@ public class CouponServiceImpl extends ServiceImpl<CouponMapper, CouponDO> imple
|
||||
return true;
|
||||
}
|
||||
|
||||
// 查询优惠券的所有标签关联
|
||||
List<CouponTagDO> couponTagDOs = couponTagMapper.selectList(new LambdaQueryWrapper<CouponTagDO>()
|
||||
.eq(CouponTagDO::getCouponId, couponId)
|
||||
.in(CouponTagDO::getTagId, productTagIds)
|
||||
);
|
||||
|
||||
// 如果有不可用标签,则返回false
|
||||
for (CouponTagDO couponTagDO : couponTagDOs) {
|
||||
if (couponTagDO.getIsAvailable() == 0) {
|
||||
// 分离可用标签和不可用标签
|
||||
List<Long> availableTagIds = couponTagDOs.stream()
|
||||
.filter(tag -> tag.getIsAvailable() == 1)
|
||||
.map(tag -> tag.getTagId())
|
||||
.collect(Collectors.toList());
|
||||
|
||||
List<Long> unavailableTagIds = couponTagDOs.stream()
|
||||
.filter(tag -> tag.getIsAvailable() == 0)
|
||||
.map(tag -> tag.getTagId())
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 检查是否有不可用标签
|
||||
for (Long tagId : productTagIds) {
|
||||
if (unavailableTagIds.contains(tagId)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// 检查是否只包含可用标签(如果有可用标签配置)
|
||||
if (!availableTagIds.isEmpty()) {
|
||||
for (Long tagId : productTagIds) {
|
||||
if (!availableTagIds.contains(tagId)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -13,4 +13,5 @@ public class CartVO {
|
||||
private BigDecimal price;
|
||||
private Integer quantity;
|
||||
private BigDecimal totalPrice;
|
||||
private String tags;
|
||||
}
|
||||
Reference in New Issue
Block a user