购物车和订单中心以及优惠券相关功能初版

This commit is contained in:
xiaowang
2026-02-27 15:57:14 +08:00
parent 6adfd2f025
commit ddba11c08c
16 changed files with 890 additions and 128 deletions
+130 -96
View File
@@ -29,15 +29,17 @@
<a-checkbox v-model:checked="item.selected" @change="handleSelectChange" />
<!-- 商品图片 -->
<div class="item-image" :style="{ backgroundColor: item.bgColor }">
<span>{{ item.title[0] }}</span>
<div class="item-image" v-if="item.productImage">
<img :src="item.productImage" :alt="item.productTitle" />
</div>
<div class="item-image" v-else :style="{ backgroundColor: getRandomColor() }">
<span>{{ item.productTitle[0] }}</span>
</div>
<!-- 商品详情 -->
<div class="item-details">
<h3 class="item-name">{{ item.title }}</h3>
<h3 class="item-name">{{ item.productTitle }}</h3>
<p class="item-price">¥{{ item.price.toFixed(2) }}</p>
<p class="item-duration">预计耗时: {{ item.approximateDuration }}</p>
<!-- 数量控制和删除按钮 -->
<div class="item-actions">
@@ -84,10 +86,6 @@
<span>小计</span>
<span>¥{{ subtotal.toFixed(2) }}</span>
</div>
<div class="summary-row">
<span>预计消耗时间</span>
<span class="duration">{{ totalDuration }}</span>
</div>
<div class="summary-row total">
<span>总计</span>
<span class="total-price">¥{{ subtotal.toFixed(2) }}</span>
@@ -109,60 +107,84 @@
</template>
<script setup lang="ts">
import { ref, computed } from 'vue'
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import {
LeftOutlined,
DeleteOutlined
} from '@ant-design/icons-vue'
import { message } from 'ant-design-vue'
import type { ProductVO } from '@/api/product'
import { cartApi } from '../api/cart'
import { orderApi } from '../api/order'
import { fileApi } from '../api/index'
interface CartItem {
id: number
productId: number
productTitle: string
productImage: string
price: number
quantity: number
totalPrice: number
selected: boolean
}
const router = useRouter()
// 购物车商品数据(模拟真实产品数据结构)
const cartItems = ref<Array<{
id: number
productId: number
title: string
price: number
imageUrl?: string
bgColor: string
quantity: number
selected: boolean
approximateDuration: string
}>>([
{
id: 1,
productId: 1,
title: '经典美甲',
price: 99,
bgColor: '#ffe4d6',
quantity: 1,
selected: true,
approximateDuration: '30分钟'
},
{
id: 2,
productId: 2,
title: '法式美甲',
price: 168,
bgColor: '#d6eaff',
quantity: 2,
selected: true,
approximateDuration: '45分钟'
},
{
id: 3,
productId: 3,
title: '光疗美甲',
price: 238,
bgColor: '#e2ffd6',
quantity: 1,
selected: false,
approximateDuration: '60分钟'
// 购物车商品数据
const cartItems = ref<CartItem[]>([])
// 图片缓存
const imageIdCache = ref<Record<string, string>>({})
// 加载购物车数据
const loadCartItems = async () => {
try {
const data = await cartApi.getCartList()
cartItems.value = data.map((item: any) => ({
...item,
selected: true
}))
// 加载商品图片的临时加签链接
await loadProductImages(cartItems.value)
} catch (error) {
message.error('获取购物车数据失败')
}
])
}
// 加载商品图片
const loadProductImages = async (items: CartItem[]) => {
for (const item of items) {
if (item.productImage) {
// 过滤掉HTML标签
const cleanImageUrl = item.productImage.replace(/<[^>]*>/g, '')
// 从cleanImageUrl中提取图片ID
// 假设图片URL的格式为 '/api/file/get/123'
const match = cleanImageUrl.match(/\/api\/file\/get\/(\d+)/)
if (match && match[1]) {
const imageId = match[1]
try {
// 检查缓存
if (!imageIdCache.value[imageId]) {
const signedUrl = await fileApi.getSignedUrl(Number(imageId))
imageIdCache.value[imageId] = signedUrl
}
// 更新商品图片为临时加签链接
item.productImage = imageIdCache.value[imageId]
} catch (error) {
console.error('获取图片签名链接失败:', error)
// 如果获取签名链接失败,清除无效的图片URL
item.productImage = ''
}
} else {
// 如果不是有效的图片URL格式,清除无效的图片URL
item.productImage = ''
}
}
}
}
// 计算选中的商品数量
const selectedCount = computed(() => {
@@ -178,39 +200,17 @@ const totalQuantity = computed(() => {
const subtotal = computed(() => {
return cartItems.value
.filter(item => item.selected)
.reduce((sum, item) => sum + (item.price * item.quantity), 0)
.reduce((sum, item) => sum + item.totalPrice, 0)
})
// 计算预计总消耗时间
const totalDuration = computed(() => {
const selectedItems = cartItems.value.filter(item => item.selected)
if (selectedItems.length === 0) return '0分钟'
let totalMinutes = 0
selectedItems.forEach(item => {
const durationStr = item.approximateDuration
const match = durationStr.match(/(\d+)/)
if (match) {
totalMinutes += parseInt(match[1]) * item.quantity
}
})
if (totalMinutes < 60) {
return `${totalMinutes}分钟`
} else {
const hours = Math.floor(totalMinutes / 60)
const minutes = totalMinutes % 60
if (minutes === 0) {
return `${hours}小时`
} else {
return `${hours}小时${minutes}分钟`
}
}
})
// 生成随机颜色
const getRandomColor = () => {
const colors = ['#ffe4d6', '#d6eaff', '#e2ffd6', '#fff2d6', '#d6ffe4', '#e4d6ff']
return colors[Math.floor(Math.random() * colors.length)]
}
// 处理返回按钮点击
const handleBackClick = () => {
console.log('返回按钮被点击')
router.back()
}
@@ -225,26 +225,42 @@ const handleSelectChange = () => {
}
// 处理减少数量
const handleDecreaseQuantity = (item: any) => {
const handleDecreaseQuantity = async (item: CartItem) => {
if (item.quantity > 1) {
item.quantity--
try {
await cartApi.updateCartQuantity({ productId: item.productId, quantity: item.quantity })
} catch (error) {
message.error('更新数量失败')
item.quantity++
}
}
}
// 处理增加数量
const handleIncreaseQuantity = (item: any) => {
const handleIncreaseQuantity = async (item: CartItem) => {
if (item.quantity < 99) {
item.quantity++
try {
await cartApi.updateCartQuantity({ productId: item.productId, quantity: item.quantity })
} catch (error) {
message.error('更新数量失败')
item.quantity--
}
}
}
// 处理删除商品
const handleDeleteItem = (item: any) => {
console.log('删除商品:', item.name)
const index = cartItems.value.findIndex(i => i.id === item.id)
if (index > -1) {
cartItems.value.splice(index, 1)
message.success('商品已删除')
const handleDeleteItem = async (item: CartItem) => {
try {
await cartApi.removeFromCart(item.productId)
const index = cartItems.value.findIndex(i => i.id === item.id)
if (index > -1) {
cartItems.value.splice(index, 1)
message.success('商品已删除')
}
} catch (error) {
message.error('删除商品失败')
}
}
@@ -254,19 +270,26 @@ const handleBrowseProducts = () => {
}
// 处理结算
const handleCheckout = () => {
const handleCheckout = async () => {
const selectedItems = cartItems.value.filter(item => item.selected)
if (selectedItems.length === 0) {
message.warning('请先选择要结算的商品')
return
}
console.log('结算购物车:', {
items: selectedItems,
subtotal: subtotal.value,
totalDuration: totalDuration.value
})
message.success('正在跳转到结算页面...')
try {
const order = await orderApi.createOrder({})
message.success('订单创建成功')
router.push('/profile')
} catch (error) {
message.error('创建订单失败')
}
}
// 页面加载时获取购物车数据
onMounted(() => {
loadCartItems()
})
</script>
<style scoped>
@@ -338,13 +361,19 @@ const handleCheckout = () => {
font-weight: bold;
color: rgba(0,0,0,0.1);
flex-shrink: 0;
overflow: hidden;
}
.item-image img {
width: 100%;
height: 100%;
object-fit: cover;
}
.item-details {
flex: 1;
display: flex;
flex-direction: column;
justify-content: space-between;
min-height: 80px;
}
@@ -353,6 +382,11 @@ const handleCheckout = () => {
font-weight: 600;
margin: 0 0 4px 0;
line-height: 1.4;
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
}
.item-price {