2026-02-13 17:34:32 +08:00
|
|
|
<template>
|
|
|
|
|
<div class="mobile-container">
|
|
|
|
|
|
|
|
|
|
<!-- 头部区域 -->
|
|
|
|
|
<header class="header">
|
|
|
|
|
<a-button class="back-btn" type="text" @click="handleBackClick">
|
|
|
|
|
<LeftOutlined :style="{ fontSize: '20px' }" />
|
|
|
|
|
</a-button>
|
|
|
|
|
<h1>购物车</h1>
|
|
|
|
|
<a-button class="edit-btn" type="text" @click="handleEditClick">
|
|
|
|
|
编辑
|
|
|
|
|
</a-button>
|
|
|
|
|
</header>
|
|
|
|
|
|
|
|
|
|
<!-- 购物车商品列表 -->
|
|
|
|
|
<div class="cart-items">
|
|
|
|
|
<a-empty v-if="cartItems.length === 0" description="购物车是空的">
|
|
|
|
|
<a-button type="primary" @click="handleBrowseProducts">去逛逛</a-button>
|
|
|
|
|
</a-empty>
|
|
|
|
|
|
|
|
|
|
<a-card
|
|
|
|
|
v-for="item in cartItems"
|
|
|
|
|
:key="item.id"
|
|
|
|
|
class="cart-item"
|
|
|
|
|
:body-style="{ padding: '16px' }"
|
|
|
|
|
>
|
|
|
|
|
<div class="item-content">
|
|
|
|
|
<!-- 复选框 -->
|
|
|
|
|
<a-checkbox v-model:checked="item.selected" @change="handleSelectChange" />
|
|
|
|
|
|
|
|
|
|
<!-- 商品图片 -->
|
2026-02-27 15:57:14 +08:00
|
|
|
<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>
|
2026-02-13 17:34:32 +08:00
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<!-- 商品详情 -->
|
|
|
|
|
<div class="item-details">
|
2026-02-27 15:57:14 +08:00
|
|
|
<h3 class="item-name">{{ item.productTitle }}</h3>
|
2026-02-25 10:37:16 +08:00
|
|
|
<p class="item-price">¥{{ item.price.toFixed(2) }}</p>
|
2026-02-13 17:34:32 +08:00
|
|
|
|
|
|
|
|
<!-- 数量控制和删除按钮 -->
|
|
|
|
|
<div class="item-actions">
|
2026-02-25 10:37:16 +08:00
|
|
|
<div class="quantity-control">
|
|
|
|
|
<a-button
|
|
|
|
|
size="small"
|
|
|
|
|
:disabled="item.quantity <= 1"
|
|
|
|
|
@click="handleDecreaseQuantity(item)"
|
|
|
|
|
>
|
|
|
|
|
-
|
|
|
|
|
</a-button>
|
|
|
|
|
<span class="quantity-value">{{ item.quantity }}</span>
|
|
|
|
|
<a-button
|
|
|
|
|
size="small"
|
|
|
|
|
:disabled="item.quantity >= 99"
|
|
|
|
|
@click="handleIncreaseQuantity(item)"
|
|
|
|
|
>
|
|
|
|
|
+
|
|
|
|
|
</a-button>
|
|
|
|
|
</div>
|
2026-02-13 17:34:32 +08:00
|
|
|
<a-popconfirm
|
|
|
|
|
title="确定要删除这个商品吗?"
|
|
|
|
|
ok-text="确定"
|
|
|
|
|
cancel-text="取消"
|
|
|
|
|
@confirm="handleDeleteItem(item)"
|
|
|
|
|
>
|
|
|
|
|
<a-button type="text" danger size="small">
|
|
|
|
|
<DeleteOutlined />
|
|
|
|
|
</a-button>
|
|
|
|
|
</a-popconfirm>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</a-card>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<!-- 订单摘要 -->
|
|
|
|
|
<div class="summary" v-if="cartItems.length > 0">
|
|
|
|
|
<div class="summary-row">
|
|
|
|
|
<span>商品数量</span>
|
|
|
|
|
<span>{{ totalQuantity }} 件</span>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="summary-row">
|
|
|
|
|
<span>小计</span>
|
2026-02-25 10:37:16 +08:00
|
|
|
<span>¥{{ subtotal.toFixed(2) }}</span>
|
2026-02-13 17:34:32 +08:00
|
|
|
</div>
|
|
|
|
|
<div class="summary-row total">
|
|
|
|
|
<span>总计</span>
|
2026-02-25 10:37:16 +08:00
|
|
|
<span class="total-price">¥{{ subtotal.toFixed(2) }}</span>
|
2026-02-13 17:34:32 +08:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<!-- 结算按钮 -->
|
|
|
|
|
<div class="checkout-section" v-if="cartItems.length > 0">
|
|
|
|
|
<a-button
|
|
|
|
|
type="primary"
|
|
|
|
|
size="large"
|
|
|
|
|
block
|
|
|
|
|
@click="handleCheckout"
|
|
|
|
|
>
|
|
|
|
|
结算 ({{ selectedCount }})
|
|
|
|
|
</a-button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<script setup lang="ts">
|
2026-02-27 15:57:14 +08:00
|
|
|
import { ref, computed, onMounted } from 'vue'
|
2026-02-13 17:34:32 +08:00
|
|
|
import { useRouter } from 'vue-router'
|
|
|
|
|
import {
|
|
|
|
|
LeftOutlined,
|
|
|
|
|
DeleteOutlined
|
|
|
|
|
} from '@ant-design/icons-vue'
|
|
|
|
|
import { message } from 'ant-design-vue'
|
2026-02-27 15:57:14 +08:00
|
|
|
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
|
|
|
|
|
}
|
2026-02-13 17:34:32 +08:00
|
|
|
|
|
|
|
|
const router = useRouter()
|
|
|
|
|
|
2026-02-27 15:57:14 +08:00
|
|
|
// 购物车商品数据
|
|
|
|
|
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('获取购物车数据失败')
|
2026-02-25 10:37:16 +08:00
|
|
|
}
|
2026-02-27 15:57:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 加载商品图片
|
|
|
|
|
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 = ''
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-02-13 17:34:32 +08:00
|
|
|
|
|
|
|
|
// 计算选中的商品数量
|
|
|
|
|
const selectedCount = computed(() => {
|
|
|
|
|
return cartItems.value.filter(item => item.selected).length
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// 计算商品总数量
|
|
|
|
|
const totalQuantity = computed(() => {
|
|
|
|
|
return cartItems.value.reduce((sum, item) => sum + item.quantity, 0)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// 计算小计
|
|
|
|
|
const subtotal = computed(() => {
|
|
|
|
|
return cartItems.value
|
|
|
|
|
.filter(item => item.selected)
|
2026-02-27 15:57:14 +08:00
|
|
|
.reduce((sum, item) => sum + item.totalPrice, 0)
|
2026-02-13 17:34:32 +08:00
|
|
|
})
|
|
|
|
|
|
2026-02-27 15:57:14 +08:00
|
|
|
// 生成随机颜色
|
|
|
|
|
const getRandomColor = () => {
|
|
|
|
|
const colors = ['#ffe4d6', '#d6eaff', '#e2ffd6', '#fff2d6', '#d6ffe4', '#e4d6ff']
|
|
|
|
|
return colors[Math.floor(Math.random() * colors.length)]
|
|
|
|
|
}
|
2026-02-13 17:34:32 +08:00
|
|
|
|
|
|
|
|
// 处理返回按钮点击
|
|
|
|
|
const handleBackClick = () => {
|
|
|
|
|
router.back()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 处理编辑按钮点击
|
|
|
|
|
const handleEditClick = () => {
|
|
|
|
|
console.log('编辑按钮被点击')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 处理选择状态变化
|
|
|
|
|
const handleSelectChange = () => {
|
|
|
|
|
console.log('选择状态改变')
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-25 10:37:16 +08:00
|
|
|
// 处理减少数量
|
2026-02-27 15:57:14 +08:00
|
|
|
const handleDecreaseQuantity = async (item: CartItem) => {
|
2026-02-25 10:37:16 +08:00
|
|
|
if (item.quantity > 1) {
|
|
|
|
|
item.quantity--
|
2026-02-27 15:57:14 +08:00
|
|
|
try {
|
|
|
|
|
await cartApi.updateCartQuantity({ productId: item.productId, quantity: item.quantity })
|
|
|
|
|
} catch (error) {
|
|
|
|
|
message.error('更新数量失败')
|
|
|
|
|
item.quantity++
|
|
|
|
|
}
|
2026-02-25 10:37:16 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 处理增加数量
|
2026-02-27 15:57:14 +08:00
|
|
|
const handleIncreaseQuantity = async (item: CartItem) => {
|
2026-02-25 10:37:16 +08:00
|
|
|
if (item.quantity < 99) {
|
|
|
|
|
item.quantity++
|
2026-02-27 15:57:14 +08:00
|
|
|
try {
|
|
|
|
|
await cartApi.updateCartQuantity({ productId: item.productId, quantity: item.quantity })
|
|
|
|
|
} catch (error) {
|
|
|
|
|
message.error('更新数量失败')
|
|
|
|
|
item.quantity--
|
|
|
|
|
}
|
2026-02-25 10:37:16 +08:00
|
|
|
}
|
2026-02-13 17:34:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 处理删除商品
|
2026-02-27 15:57:14 +08:00
|
|
|
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('删除商品失败')
|
2026-02-13 17:34:32 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 处理浏览商品
|
|
|
|
|
const handleBrowseProducts = () => {
|
|
|
|
|
router.push('/products')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 处理结算
|
2026-02-27 15:57:14 +08:00
|
|
|
const handleCheckout = async () => {
|
2026-02-13 17:34:32 +08:00
|
|
|
const selectedItems = cartItems.value.filter(item => item.selected)
|
|
|
|
|
if (selectedItems.length === 0) {
|
|
|
|
|
message.warning('请先选择要结算的商品')
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-02-27 15:57:14 +08:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const order = await orderApi.createOrder({})
|
|
|
|
|
message.success('订单创建成功')
|
|
|
|
|
router.push('/profile')
|
|
|
|
|
} catch (error) {
|
|
|
|
|
message.error('创建订单失败')
|
|
|
|
|
}
|
2026-02-13 17:34:32 +08:00
|
|
|
}
|
2026-02-27 15:57:14 +08:00
|
|
|
|
|
|
|
|
// 页面加载时获取购物车数据
|
|
|
|
|
onMounted(() => {
|
|
|
|
|
loadCartItems()
|
|
|
|
|
})
|
2026-02-13 17:34:32 +08:00
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<style scoped>
|
|
|
|
|
.mobile-container {
|
|
|
|
|
padding-top: 60px;
|
|
|
|
|
min-height: 100vh;
|
|
|
|
|
background: #f5f5f5;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.header {
|
|
|
|
|
position: fixed;
|
|
|
|
|
top: 0;
|
|
|
|
|
left: 0;
|
|
|
|
|
right: 0;
|
|
|
|
|
z-index: 100;
|
|
|
|
|
display: flex;
|
|
|
|
|
justify-content: space-between;
|
|
|
|
|
align-items: center;
|
|
|
|
|
padding: 10px 15px;
|
|
|
|
|
border-bottom: 1px solid #f0f0f0;
|
|
|
|
|
background: white;
|
|
|
|
|
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.header h1 {
|
|
|
|
|
font-size: 18px;
|
|
|
|
|
font-weight: bold;
|
|
|
|
|
margin: 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.back-btn,
|
|
|
|
|
.edit-btn {
|
|
|
|
|
width: 40px;
|
|
|
|
|
height: 40px;
|
|
|
|
|
display: flex;
|
|
|
|
|
align-items: center;
|
|
|
|
|
justify-content: center;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.cart-items {
|
|
|
|
|
padding: 15px;
|
|
|
|
|
padding-bottom: 20px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.cart-item {
|
|
|
|
|
margin-bottom: 12px;
|
|
|
|
|
border-radius: 12px;
|
|
|
|
|
overflow: hidden;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.cart-item :deep(.ant-card-body) {
|
|
|
|
|
padding: 16px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.item-content {
|
|
|
|
|
display: flex;
|
|
|
|
|
gap: 12px;
|
|
|
|
|
align-items: flex-start;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.item-image {
|
|
|
|
|
width: 80px;
|
|
|
|
|
height: 80px;
|
|
|
|
|
border-radius: 8px;
|
|
|
|
|
display: flex;
|
|
|
|
|
justify-content: center;
|
|
|
|
|
align-items: center;
|
|
|
|
|
font-size: 30px;
|
|
|
|
|
font-weight: bold;
|
|
|
|
|
color: rgba(0,0,0,0.1);
|
|
|
|
|
flex-shrink: 0;
|
2026-02-27 15:57:14 +08:00
|
|
|
overflow: hidden;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.item-image img {
|
|
|
|
|
width: 100%;
|
|
|
|
|
height: 100%;
|
|
|
|
|
object-fit: cover;
|
2026-02-13 17:34:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.item-details {
|
|
|
|
|
flex: 1;
|
|
|
|
|
display: flex;
|
|
|
|
|
flex-direction: column;
|
|
|
|
|
min-height: 80px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.item-name {
|
|
|
|
|
font-size: 15px;
|
|
|
|
|
font-weight: 600;
|
|
|
|
|
margin: 0 0 4px 0;
|
|
|
|
|
line-height: 1.4;
|
2026-02-27 15:57:14 +08:00
|
|
|
overflow: hidden;
|
|
|
|
|
text-overflow: ellipsis;
|
|
|
|
|
display: -webkit-box;
|
|
|
|
|
-webkit-line-clamp: 2;
|
|
|
|
|
-webkit-box-orient: vertical;
|
2026-02-13 17:34:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.item-price {
|
|
|
|
|
color: #ff4d4f;
|
|
|
|
|
font-weight: bold;
|
|
|
|
|
margin: 0;
|
|
|
|
|
font-size: 16px;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-25 10:37:16 +08:00
|
|
|
.item-duration {
|
|
|
|
|
color: #666;
|
|
|
|
|
font-size: 13px;
|
|
|
|
|
margin: 4px 0 0 0;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-13 17:34:32 +08:00
|
|
|
.item-actions {
|
|
|
|
|
display: flex;
|
|
|
|
|
justify-content: space-between;
|
|
|
|
|
align-items: center;
|
|
|
|
|
margin-top: 8px;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-25 10:37:16 +08:00
|
|
|
.quantity-control {
|
|
|
|
|
display: flex;
|
|
|
|
|
align-items: center;
|
|
|
|
|
gap: 8px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.quantity-value {
|
|
|
|
|
min-width: 30px;
|
|
|
|
|
text-align: center;
|
|
|
|
|
font-weight: 600;
|
|
|
|
|
font-size: 16px;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-13 17:34:32 +08:00
|
|
|
.summary {
|
|
|
|
|
margin: 0 15px 15px;
|
|
|
|
|
padding: 16px;
|
|
|
|
|
background: #fafafa;
|
|
|
|
|
border-radius: 12px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.summary-row {
|
|
|
|
|
display: flex;
|
|
|
|
|
justify-content: space-between;
|
|
|
|
|
padding: 8px 0;
|
|
|
|
|
font-size: 14px;
|
|
|
|
|
color: #666;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.summary-row.total {
|
|
|
|
|
font-size: 16px;
|
|
|
|
|
font-weight: bold;
|
|
|
|
|
color: #333;
|
|
|
|
|
border-top: 1px solid #e8e8e8;
|
|
|
|
|
padding-top: 12px;
|
|
|
|
|
margin-top: 4px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.total-price {
|
|
|
|
|
color: #ff4d4f;
|
|
|
|
|
font-size: 20px;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-25 10:37:16 +08:00
|
|
|
.duration {
|
|
|
|
|
color: #667eea;
|
|
|
|
|
font-weight: 600;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-13 17:34:32 +08:00
|
|
|
.checkout-section {
|
|
|
|
|
position: fixed;
|
|
|
|
|
bottom: 60px;
|
|
|
|
|
left: 0;
|
|
|
|
|
right: 0;
|
|
|
|
|
padding: 15px;
|
|
|
|
|
background: white;
|
|
|
|
|
border-top: 1px solid #f0f0f0;
|
|
|
|
|
box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.06);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.checkout-section :deep(.ant-btn-lg) {
|
|
|
|
|
height: 48px;
|
|
|
|
|
border-radius: 24px;
|
|
|
|
|
font-weight: 600;
|
|
|
|
|
font-size: 16px;
|
|
|
|
|
}
|
|
|
|
|
</style>
|