优惠券的使用功能优化

This commit is contained in:
xiaowang
2026-02-28 17:09:45 +08:00
parent e74dc3faa4
commit c95394260d
9 changed files with 631 additions and 49 deletions
+54 -12
View File
@@ -130,16 +130,41 @@
v-model:visible="showCouponModal"
title="选择优惠券"
width="90%"
@ok="confirmCouponSelection"
@cancel="cancelCouponSelection"
>
<div class="coupon-modal-content">
<a-empty v-if="availableCoupons.length === 0" description="暂无可用优惠券" />
<div v-else class="coupon-list">
<!-- 取消选择选项 -->
<a-card
class="modal-coupon-card"
:class="{ active: !selectedCoupon }"
@click="selectedCoupon = null"
>
<div class="coupon-content">
<div class="coupon-value">
不使用
</div>
<div class="coupon-info">
<div class="coupon-name">不使用优惠券</div>
<div class="coupon-condition">
直接结算
</div>
<div class="coupon-select" v-if="!selectedCoupon">
<CheckCircleOutlined />
</div>
</div>
</div>
</a-card>
<a-divider />
<!-- 优惠券列表 -->
<a-card
v-for="coupon in availableCoupons"
:key="coupon.id"
class="modal-coupon-card"
:class="{ active: selectedCoupon && selectedCoupon.id === coupon.id }"
@click="selectCoupon(coupon)"
@click="selectedCoupon = coupon"
>
<div class="coupon-content">
<div class="coupon-value">
@@ -193,7 +218,7 @@
</template>
<script setup lang="ts">
import { ref, computed, onMounted, watch } from 'vue'
import { ref, computed, onMounted, watch, inject } from 'vue'
import { useRouter } from 'vue-router'
import {
LeftOutlined,
@@ -206,6 +231,11 @@ import { cartApi } from '../api/cart'
import { orderApi } from '../api/order'
import { fileApi } from '../api/index'
import { couponApi, type UserCoupon } from '../api/coupon'
import { userApi } from '../api/user'
// 注入更新购物车数量的方法
const updateCartCount = inject<() => Promise<void>>('updateCartCount', async () => {})
interface CartItem {
id: number
@@ -387,6 +417,8 @@ const handleDecreaseQuantity = async (item: CartItem) => {
await cartApi.updateCartQuantity({ productId: item.productId, quantity: item.quantity })
// 重新加载可用优惠券
await loadAvailableCoupons()
// 更新购物车数量
await updateCartCount()
} catch (error) {
message.error('更新数量失败')
item.quantity++
@@ -402,6 +434,8 @@ const handleIncreaseQuantity = async (item: CartItem) => {
await cartApi.updateCartQuantity({ productId: item.productId, quantity: item.quantity })
// 重新加载可用优惠券
await loadAvailableCoupons()
// 更新购物车数量
await updateCartCount()
} catch (error) {
message.error('更新数量失败')
item.quantity--
@@ -419,6 +453,8 @@ const handleDeleteItem = async (item: CartItem) => {
message.success('商品已删除')
// 重新加载可用优惠券
await loadAvailableCoupons()
// 更新购物车数量
await updateCartCount()
}
} catch (error) {
message.error('删除商品失败')
@@ -430,9 +466,13 @@ const handleBrowseProducts = () => {
router.push('/products')
}
// 选择优惠券
const selectCoupon = (coupon: UserCoupon) => {
selectedCoupon.value = coupon
// 确认优惠券选择
const confirmCouponSelection = () => {
showCouponModal.value = false
}
// 取消优惠券选择
const cancelCouponSelection = () => {
showCouponModal.value = false
}
@@ -444,15 +484,15 @@ const availableArtists = ref<{id: number, name: string}[]>([])
// 加载可用美甲师
const loadAvailableArtists = async () => {
try {
// 这里需要根据商品ID获取可用的美甲师
// 暂时使用模拟数据
availableArtists.value = [
{ id: 1, name: '美甲师1' },
{ id: 2, name: '美甲师2' },
{ id: 3, name: '美甲师3' }
]
// 获取美甲师列表(用户类型为3)
const artists = await userApi.listUsersByType(3)
availableArtists.value = artists.map(artist => ({
id: artist.id,
name: artist.nickname
}))
} catch (error) {
console.error('获取美甲师列表失败:', error)
availableArtists.value = []
}
}
@@ -485,6 +525,8 @@ const selectArtistAndCheckout = async () => {
})
message.success('订单创建成功')
showArtistModal.value = false
// 更新购物车数量(因为订单创建后购物车会被清空)
await updateCartCount()
router.push('/profile')
} catch (error) {
message.error('创建订单失败')