Files
JayuCloud/Frontend/src/views/CartView.vue
T
2026-02-25 10:37:16 +08:00

442 lines
9.4 KiB
Vue

<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" />
<!-- 商品图片 -->
<div class="item-image" :style="{ backgroundColor: item.bgColor }">
<span>{{ item.title[0] }}</span>
</div>
<!-- 商品详情 -->
<div class="item-details">
<h3 class="item-name">{{ item.title }}</h3>
<p class="item-price">¥{{ item.price.toFixed(2) }}</p>
<p class="item-duration">预计耗时: {{ item.approximateDuration }}</p>
<!-- 数量控制和删除按钮 -->
<div class="item-actions">
<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>
<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>
<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>
</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">
import { ref, computed } 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'
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 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)
.reduce((sum, item) => sum + (item.price * item.quantity), 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 handleBackClick = () => {
console.log('返回按钮被点击')
router.back()
}
// 处理编辑按钮点击
const handleEditClick = () => {
console.log('编辑按钮被点击')
}
// 处理选择状态变化
const handleSelectChange = () => {
console.log('选择状态改变')
}
// 处理减少数量
const handleDecreaseQuantity = (item: any) => {
if (item.quantity > 1) {
item.quantity--
}
}
// 处理增加数量
const handleIncreaseQuantity = (item: any) => {
if (item.quantity < 99) {
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 handleBrowseProducts = () => {
router.push('/products')
}
// 处理结算
const handleCheckout = () => {
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('正在跳转到结算页面...')
}
</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;
}
.item-details {
flex: 1;
display: flex;
flex-direction: column;
justify-content: space-between;
min-height: 80px;
}
.item-name {
font-size: 15px;
font-weight: 600;
margin: 0 0 4px 0;
line-height: 1.4;
}
.item-price {
color: #ff4d4f;
font-weight: bold;
margin: 0;
font-size: 16px;
}
.item-duration {
color: #666;
font-size: 13px;
margin: 4px 0 0 0;
}
.item-actions {
display: flex;
justify-content: space-between;
align-items: center;
margin-top: 8px;
}
.quantity-control {
display: flex;
align-items: center;
gap: 8px;
}
.quantity-value {
min-width: 30px;
text-align: center;
font-weight: 600;
font-size: 16px;
}
.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;
}
.duration {
color: #667eea;
font-weight: 600;
}
.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>