2025-03-05 09:22:29 +08:00
|
|
|
|
<script setup lang="ts">
|
|
|
|
|
import { useCartStore } from "@/pinia/stores/cart"
|
|
|
|
|
import { storeToRefs } from "pinia"
|
2025-03-08 08:09:31 +08:00
|
|
|
|
import { showConfirmDialog } from "vant"
|
|
|
|
|
import { useRouter } from "vue-router"
|
2025-03-05 09:22:29 +08:00
|
|
|
|
|
2025-03-08 08:09:31 +08:00
|
|
|
|
// 新增购物车状态
|
2025-03-05 09:22:29 +08:00
|
|
|
|
|
|
|
|
|
// 定义组件事件:关闭详情
|
|
|
|
|
const emit = defineEmits(["cartClose"])
|
2025-03-08 08:09:31 +08:00
|
|
|
|
const router = useRouter()
|
|
|
|
|
// 状态管理
|
|
|
|
|
const cartStore = useCartStore()
|
|
|
|
|
const { cartItems, totalPrice, totalQuantity } = storeToRefs(cartStore)// 处理关闭按钮点击事件
|
2025-03-05 09:22:29 +08:00
|
|
|
|
function handleClose() {
|
2025-03-08 08:09:31 +08:00
|
|
|
|
emit("cartClose")
|
2025-03-05 09:22:29 +08:00
|
|
|
|
}
|
|
|
|
|
function handleAddToCart(product: any) {
|
2025-03-08 08:09:31 +08:00
|
|
|
|
cartStore.addToCart(product)
|
2025-03-05 09:22:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleRemoveFromCart(product: any) {
|
2025-03-08 08:09:31 +08:00
|
|
|
|
cartStore.removeFromCart(product.id)
|
2025-03-05 09:22:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleClearCart() {
|
2025-03-08 08:09:31 +08:00
|
|
|
|
showConfirmDialog({
|
|
|
|
|
title: "确认清空购物车吗?"
|
|
|
|
|
})
|
|
|
|
|
.then(() => {
|
|
|
|
|
cartStore.clearCart()
|
|
|
|
|
})
|
|
|
|
|
.catch(() => {
|
|
|
|
|
// on cancel
|
2025-03-05 09:22:29 +08:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 结算方法
|
|
|
|
|
function handleCheckout() {
|
2025-03-08 08:09:31 +08:00
|
|
|
|
handleClose()
|
|
|
|
|
router.push("/product/checkout")
|
2025-03-05 09:22:29 +08:00
|
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<template>
|
2025-03-08 08:09:31 +08:00
|
|
|
|
<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)"
|
|
|
|
|
/>
|
2025-03-05 09:22:29 +08:00
|
|
|
|
</div>
|
2025-03-08 08:09:31 +08:00
|
|
|
|
</div>
|
2025-03-05 09:22:29 +08:00
|
|
|
|
</div>
|
2025-03-08 08:09:31 +08:00
|
|
|
|
</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>
|
2025-03-05 09:22:29 +08:00
|
|
|
|
</div>
|
2025-03-08 08:09:31 +08:00
|
|
|
|
<van-button type="primary" size="small" @click="handleCheckout" class="checkout-btn">
|
|
|
|
|
去结算
|
|
|
|
|
</van-button>
|
|
|
|
|
</div>
|
2025-03-05 09:22:29 +08:00
|
|
|
|
</div>
|
2025-03-08 08:09:31 +08:00
|
|
|
|
</div>
|
2025-03-05 09:22:29 +08:00
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<style scoped>
|
|
|
|
|
.product-list {
|
2025-03-08 08:09:31 +08:00
|
|
|
|
flex: 1;
|
|
|
|
|
overflow-y: auto;
|
|
|
|
|
padding: 10px 16px 60px;
|
|
|
|
|
background: #ffffff;
|
2025-03-05 09:22:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.product-item {
|
2025-03-08 08:09:31 +08:00
|
|
|
|
margin-bottom: 10px;
|
|
|
|
|
padding: min(2.667vw, 20px) 0;
|
|
|
|
|
/* border-radius: 4px; */
|
|
|
|
|
/* box-shadow: 0 2px 4px rgba(0,0,0,0.05); */
|
2025-03-05 09:22:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.product-image {
|
2025-03-08 08:09:31 +08:00
|
|
|
|
margin-right: 12px;
|
|
|
|
|
border-radius: 4px;
|
|
|
|
|
overflow: hidden;
|
2025-03-05 09:22:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.product-info {
|
2025-03-08 08:09:31 +08:00
|
|
|
|
display: flex;
|
|
|
|
|
flex-direction: column;
|
|
|
|
|
justify-content: space-between;
|
|
|
|
|
height: 80px;
|
|
|
|
|
position: relative;
|
2025-03-05 09:22:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.product-name {
|
2025-03-08 08:09:31 +08:00
|
|
|
|
font-size: 14px;
|
|
|
|
|
color: #333;
|
|
|
|
|
line-height: 1.4;
|
|
|
|
|
text-align: left;
|
2025-03-05 09:22:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.product-price {
|
2025-03-08 08:09:31 +08:00
|
|
|
|
font-size: 16px;
|
|
|
|
|
color: #e95d5d;
|
|
|
|
|
font-weight: bold;
|
|
|
|
|
text-align: left;
|
2025-03-05 09:22:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.add-cart-btn {
|
2025-03-08 08:09:31 +08:00
|
|
|
|
align-self: flex-end;
|
|
|
|
|
margin-top: 0;
|
|
|
|
|
background-color: #e95d5d;
|
|
|
|
|
border-color: #e95d5d;
|
2025-03-05 09:22:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.action-row {
|
2025-03-08 08:09:31 +08:00
|
|
|
|
display: flex;
|
|
|
|
|
justify-content: space-between;
|
|
|
|
|
align-items: flex-end;
|
|
|
|
|
/* 底部对齐 */
|
|
|
|
|
margin-top: auto;
|
|
|
|
|
/* 推到容器底部 */
|
2025-03-05 09:22:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.stock-count {
|
2025-03-08 08:09:31 +08:00
|
|
|
|
font-size: 11px;
|
|
|
|
|
color: #bbbbbb;
|
|
|
|
|
margin-right: 8px;
|
|
|
|
|
display: flex;
|
|
|
|
|
align-items: flex-end;
|
|
|
|
|
height: 100%;
|
|
|
|
|
line-height: 1;
|
2025-03-05 09:22:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 修改购物车栏样式 */
|
|
|
|
|
.shopping-cart-bar {
|
2025-03-08 08:09:31 +08:00
|
|
|
|
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;
|
2025-03-05 09:22:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.cart-info {
|
2025-03-08 08:09:31 +08:00
|
|
|
|
display: flex;
|
|
|
|
|
align-items: center;
|
|
|
|
|
gap: 12px;
|
2025-03-05 09:22:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.total-price {
|
2025-03-08 08:09:31 +08:00
|
|
|
|
font-size: 14px;
|
|
|
|
|
color: #333;
|
|
|
|
|
font-weight: bold;
|
2025-03-05 09:22:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.checkout-btn {
|
2025-03-08 08:09:31 +08:00
|
|
|
|
background-color: #e95d5d;
|
|
|
|
|
border: none;
|
|
|
|
|
border-radius: 16px;
|
|
|
|
|
padding: 0 24px;
|
2025-03-05 09:22:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.cart-actions {
|
2025-03-08 08:09:31 +08:00
|
|
|
|
margin-left: auto;
|
|
|
|
|
align-self: flex-end;
|
|
|
|
|
display: flex;
|
|
|
|
|
align-items: center;
|
|
|
|
|
gap: 4px;
|
|
|
|
|
/* margin-left: 8px; */
|
2025-03-05 09:22:29 +08:00
|
|
|
|
|
2025-03-08 08:09:31 +08:00
|
|
|
|
.cart-count {
|
|
|
|
|
font-size: 12px;
|
|
|
|
|
min-width: 20px;
|
|
|
|
|
text-align: center;
|
|
|
|
|
}
|
2025-03-05 09:22:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.cart-container {
|
2025-03-08 08:09:31 +08:00
|
|
|
|
display: flex;
|
|
|
|
|
flex-direction: column;
|
|
|
|
|
height: 100vh;
|
2025-03-05 09:22:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 保持其他样式不变 */
|
|
|
|
|
.flex-header {
|
2025-03-08 08:09:31 +08:00
|
|
|
|
display: flex;
|
|
|
|
|
justify-content: flex-end;
|
|
|
|
|
background: white;
|
|
|
|
|
z-index: 1000;
|
|
|
|
|
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
|
2025-03-05 09:22:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.clear-header {
|
2025-03-08 08:09:31 +08:00
|
|
|
|
padding: 10px 16px;
|
|
|
|
|
/* 添加左右边距 */
|
|
|
|
|
margin-bottom: 0;
|
2025-03-05 09:22:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.clear-btn {
|
2025-03-08 08:09:31 +08:00
|
|
|
|
color: #e95d5d;
|
|
|
|
|
border-color: #e95d5d;
|
2025-03-05 09:22:29 +08:00
|
|
|
|
}
|
2025-03-08 08:09:31 +08:00
|
|
|
|
</style>
|