修复一些问题以及优惠券管理功能
This commit is contained in:
@@ -76,6 +76,23 @@
|
||||
</a-card>
|
||||
</div>
|
||||
|
||||
<!-- 优惠券选择 -->
|
||||
<div class="coupon-section" v-if="cartItems.length > 0 && selectedCount > 0">
|
||||
<a-card class="coupon-card">
|
||||
<div class="coupon-header" @click="showCouponModal = true">
|
||||
<span class="coupon-title">优惠券</span>
|
||||
<div class="coupon-value">
|
||||
{{ selectedCoupon ? '已选择: ' + selectedCoupon.couponName : '请选择' }}
|
||||
<RightOutlined :style="{ marginLeft: '4px' }" />
|
||||
</div>
|
||||
</div>
|
||||
<div class="coupon-discount" v-if="selectedCoupon">
|
||||
<span class="discount-label">优惠金额</span>
|
||||
<span class="discount-amount">-¥{{ discountAmount.toFixed(2) }}</span>
|
||||
</div>
|
||||
</a-card>
|
||||
</div>
|
||||
|
||||
<!-- 订单摘要 -->
|
||||
<div class="summary" v-if="cartItems.length > 0">
|
||||
<div class="summary-row">
|
||||
@@ -86,9 +103,13 @@
|
||||
<span>小计</span>
|
||||
<span>¥{{ subtotal.toFixed(2) }}</span>
|
||||
</div>
|
||||
<div class="summary-row" v-if="selectedCoupon">
|
||||
<span>优惠</span>
|
||||
<span class="discount">-¥{{ discountAmount.toFixed(2) }}</span>
|
||||
</div>
|
||||
<div class="summary-row total">
|
||||
<span>总计</span>
|
||||
<span class="total-price">¥{{ subtotal.toFixed(2) }}</span>
|
||||
<span class="total-price">¥{{ totalAmount.toFixed(2) }}</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -100,23 +121,64 @@
|
||||
block
|
||||
@click="handleCheckout"
|
||||
>
|
||||
结算 ({{ selectedCount }})
|
||||
结算 ({{ selectedCount }}) - ¥{{ totalAmount.toFixed(2) }}
|
||||
</a-button>
|
||||
</div>
|
||||
|
||||
<!-- 优惠券选择弹窗 -->
|
||||
<a-modal
|
||||
v-model:visible="showCouponModal"
|
||||
title="选择优惠券"
|
||||
width="90%"
|
||||
>
|
||||
<div class="coupon-modal-content">
|
||||
<a-empty v-if="availableCoupons.length === 0" description="暂无可用优惠券" />
|
||||
<div v-else class="coupon-list">
|
||||
<a-card
|
||||
v-for="coupon in availableCoupons"
|
||||
:key="coupon.id"
|
||||
class="modal-coupon-card"
|
||||
:class="{ active: selectedCoupon && selectedCoupon.id === coupon.id }"
|
||||
@click="selectCoupon(coupon)"
|
||||
>
|
||||
<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>
|
||||
<div class="coupon-select" v-if="selectedCoupon && selectedCoupon.id === coupon.id">
|
||||
<CheckCircleOutlined />
|
||||
</div>
|
||||
</div>
|
||||
</a-card>
|
||||
</div>
|
||||
</div>
|
||||
</a-modal>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref, computed, onMounted } from 'vue'
|
||||
import { ref, computed, onMounted, watch } from 'vue'
|
||||
import { useRouter } from 'vue-router'
|
||||
import {
|
||||
LeftOutlined,
|
||||
DeleteOutlined
|
||||
DeleteOutlined,
|
||||
RightOutlined,
|
||||
CheckCircleOutlined
|
||||
} from '@ant-design/icons-vue'
|
||||
import { message } from 'ant-design-vue'
|
||||
import { cartApi } from '../api/cart'
|
||||
import { orderApi } from '../api/order'
|
||||
import { fileApi } from '../api/index'
|
||||
import { couponApi, type UserCoupon } from '../api/coupon'
|
||||
|
||||
interface CartItem {
|
||||
id: number
|
||||
@@ -137,6 +199,11 @@ const cartItems = ref<CartItem[]>([])
|
||||
// 图片缓存
|
||||
const imageIdCache = ref<Record<string, string>>({})
|
||||
|
||||
// 优惠券相关状态
|
||||
const availableCoupons = ref<UserCoupon[]>([])
|
||||
const selectedCoupon = ref<UserCoupon | null>(null)
|
||||
const showCouponModal = ref(false)
|
||||
|
||||
// 加载购物车数据
|
||||
const loadCartItems = async () => {
|
||||
try {
|
||||
@@ -148,6 +215,9 @@ const loadCartItems = async () => {
|
||||
|
||||
// 加载商品图片的临时加签链接
|
||||
await loadProductImages(cartItems.value)
|
||||
|
||||
// 加载可用优惠券
|
||||
await loadAvailableCoupons()
|
||||
} catch (error) {
|
||||
message.error('获取购物车数据失败')
|
||||
}
|
||||
@@ -186,6 +256,27 @@ const loadProductImages = async (items: CartItem[]) => {
|
||||
}
|
||||
}
|
||||
|
||||
// 加载可用优惠券
|
||||
const loadAvailableCoupons = async () => {
|
||||
try {
|
||||
// 计算商品总价
|
||||
const totalPrice = subtotal.value
|
||||
|
||||
// 收集商品标签
|
||||
const productTags = cartItems.value
|
||||
.filter(item => item.selected)
|
||||
.map(item => item.productTitle.split(' ')[0]) // 简单处理,取商品标题的第一个词作为标签
|
||||
.join(',')
|
||||
|
||||
if (totalPrice > 0) {
|
||||
const coupons = await couponApi.getAvailableCoupons(productTags, totalPrice)
|
||||
availableCoupons.value = coupons
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取可用优惠券失败:', error)
|
||||
}
|
||||
}
|
||||
|
||||
// 计算选中的商品数量
|
||||
const selectedCount = computed(() => {
|
||||
return cartItems.value.filter(item => item.selected).length
|
||||
@@ -203,6 +294,29 @@ const subtotal = computed(() => {
|
||||
.reduce((sum, item) => sum + item.totalPrice, 0)
|
||||
})
|
||||
|
||||
// 计算优惠金额
|
||||
const discountAmount = computed(() => {
|
||||
if (!selectedCoupon.value) return 0
|
||||
|
||||
const total = subtotal.value
|
||||
const coupon = selectedCoupon.value
|
||||
|
||||
if (coupon.couponType === 1) {
|
||||
// 折扣券
|
||||
return total - (total * parseFloat(coupon.discountValue) / 10)
|
||||
} else if (coupon.couponType === 2) {
|
||||
// 满减券
|
||||
return parseFloat(coupon.reduceAmount)
|
||||
}
|
||||
|
||||
return 0
|
||||
})
|
||||
|
||||
// 计算总金额
|
||||
const totalAmount = computed(() => {
|
||||
return Math.max(0, subtotal.value - discountAmount.value)
|
||||
})
|
||||
|
||||
// 生成随机颜色
|
||||
const getRandomColor = () => {
|
||||
const colors = ['#ffe4d6', '#d6eaff', '#e2ffd6', '#fff2d6', '#d6ffe4', '#e4d6ff']
|
||||
@@ -220,8 +334,10 @@ const handleEditClick = () => {
|
||||
}
|
||||
|
||||
// 处理选择状态变化
|
||||
const handleSelectChange = () => {
|
||||
const handleSelectChange = async () => {
|
||||
console.log('选择状态改变')
|
||||
// 重新加载可用优惠券
|
||||
await loadAvailableCoupons()
|
||||
}
|
||||
|
||||
// 处理减少数量
|
||||
@@ -230,6 +346,8 @@ const handleDecreaseQuantity = async (item: CartItem) => {
|
||||
item.quantity--
|
||||
try {
|
||||
await cartApi.updateCartQuantity({ productId: item.productId, quantity: item.quantity })
|
||||
// 重新加载可用优惠券
|
||||
await loadAvailableCoupons()
|
||||
} catch (error) {
|
||||
message.error('更新数量失败')
|
||||
item.quantity++
|
||||
@@ -243,6 +361,8 @@ const handleIncreaseQuantity = async (item: CartItem) => {
|
||||
item.quantity++
|
||||
try {
|
||||
await cartApi.updateCartQuantity({ productId: item.productId, quantity: item.quantity })
|
||||
// 重新加载可用优惠券
|
||||
await loadAvailableCoupons()
|
||||
} catch (error) {
|
||||
message.error('更新数量失败')
|
||||
item.quantity--
|
||||
@@ -258,6 +378,8 @@ const handleDeleteItem = async (item: CartItem) => {
|
||||
if (index > -1) {
|
||||
cartItems.value.splice(index, 1)
|
||||
message.success('商品已删除')
|
||||
// 重新加载可用优惠券
|
||||
await loadAvailableCoupons()
|
||||
}
|
||||
} catch (error) {
|
||||
message.error('删除商品失败')
|
||||
@@ -269,6 +391,12 @@ const handleBrowseProducts = () => {
|
||||
router.push('/products')
|
||||
}
|
||||
|
||||
// 选择优惠券
|
||||
const selectCoupon = (coupon: UserCoupon) => {
|
||||
selectedCoupon.value = coupon
|
||||
showCouponModal.value = false
|
||||
}
|
||||
|
||||
// 处理结算
|
||||
const handleCheckout = async () => {
|
||||
const selectedItems = cartItems.value.filter(item => item.selected)
|
||||
@@ -278,7 +406,9 @@ const handleCheckout = async () => {
|
||||
}
|
||||
|
||||
try {
|
||||
const order = await orderApi.createOrder({})
|
||||
const order = await orderApi.createOrder({
|
||||
couponId: selectedCoupon.value ? selectedCoupon.value.id : undefined
|
||||
})
|
||||
message.success('订单创建成功')
|
||||
router.push('/profile')
|
||||
} catch (error) {
|
||||
@@ -286,6 +416,15 @@ const handleCheckout = async () => {
|
||||
}
|
||||
}
|
||||
|
||||
// 监听选中商品变化,重新加载可用优惠券
|
||||
watch(
|
||||
() => cartItems.value.map(item => ({ id: item.id, selected: item.selected, quantity: item.quantity })),
|
||||
async () => {
|
||||
await loadAvailableCoupons()
|
||||
},
|
||||
{ deep: true }
|
||||
)
|
||||
|
||||
// 页面加载时获取购物车数据
|
||||
onMounted(() => {
|
||||
loadCartItems()
|
||||
@@ -473,4 +612,137 @@ onMounted(() => {
|
||||
font-weight: 600;
|
||||
font-size: 16px;
|
||||
}
|
||||
|
||||
/* 优惠券样式 */
|
||||
.coupon-section {
|
||||
margin: 0 15px 15px;
|
||||
}
|
||||
|
||||
.coupon-card {
|
||||
border-radius: 12px;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.coupon-header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
padding: 16px;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.coupon-header:hover {
|
||||
background-color: #f5f5f5;
|
||||
}
|
||||
|
||||
.coupon-title {
|
||||
font-size: 15px;
|
||||
font-weight: 600;
|
||||
color: #333;
|
||||
}
|
||||
|
||||
.coupon-value {
|
||||
font-size: 14px;
|
||||
color: #1890ff;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 4px;
|
||||
}
|
||||
|
||||
.coupon-discount {
|
||||
padding: 12px 16px;
|
||||
background-color: #f0f9ff;
|
||||
border-top: 1px solid #e6f7ff;
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.discount-label {
|
||||
font-size: 14px;
|
||||
color: #333;
|
||||
}
|
||||
|
||||
.discount-amount {
|
||||
font-size: 16px;
|
||||
font-weight: 600;
|
||||
color: #1890ff;
|
||||
}
|
||||
|
||||
.discount {
|
||||
color: #1890ff;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
/* 优惠券弹窗样式 */
|
||||
.coupon-modal-content {
|
||||
max-height: 400px;
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
||||
.coupon-list {
|
||||
padding: 8px 0;
|
||||
}
|
||||
|
||||
.modal-coupon-card {
|
||||
margin-bottom: 12px;
|
||||
border-radius: 12px;
|
||||
overflow: hidden;
|
||||
cursor: pointer;
|
||||
transition: all 0.3s;
|
||||
}
|
||||
|
||||
.modal-coupon-card:hover {
|
||||
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
|
||||
.modal-coupon-card.active {
|
||||
border: 2px solid #1890ff;
|
||||
}
|
||||
|
||||
.coupon-content {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
padding: 16px;
|
||||
gap: 16px;
|
||||
}
|
||||
|
||||
.coupon-value {
|
||||
font-size: 24px;
|
||||
font-weight: 600;
|
||||
color: #ff4d4f;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.coupon-info {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.coupon-name {
|
||||
font-size: 15px;
|
||||
font-weight: 600;
|
||||
color: #333;
|
||||
margin-bottom: 4px;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.coupon-condition {
|
||||
font-size: 13px;
|
||||
color: #666;
|
||||
margin-bottom: 4px;
|
||||
}
|
||||
|
||||
.coupon-time {
|
||||
font-size: 12px;
|
||||
color: #999;
|
||||
}
|
||||
|
||||
.coupon-select {
|
||||
color: #1890ff;
|
||||
font-size: 20px;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user