功能:落地待优化建议与VIP资产联动
This commit is contained in:
+129
-32
@@ -56,10 +56,10 @@
|
||||
</div>
|
||||
</a-card>
|
||||
|
||||
<a-card v-if="selectedCount" class="coupon-card" @click="showCouponModal = true">
|
||||
<a-card v-if="selectedCount" class="coupon-card" @click="openCouponModal">
|
||||
<div class="coupon-row">
|
||||
<span>优惠券</span>
|
||||
<span>{{ selectedCoupon?.couponName || '使用优惠券' }}</span>
|
||||
<span>{{ selectedCoupon ? formatCouponDisplay(selectedCoupon) : '使用优惠券' }}</span>
|
||||
</div>
|
||||
<div v-if="selectedCoupon" class="coupon-discount">优惠金额 -¥{{ discountAmount.toFixed(2) }}</div>
|
||||
</a-card>
|
||||
@@ -79,12 +79,15 @@
|
||||
</footer>
|
||||
|
||||
<a-modal v-model:open="showCouponModal" title="使用优惠券" @ok="applyCouponSelection" @cancel="showCouponModal = false">
|
||||
<a-radio-group v-model:value="couponSelectionId" class="coupon-options">
|
||||
<a-radio :value="0">不使用优惠券</a-radio>
|
||||
<a-radio v-for="coupon in availableCoupons" :key="coupon.id" :value="coupon.id">
|
||||
{{ coupon.couponName }}
|
||||
<a-radio-group v-model:value="couponSelectionKey" class="coupon-options">
|
||||
<a-radio :value="NO_COUPON_KEY">不使用优惠券</a-radio>
|
||||
<a-radio v-for="coupon in availableCoupons" :key="getCouponKey(coupon)" :value="getCouponKey(coupon)">
|
||||
{{ formatCouponDisplay(coupon) }}
|
||||
</a-radio>
|
||||
</a-radio-group>
|
||||
<div class="coupon-tip">
|
||||
系统只会默认预选 VIP 自动券,普通用户券需要你手动选择后才会在下单时使用。
|
||||
</div>
|
||||
</a-modal>
|
||||
|
||||
<a-modal v-model:open="showBookingModal" :title="bookingTitle" @ok="confirmBooking" @cancel="closeBookingModal">
|
||||
@@ -131,6 +134,8 @@ import { userApi } from '../api/user'
|
||||
const router = useRouter()
|
||||
const updateCartCount = inject<() => Promise<void>>('updateCartCount', async () => {})
|
||||
|
||||
const NO_COUPON_KEY = 'NONE'
|
||||
|
||||
interface ArtistOption {
|
||||
id: number
|
||||
name: string
|
||||
@@ -161,7 +166,8 @@ const cartItems = ref<CartItem[]>([])
|
||||
const imageIdCache = ref<Record<string, string>>({})
|
||||
const availableCoupons = ref<UserCoupon[]>([])
|
||||
const selectedCoupon = ref<UserCoupon | null>(null)
|
||||
const couponSelectionId = ref(0)
|
||||
const couponSelectionKey = ref(NO_COUPON_KEY)
|
||||
const couponSelectionTouched = ref(false)
|
||||
const showCouponModal = ref(false)
|
||||
const showBookingModal = ref(false)
|
||||
const currentItem = ref<CartItem | null>(null)
|
||||
@@ -170,15 +176,11 @@ const draftArtistId = ref<number>()
|
||||
const draftAppointmentTime = ref('')
|
||||
const currentShopHours = ref('')
|
||||
|
||||
const selectedCount = computed(() => cartItems.value.filter(item => item.selected).length)
|
||||
const totalQuantity = computed(() => cartItems.value.filter(item => item.selected).reduce((sum, item) => sum + item.quantity, 0))
|
||||
const subtotal = computed(() => cartItems.value.filter(item => item.selected).reduce((sum, item) => sum + item.price * item.quantity, 0))
|
||||
const discountAmount = computed(() => {
|
||||
if (!selectedCoupon.value) return 0
|
||||
if (selectedCoupon.value.couponType === 1) return subtotal.value - subtotal.value * parseFloat(selectedCoupon.value.discountValue) / 10
|
||||
if (selectedCoupon.value.couponType === 2) return parseFloat(selectedCoupon.value.reduceAmount)
|
||||
return 0
|
||||
})
|
||||
const selectedItems = computed(() => cartItems.value.filter(item => item.selected))
|
||||
const selectedCount = computed(() => selectedItems.value.length)
|
||||
const totalQuantity = computed(() => selectedItems.value.reduce((sum, item) => sum + item.quantity, 0))
|
||||
const subtotal = computed(() => selectedItems.value.reduce((sum, item) => sum + item.price * item.quantity, 0))
|
||||
const discountAmount = computed(() => calculateCouponDiscount(selectedCoupon.value, subtotal.value))
|
||||
const totalAmount = computed(() => Math.max(0, subtotal.value - discountAmount.value))
|
||||
const bookingTitle = computed(() => currentItem.value ? '为 ' + currentItem.value.productTitle + ' 选择预约信息' : '选择预约信息')
|
||||
const bookingHelperText = computed(() => {
|
||||
@@ -204,6 +206,39 @@ const minAppointmentTime = computed(() => {
|
||||
].join('')
|
||||
})
|
||||
|
||||
const getCouponKey = (coupon: UserCoupon) => `${coupon.sourceType || 'USER_COUPON'}:${coupon.id}`
|
||||
|
||||
const calculateCouponDiscount = (coupon: UserCoupon | null, amount: number) => {
|
||||
if (!coupon || amount <= 0) {
|
||||
return 0
|
||||
}
|
||||
if (coupon.couponType === 1) {
|
||||
const discountValue = Number(coupon.discountValue || 0)
|
||||
if (!discountValue) {
|
||||
return 0
|
||||
}
|
||||
return amount - amount * discountValue / 10
|
||||
}
|
||||
if (coupon.couponType === 2) {
|
||||
return Number(coupon.reduceAmount || 0)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
const formatCouponDisplay = (coupon: UserCoupon) => {
|
||||
const badge = coupon.sourceType === 'SYSTEM_VIP' ? '[VIP 自动券]' : '[用户券]'
|
||||
const benefit = coupon.couponType === 1
|
||||
? `${coupon.discountValue}折`
|
||||
: `满${coupon.minAmount}减${coupon.reduceAmount}`
|
||||
return `${badge} ${coupon.couponName} ${benefit}`
|
||||
}
|
||||
|
||||
const findBestSystemVipCoupon = (coupons: UserCoupon[]) => {
|
||||
return coupons
|
||||
.filter(coupon => coupon.sourceType === 'SYSTEM_VIP')
|
||||
.sort((left, right) => calculateCouponDiscount(right, subtotal.value) - calculateCouponDiscount(left, subtotal.value))[0] || null
|
||||
}
|
||||
|
||||
const openProduct = (item: CartItem) => {
|
||||
router.push('/product/' + item.productId)
|
||||
}
|
||||
@@ -217,6 +252,9 @@ const loadCartItems = async () => {
|
||||
productImage: '',
|
||||
appointmentTime: item.appointmentTime
|
||||
}))
|
||||
couponSelectionTouched.value = false
|
||||
couponSelectionKey.value = NO_COUPON_KEY
|
||||
selectedCoupon.value = null
|
||||
await Promise.all([loadProductImages(), loadAvailableCoupons()])
|
||||
}
|
||||
|
||||
@@ -227,7 +265,9 @@ const loadProductImages = async () => {
|
||||
if (!match?.[1]) continue
|
||||
const imageId = match[1]
|
||||
try {
|
||||
if (!imageIdCache.value[imageId]) imageIdCache.value[imageId] = await fileApi.getSignedUrl(Number(imageId)) as any
|
||||
if (!imageIdCache.value[imageId]) {
|
||||
imageIdCache.value[imageId] = await fileApi.getSignedUrl(Number(imageId)) as any
|
||||
}
|
||||
item.productImage = imageIdCache.value[imageId] || ''
|
||||
} catch {
|
||||
item.productImage = ''
|
||||
@@ -236,17 +276,56 @@ const loadProductImages = async () => {
|
||||
}
|
||||
|
||||
const loadAvailableCoupons = async () => {
|
||||
try {
|
||||
const tags = new Set<string>()
|
||||
cartItems.value.filter(item => item.selected).forEach(item => item.tagNames?.split(',').map(tag => tag.trim()).forEach(tag => tag && tags.add(tag)))
|
||||
const data = subtotal.value > 0 ? await couponApi.getAvailableCoupons(Array.from(tags).join(','), subtotal.value) as unknown as any[] : []
|
||||
availableCoupons.value = data || []
|
||||
if (selectedCoupon.value && !availableCoupons.value.some(coupon => coupon.id === selectedCoupon.value?.id)) selectedCoupon.value = null
|
||||
couponSelectionId.value = selectedCoupon.value?.id || 0
|
||||
} catch {
|
||||
const currentSelectedKey = selectedCoupon.value ? getCouponKey(selectedCoupon.value) : couponSelectionKey.value
|
||||
const selectedShopId = selectedItems.value[0]?.shopId
|
||||
|
||||
if (!selectedItems.value.length || subtotal.value <= 0) {
|
||||
availableCoupons.value = []
|
||||
selectedCoupon.value = null
|
||||
couponSelectionId.value = 0
|
||||
couponSelectionKey.value = NO_COUPON_KEY
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
const tags = new Set<string>()
|
||||
selectedItems.value.forEach(item => {
|
||||
item.tagNames?.split(',').map(tag => tag.trim()).forEach(tag => {
|
||||
if (tag) {
|
||||
tags.add(tag)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
const data = await couponApi.getAvailableCoupons(Array.from(tags).join(','), subtotal.value, selectedShopId) as unknown as UserCoupon[]
|
||||
availableCoupons.value = Array.isArray(data) ? data : []
|
||||
|
||||
const matchedCoupon = availableCoupons.value.find(coupon => getCouponKey(coupon) === currentSelectedKey) || null
|
||||
if (matchedCoupon) {
|
||||
selectedCoupon.value = matchedCoupon
|
||||
couponSelectionKey.value = getCouponKey(matchedCoupon)
|
||||
return
|
||||
}
|
||||
|
||||
if (couponSelectionTouched.value && currentSelectedKey === NO_COUPON_KEY) {
|
||||
selectedCoupon.value = null
|
||||
couponSelectionKey.value = NO_COUPON_KEY
|
||||
return
|
||||
}
|
||||
|
||||
couponSelectionTouched.value = false
|
||||
const defaultVipCoupon = findBestSystemVipCoupon(availableCoupons.value)
|
||||
if (defaultVipCoupon) {
|
||||
selectedCoupon.value = defaultVipCoupon
|
||||
couponSelectionKey.value = getCouponKey(defaultVipCoupon)
|
||||
} else {
|
||||
selectedCoupon.value = null
|
||||
couponSelectionKey.value = NO_COUPON_KEY
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取可用优惠券失败:', error)
|
||||
availableCoupons.value = []
|
||||
selectedCoupon.value = null
|
||||
couponSelectionKey.value = NO_COUPON_KEY
|
||||
}
|
||||
}
|
||||
|
||||
@@ -334,13 +413,23 @@ const confirmBooking = () => {
|
||||
closeBookingModal()
|
||||
}
|
||||
|
||||
const openCouponModal = () => {
|
||||
couponSelectionKey.value = selectedCoupon.value ? getCouponKey(selectedCoupon.value) : NO_COUPON_KEY
|
||||
showCouponModal.value = true
|
||||
}
|
||||
|
||||
const applyCouponSelection = () => {
|
||||
selectedCoupon.value = availableCoupons.value.find(coupon => coupon.id === couponSelectionId.value) || null
|
||||
couponSelectionTouched.value = true
|
||||
if (couponSelectionKey.value === NO_COUPON_KEY) {
|
||||
selectedCoupon.value = null
|
||||
} else {
|
||||
selectedCoupon.value = availableCoupons.value.find(coupon => getCouponKey(coupon) === couponSelectionKey.value) || null
|
||||
}
|
||||
showCouponModal.value = false
|
||||
}
|
||||
|
||||
const checkout = async () => {
|
||||
const items = cartItems.value.filter(item => item.selected)
|
||||
const items = selectedItems.value
|
||||
if (!items.length) return message.warning('请先选择要结算的作品')
|
||||
if (items.some(item => !item.artistId)) return message.warning('请先为所有项目选择美甲师')
|
||||
if (items.some(item => !item.appointmentTime)) return message.warning('请先为所有项目选择预约时间')
|
||||
@@ -351,6 +440,7 @@ const checkout = async () => {
|
||||
try {
|
||||
await orderApi.createOrder({
|
||||
couponId: selectedCoupon.value?.id,
|
||||
couponSourceType: selectedCoupon.value?.sourceType || (couponSelectionTouched.value && !selectedCoupon.value ? NO_COUPON_KEY : undefined),
|
||||
items: items.map(item => ({
|
||||
productId: item.productId,
|
||||
quantity: item.quantity,
|
||||
@@ -361,12 +451,18 @@ const checkout = async () => {
|
||||
await Promise.all([loadCartItems(), updateCartCount()])
|
||||
message.success('订单创建成功')
|
||||
router.push('/orders')
|
||||
} catch {
|
||||
message.error('创建订单失败')
|
||||
} catch (error: any) {
|
||||
console.error('创建订单失败:', error)
|
||||
message.error(error?.message || '创建订单失败')
|
||||
}
|
||||
}
|
||||
|
||||
watch(() => cartItems.value.map(item => String(item.id) + '-' + item.selected + '-' + item.quantity), loadAvailableCoupons)
|
||||
watch(
|
||||
() => cartItems.value.map(item => `${item.id}-${item.selected}-${item.quantity}`).join('|'),
|
||||
() => {
|
||||
void loadAvailableCoupons()
|
||||
}
|
||||
)
|
||||
|
||||
onMounted(async () => {
|
||||
try {
|
||||
@@ -395,7 +491,8 @@ onMounted(async () => {
|
||||
.selector .value { flex: 1; color: #1890ff; font-size: 13px; }
|
||||
.actions, .qty, .coupon-row, .summary-row { display: flex; align-items: center; justify-content: space-between; }
|
||||
.qty { gap: 8px; }
|
||||
.duration, .coupon-discount, .helper { color: #666; font-size: 12px; }
|
||||
.duration, .coupon-discount, .helper, .coupon-tip { color: #666; font-size: 12px; }
|
||||
.coupon-tip { margin-top: 12px; line-height: 1.6; }
|
||||
.summary-row.total { font-weight: 700; }
|
||||
.footer { bottom: 60px; padding: 15px; border-top: 1px solid #f0f0f0; box-shadow: 0 -2px 8px rgba(0,0,0,.06); }
|
||||
.coupon-options { display: flex; flex-direction: column; gap: 8px; }
|
||||
|
||||
Reference in New Issue
Block a user