shop-web/src/pages/product/components/cart.vue

256 lines
5.5 KiB
Vue

<script setup lang="ts">
import { useCartStore } from "@/pinia/stores/cart"
import { Product } from "@/pinia/stores/product"
import { storeToRefs } from "pinia"
import { showConfirmDialog } from "vant"
import { useRouter } from "vue-router"
// 新增购物车状态
// 定义组件事件:关闭详情
const emit = defineEmits(["cartClose"])
const router = useRouter()
// 状态管理
const cartStore = useCartStore()
const { cartItems, totalPrice, totalQuantity } = storeToRefs(cartStore)// 处理关闭按钮点击事件
function handleClose() {
emit("cartClose")
}
function handleAddToCart(product: Product) {
cartStore.addToCart(product)
}
function handleRemoveFromCart(product: Product) {
cartStore.removeFromCart(product.cellId)
}
function handleClearCart() {
showConfirmDialog({
title: "确认清空购物车吗?"
})
.then(() => {
cartStore.clearCart()
})
.catch(() => {
// on cancel
})
}
// 结算方法
function handleCheckout() {
handleClose()
router.push("/product/checkout")
}
</script>
<template>
<div class="cart-container">
<!-- 修改后的header结构 -->
<div class="flex-header">
<div class="clear-header">
<van-button
size="small" type="primary" class="clear-btn" plain
@click="handleClearCart"
>
清空购物车
</van-button>
</div>
</div>
<div class="product-list">
<van-cell v-for="item in cartItems" :key="item.product.id" class="product-item">
<template #icon>
<van-image :src="item.product.image" width="80" height="80" class="product-image">
<!-- 真正的图片错误处理 -->
<template #error>
<div class="custom-error">
图片加载失败
</div>
</template>
</van-image>
</template>
<div class="product-info">
<div class="product-name van-ellipsis">
{{ item.product.name }}
</div>
<div class="product-price">
¥{{ item.product.price.toFixed(2) }}
</div>
<div class="action-row">
<span v-if="item.product.stock > 0" class="stock-count">
还剩{{ item.product.stock }}份
</span>
<div class="cart-actions">
<van-button size="mini" icon="minus" @click.stop="handleRemoveFromCart(item.product)" />
<span class="cart-count">{{ item.quantity }}</span>
<van-button
size="mini" type="primary" class="add-cart-btn" icon="plus"
@click.stop="handleAddToCart(item.product)"
/>
</div>
</div>
</div>
</van-cell>
<!-- 底部购物车栏 -->
<div v-if="cartItems.length" class="shopping-cart-bar">
<div class="cart-info">
<van-badge :content="totalQuantity" @click.stop="handleClose()">
<van-icon name="shopping-cart-o" size="24" />
</van-badge>
<div class="total-price">
合计:¥{{ totalPrice.toFixed(2) }}
</div>
</div>
<van-button type="primary" size="small" @click="handleCheckout" class="checkout-btn">
去结算
</van-button>
</div>
</div>
</div>
</template>
<style scoped>
.product-list {
flex: 1;
overflow-y: auto;
padding: 10px 16px 60px;
background: #ffffff;
}
.product-item {
margin-bottom: 10px;
padding: min(2.667vw, 20px) 0;
/* border-radius: 4px; */
/* box-shadow: 0 2px 4px rgba(0,0,0,0.05); */
}
.product-image {
margin-right: 12px;
border-radius: 4px;
overflow: hidden;
}
.product-info {
display: flex;
flex-direction: column;
justify-content: space-between;
height: 80px;
position: relative;
}
.product-name {
font-size: 14px;
color: #333;
line-height: 1.4;
text-align: left;
}
.product-price {
font-size: 16px;
color: #e95d5d;
font-weight: bold;
text-align: left;
}
.add-cart-btn {
align-self: flex-end;
margin-top: 0;
background-color: #e95d5d;
border-color: #e95d5d;
}
.action-row {
display: flex;
justify-content: space-between;
align-items: flex-end;
/* 底部对齐 */
margin-top: auto;
/* 推到容器底部 */
}
.stock-count {
font-size: 11px;
color: #bbbbbb;
margin-right: 8px;
display: flex;
align-items: flex-end;
height: 100%;
line-height: 1;
}
/* 修改购物车栏样式 */
.shopping-cart-bar {
position: absolute;
bottom: 0;
left: 0;
right: 0;
height: 50px;
background: #fff;
display: flex;
align-items: center;
justify-content: space-between;
padding: 0 16px;
box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.05);
z-index: 100;
}
.cart-info {
display: flex;
align-items: center;
gap: 12px;
}
.total-price {
font-size: 14px;
color: #333;
font-weight: bold;
}
.checkout-btn {
background-color: #e95d5d;
border: none;
border-radius: 16px;
padding: 0 24px;
}
.cart-actions {
margin-left: auto;
align-self: flex-end;
display: flex;
align-items: center;
gap: 4px;
/* margin-left: 8px; */
.cart-count {
font-size: 12px;
min-width: 20px;
text-align: center;
}
}
.cart-container {
display: flex;
flex-direction: column;
height: 100vh;
}
/* 保持其他样式不变 */
.flex-header {
display: flex;
justify-content: flex-end;
background: white;
z-index: 1000;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}
.clear-header {
padding: 10px 16px;
/* 添加左右边距 */
margin-bottom: 0;
}
.clear-btn {
color: #e95d5d;
border-color: #e95d5d;
}
</style>