454 lines
9.6 KiB
Vue
454 lines
9.6 KiB
Vue
<template>
|
|
<div class="mobile-container">
|
|
|
|
<!-- Header -->
|
|
<header class="header">
|
|
<div class="back-btn" @click="handleBackClick">
|
|
<el-icon><ArrowLeft /></el-icon>
|
|
</div>
|
|
<h1>{{ product.name || '商品详情' }}</h1>
|
|
<div class="actions">
|
|
<el-icon @click="handleFavoriteClick" :style="{ fontSize: '20px' }" :class="{ active: isFavorite }"><Star /></el-icon>
|
|
</div>
|
|
</header>
|
|
|
|
<!-- Product Image Carousel -->
|
|
<div class="product-image-container">
|
|
<div v-if="productImages.length > 0" class="image-carousel">
|
|
<div
|
|
v-for="(image, index) in productImages"
|
|
:key="index"
|
|
class="carousel-item"
|
|
:class="{ active: currentImageIndex === index }"
|
|
@click="handleImageClick(index)"
|
|
>
|
|
<img
|
|
:src="image"
|
|
:alt="product.name"
|
|
class="product-image"
|
|
/>
|
|
</div>
|
|
</div>
|
|
<div v-if="productImages.length > 1" class="carousel-indicators">
|
|
<div
|
|
v-for="(image, index) in productImages"
|
|
:key="index"
|
|
class="indicator"
|
|
:class="{ active: currentImageIndex === index }"
|
|
@click="handleImageClick(index)"
|
|
></div>
|
|
</div>
|
|
<div v-else class="product-image-placeholder">
|
|
<span class="product-letter">{{ product.name ? product.name[0] : '商' }}</span>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Product Info -->
|
|
<div class="product-info">
|
|
<div class="info-header">
|
|
<h2 class="product-name">{{ product.name }}</h2>
|
|
<div class="price-tag">¥{{ product.price }}</div>
|
|
</div>
|
|
<div class="shop-info">
|
|
<span class="shop-name">店铺</span>
|
|
</div>
|
|
<div class="product-meta">
|
|
<div class="duration">
|
|
<el-icon><Clock /></el-icon>
|
|
<span>{{ product.estimatedTime }} 分钟</span>
|
|
</div>
|
|
<div class="stats">
|
|
<span class="like-count">
|
|
<el-icon><Star /></el-icon>
|
|
0 赞
|
|
</span>
|
|
<span class="collect-count">
|
|
<el-icon><Collection /></el-icon>
|
|
0 收藏
|
|
</span>
|
|
</div>
|
|
</div>
|
|
<div class="tags">
|
|
<el-tag size="small" effect="light" style="margin-right: 8px; margin-bottom: 8px">
|
|
{{ product.category }}
|
|
</el-tag>
|
|
</div>
|
|
<p class="description">{{ product.description }}</p>
|
|
</div>
|
|
|
|
<!-- Action Buttons -->
|
|
<div class="action-buttons">
|
|
<el-button class="add-cart-btn" @click="handleAddToCart">
|
|
<el-icon><ShoppingCart /></el-icon>
|
|
添加到购物车
|
|
</el-button>
|
|
<el-button class="buy-now-btn" type="primary" @click="handleBuyNow">
|
|
<el-icon><Right /></el-icon>
|
|
立即购买
|
|
</el-button>
|
|
</div>
|
|
</div>
|
|
</template>
|
|
|
|
<script setup lang="ts">
|
|
import { ref, onMounted, inject } from 'vue'
|
|
import { ArrowLeft, Star, Clock, Collection, ShoppingCart, Right } from '@element-plus/icons-vue'
|
|
import { useRouter, useRoute } from 'vue-router'
|
|
import { ElMessage } from 'element-plus'
|
|
import { productApi, type ProductVO } from '@/api/product'
|
|
|
|
import { cartApi } from '@/api/cart'
|
|
|
|
// 注入更新购物车数量的方法
|
|
const updateCartCount = inject<() => Promise<void>>('updateCartCount', async () => {})
|
|
|
|
const router = useRouter()
|
|
const route = useRoute()
|
|
|
|
const product = ref<ProductVO>({
|
|
id: 0,
|
|
shopId: null,
|
|
name: '',
|
|
description: '',
|
|
image: '',
|
|
price: 0,
|
|
estimatedTime: 0,
|
|
category: '',
|
|
status: 0,
|
|
createTime: '',
|
|
updateTime: ''
|
|
})
|
|
|
|
const isFavorite = ref(false)
|
|
const currentImageIndex = ref(0)
|
|
const productImages = ref<string[]>([])
|
|
|
|
// 从后端获取商品详情
|
|
const fetchProductDetail = async () => {
|
|
const productId = route.params.id
|
|
console.log('产品ID:', productId)
|
|
|
|
try {
|
|
const response = await productApi.getProductById(Number(productId)) as any
|
|
product.value = response || {}
|
|
|
|
await loadProductImages()
|
|
} catch (error) {
|
|
console.error('获取商品详情失败:', error)
|
|
ElMessage.error('未登录,请先登录')
|
|
}
|
|
}
|
|
|
|
// 加载商品图片
|
|
const loadProductImages = async () => {
|
|
// 由于ProductVO类型中没有imageFileIds属性,这里简化处理
|
|
if (product.value.image) {
|
|
productImages.value = [product.value.image]
|
|
currentImageIndex.value = 0
|
|
}
|
|
}
|
|
|
|
// 处理图片点击
|
|
const handleImageClick = (index: number) => {
|
|
currentImageIndex.value = index
|
|
}
|
|
|
|
const handleBackClick = () => {
|
|
console.log('返回按钮被点击')
|
|
router.back()
|
|
}
|
|
|
|
const handleFavoriteClick = () => {
|
|
isFavorite.value = !isFavorite.value
|
|
console.log('收藏状态:', isFavorite.value)
|
|
ElMessage.success(isFavorite.value ? '已收藏' : '已取消收藏')
|
|
}
|
|
|
|
const handleAddToCart = async () => {
|
|
console.log('添加到购物车按钮被点击')
|
|
console.log('商品ID:', product.value.id)
|
|
console.log('商品标题:', product.value.name)
|
|
try {
|
|
console.log('开始调用cartApi.addToCart')
|
|
await cartApi.addToCart({ productId: product.value.id, quantity: 1 })
|
|
console.log('调用cartApi.addToCart成功')
|
|
// 实时更新购物车角标数字
|
|
await updateCartCount()
|
|
ElMessage.success(`已将 ${product.value.name} 添加到购物车`)
|
|
} catch (error) {
|
|
console.error('添加到购物车失败:', error)
|
|
ElMessage.error('添加到购物车失败,请重试')
|
|
}
|
|
}
|
|
|
|
const handleBuyNow = async () => {
|
|
console.log('立即购买按钮被点击')
|
|
console.log('商品ID:', product.value.id)
|
|
console.log('商品标题:', product.value.name)
|
|
try {
|
|
console.log('开始调用cartApi.addToCart')
|
|
await cartApi.addToCart({ productId: product.value.id, quantity: 1 })
|
|
console.log('调用cartApi.addToCart成功')
|
|
// 实时更新购物车角标数字
|
|
await updateCartCount()
|
|
// 跳转到购物车页面
|
|
console.log('开始跳转到购物车页面')
|
|
router.push('/cart')
|
|
} catch (error) {
|
|
console.error('立即购买失败:', error)
|
|
ElMessage.error('操作失败,请重试')
|
|
}
|
|
}
|
|
|
|
// 页面挂载时获取商品详情
|
|
onMounted(() => {
|
|
fetchProductDetail()
|
|
})
|
|
</script>
|
|
|
|
<style scoped>
|
|
.mobile-container {
|
|
min-height: 100vh;
|
|
background: #f5f5f5;
|
|
}
|
|
|
|
.header {
|
|
display: flex;
|
|
justify-content: space-between;
|
|
align-items: center;
|
|
padding: 15px 20px;
|
|
border-bottom: 1px solid #f0f0f0;
|
|
background: white;
|
|
position: sticky;
|
|
top: 0;
|
|
z-index: 100;
|
|
}
|
|
|
|
.back-btn {
|
|
width: 40px;
|
|
height: 40px;
|
|
display: flex;
|
|
align-items: center;
|
|
cursor: pointer;
|
|
}
|
|
|
|
.header h1 {
|
|
font-size: 18px;
|
|
font-weight: bold;
|
|
margin: 0;
|
|
}
|
|
|
|
.actions {
|
|
width: 40px;
|
|
display: flex;
|
|
justify-content: center;
|
|
align-items: center;
|
|
cursor: pointer;
|
|
}
|
|
|
|
.actions .active {
|
|
color: #ff4d4f;
|
|
}
|
|
|
|
.product-image-container {
|
|
width: 100%;
|
|
height: 300px;
|
|
background: white;
|
|
position: relative;
|
|
overflow: hidden;
|
|
}
|
|
|
|
.image-carousel {
|
|
width: 100%;
|
|
height: 100%;
|
|
position: relative;
|
|
display: flex;
|
|
overflow-x: auto;
|
|
scroll-snap-type: x mandatory;
|
|
scrollbar-width: none;
|
|
}
|
|
|
|
.image-carousel::-webkit-scrollbar {
|
|
display: none;
|
|
}
|
|
|
|
.carousel-item {
|
|
flex: 0 0 auto;
|
|
width: 100%;
|
|
height: 100%;
|
|
scroll-snap-align: start;
|
|
cursor: pointer;
|
|
transition: opacity 0.3s;
|
|
}
|
|
|
|
.carousel-item.active {
|
|
opacity: 1;
|
|
}
|
|
|
|
.carousel-item:not(.active) {
|
|
opacity: 0.6;
|
|
}
|
|
|
|
.product-image {
|
|
width: 100%;
|
|
height: 100%;
|
|
object-fit: cover;
|
|
}
|
|
|
|
.carousel-indicators {
|
|
position: absolute;
|
|
bottom: 12px;
|
|
left: 50%;
|
|
transform: translateX(-50%);
|
|
display: flex;
|
|
gap: 8px;
|
|
}
|
|
|
|
.indicator {
|
|
width: 8px;
|
|
height: 8px;
|
|
border-radius: 50%;
|
|
background: rgba(255, 255, 255, 0.5);
|
|
cursor: pointer;
|
|
transition: all 0.3s;
|
|
}
|
|
|
|
.indicator.active {
|
|
background: white;
|
|
width: 24px;
|
|
border-radius: 4px;
|
|
}
|
|
|
|
.product-image-placeholder {
|
|
width: 100%;
|
|
height: 100%;
|
|
display: flex;
|
|
justify-content: center;
|
|
align-items: center;
|
|
font-size: 80px;
|
|
font-weight: bold;
|
|
color: rgba(0,0,0,0.1);
|
|
background: #f8f8f8;
|
|
}
|
|
|
|
.product-info {
|
|
padding: 20px;
|
|
background: white;
|
|
margin-bottom: 10px;
|
|
}
|
|
|
|
.info-header {
|
|
display: flex;
|
|
justify-content: space-between;
|
|
align-items: flex-start;
|
|
margin-bottom: 12px;
|
|
}
|
|
|
|
.product-name {
|
|
font-size: 20px;
|
|
font-weight: bold;
|
|
margin: 0;
|
|
flex: 1;
|
|
margin-right: 15px;
|
|
}
|
|
|
|
.price-tag {
|
|
font-size: 22px;
|
|
color: #ff4d4f;
|
|
font-weight: bold;
|
|
white-space: nowrap;
|
|
}
|
|
|
|
.shop-info {
|
|
margin-bottom: 12px;
|
|
}
|
|
|
|
.shop-name {
|
|
font-size: 14px;
|
|
color: #666;
|
|
background: #f8f8f8;
|
|
padding: 4px 12px;
|
|
border-radius: 12px;
|
|
}
|
|
|
|
.product-meta {
|
|
display: flex;
|
|
justify-content: space-between;
|
|
align-items: center;
|
|
margin-bottom: 16px;
|
|
padding: 12px;
|
|
background: #f8f8f8;
|
|
border-radius: 8px;
|
|
}
|
|
|
|
.duration {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 6px;
|
|
font-size: 14px;
|
|
color: #666;
|
|
}
|
|
|
|
.stats {
|
|
display: flex;
|
|
gap: 16px;
|
|
}
|
|
|
|
.like-count, .collect-count {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 4px;
|
|
font-size: 14px;
|
|
color: #666;
|
|
}
|
|
|
|
.tags {
|
|
margin-bottom: 16px;
|
|
display: flex;
|
|
flex-wrap: wrap;
|
|
gap: 8px;
|
|
}
|
|
|
|
.description {
|
|
color: #666;
|
|
line-height: 1.6;
|
|
font-size: 14px;
|
|
margin: 0;
|
|
}
|
|
|
|
.action-buttons {
|
|
padding: 20px;
|
|
display: flex;
|
|
gap: 15px;
|
|
background: white;
|
|
margin-top: 10px;
|
|
position: sticky;
|
|
bottom: 0;
|
|
z-index: 90;
|
|
border-top: 1px solid #f0f0f0;
|
|
}
|
|
|
|
.add-cart-btn, .buy-now-btn {
|
|
flex: 1;
|
|
height: 50px;
|
|
border-radius: 25px;
|
|
font-size: 16px;
|
|
font-weight: bold;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
gap: 8px;
|
|
}
|
|
|
|
.add-cart-btn {
|
|
background-color: #f5f5f5;
|
|
border: 1px solid #e8e8e8;
|
|
color: #333;
|
|
}
|
|
|
|
.buy-now-btn {
|
|
background-color: #ff4d4f;
|
|
border: none;
|
|
color: white;
|
|
}
|
|
</style> |