Files
JayuCloud/Frontend/src/views/CartView.vue
T

342 lines
7.4 KiB
Vue
Raw Normal View History

<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.name[0] }}</span>
</div>
<!-- 商品详情 -->
<div class="item-details">
<h3 class="item-name">{{ item.name }}</h3>
<p class="item-price">${{ item.price }}</p>
<!-- 数量控制和删除按钮 -->
<div class="item-actions">
<a-input-number
v-model:value="item.quantity"
:min="1"
:max="99"
size="small"
2026-02-24 09:59:22 +08:00
@change="(val: number | null) => handleQuantityChange(item, val)"
/>
<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 }}</span>
</div>
<div class="summary-row">
<span>运费</span>
<span>${{ shipping }}</span>
</div>
<div class="summary-row total">
<span>总计</span>
<span class="total-price">${{ total }}</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'
const router = useRouter()
// 购物车商品数据
const cartItems = ref([
{ id: 1, name: 'Watch', price: 40, bgColor: '#ffe4d6', quantity: 1, selected: true },
{ id: 2, name: 'Nike Shoes', price: 430, bgColor: '#d6eaff', quantity: 2, selected: true },
{ id: 3, name: 'Airpods', price: 333, bgColor: '#e2ffd6', quantity: 1, selected: false }
])
// 运费
const shipping = 10
// 计算选中的商品数量
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 total = computed(() => {
return subtotal.value + shipping
})
// 处理返回按钮点击
const handleBackClick = () => {
console.log('返回按钮被点击')
router.back()
}
// 处理编辑按钮点击
const handleEditClick = () => {
console.log('编辑按钮被点击')
}
// 处理选择状态变化
const handleSelectChange = () => {
console.log('选择状态改变')
}
// 处理数量变化
const handleQuantityChange = (item: any, val: number | null) => {
console.log('商品数量改变:', item.name, val)
}
// 处理删除商品
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,
shipping: shipping,
total: total.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-actions {
display: flex;
justify-content: space-between;
align-items: center;
margin-top: 8px;
}
.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;
}
.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>