优化一些问题
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
<div class="back-btn" @click="handleBackClick">
|
||||
<el-icon><ArrowLeft /></el-icon>
|
||||
</div>
|
||||
<h1>{{ product.title || '商品详情' }}</h1>
|
||||
<h1>{{ product.name || '商品详情' }}</h1>
|
||||
<div class="actions">
|
||||
<el-icon @click="handleFavoriteClick" :style="{ fontSize: '20px' }" :class="{ active: isFavorite }"><Star /></el-icon>
|
||||
</div>
|
||||
@@ -16,22 +16,22 @@
|
||||
<div class="product-image-container">
|
||||
<div v-if="productImages.length > 0" class="image-carousel">
|
||||
<div
|
||||
v-for="(url, index) in productImages"
|
||||
v-for="(image, index) in productImages"
|
||||
:key="index"
|
||||
class="carousel-item"
|
||||
:class="{ active: currentImageIndex === index }"
|
||||
@click="handleImageClick(index)"
|
||||
>
|
||||
<img
|
||||
:src="url"
|
||||
:alt="product.title"
|
||||
:src="image"
|
||||
:alt="product.name"
|
||||
class="product-image"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<div v-if="productImages.length > 1" class="carousel-indicators">
|
||||
<div
|
||||
v-for="(url, index) in productImages"
|
||||
v-for="(image, index) in productImages"
|
||||
:key="index"
|
||||
class="indicator"
|
||||
:class="{ active: currentImageIndex === index }"
|
||||
@@ -39,38 +39,38 @@
|
||||
></div>
|
||||
</div>
|
||||
<div v-else class="product-image-placeholder">
|
||||
<span class="product-letter">{{ product.title ? product.title[0] : '商' }}</span>
|
||||
<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.title }}</h2>
|
||||
<h2 class="product-name">{{ product.name }}</h2>
|
||||
<div class="price-tag">¥{{ product.price }}</div>
|
||||
</div>
|
||||
<div class="shop-info">
|
||||
<span class="shop-name">{{ product.shopName }}</span>
|
||||
<span class="shop-name">店铺</span>
|
||||
</div>
|
||||
<div class="product-meta">
|
||||
<div class="duration">
|
||||
<el-icon><Clock /></el-icon>
|
||||
<span>{{ product.approximateDuration }}</span>
|
||||
<span>{{ product.estimatedTime }} 分钟</span>
|
||||
</div>
|
||||
<div class="stats">
|
||||
<span class="like-count">
|
||||
<el-icon><Star /></el-icon>
|
||||
{{ product.likeCount }} 赞
|
||||
0 赞
|
||||
</span>
|
||||
<span class="collect-count">
|
||||
<el-icon><Collection /></el-icon>
|
||||
{{ product.collectCount }} 收藏
|
||||
0 收藏
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="tags">
|
||||
<el-tag v-for="(tag, index) in product.tagList" :key="index" size="small" effect="light" style="margin-right: 8px; margin-bottom: 8px">
|
||||
{{ tag }}
|
||||
<el-tag size="small" effect="light" style="margin-right: 8px; margin-bottom: 8px">
|
||||
{{ product.category }}
|
||||
</el-tag>
|
||||
</div>
|
||||
<p class="description">{{ product.description }}</p>
|
||||
@@ -96,7 +96,7 @@ import { ArrowLeft, Star, Clock, Collection, ShoppingCart, Right } from '@elemen
|
||||
import { useRouter, useRoute } from 'vue-router'
|
||||
import { ElMessage } from 'element-plus'
|
||||
import { productApi, type ProductVO } from '@/api/product'
|
||||
import { fileApi } from '@/api/index'
|
||||
|
||||
import { cartApi } from '@/api/cart'
|
||||
|
||||
// 注入更新购物车数量的方法
|
||||
@@ -107,20 +107,14 @@ const route = useRoute()
|
||||
|
||||
const product = ref<ProductVO>({
|
||||
id: 0,
|
||||
shopId: 0,
|
||||
shopName: '',
|
||||
title: '',
|
||||
shopId: null,
|
||||
name: '',
|
||||
description: '',
|
||||
imageFileIds: '',
|
||||
imageUrls: [],
|
||||
image: '',
|
||||
price: 0,
|
||||
tags: '',
|
||||
tagList: [],
|
||||
likeCount: 0,
|
||||
collectCount: 0,
|
||||
approximateDuration: '',
|
||||
bookableArtists: '',
|
||||
availableCoupons: '',
|
||||
estimatedTime: 0,
|
||||
category: '',
|
||||
status: 0,
|
||||
createTime: '',
|
||||
updateTime: ''
|
||||
})
|
||||
@@ -128,7 +122,6 @@ const product = ref<ProductVO>({
|
||||
const isFavorite = ref(false)
|
||||
const currentImageIndex = ref(0)
|
||||
const productImages = ref<string[]>([])
|
||||
const imageIdCache = ref<Record<string, string>>({})
|
||||
|
||||
// 从后端获取商品详情
|
||||
const fetchProductDetail = async () => {
|
||||
@@ -136,17 +129,9 @@ const fetchProductDetail = async () => {
|
||||
console.log('产品ID:', productId)
|
||||
|
||||
try {
|
||||
const response = await productApi.getProductById(Number(productId))
|
||||
const response = await productApi.getProductById(Number(productId)) as any
|
||||
product.value = response || {}
|
||||
|
||||
// 处理返回的数据
|
||||
const productData = response as any
|
||||
product.value = {
|
||||
...productData,
|
||||
imageUrls: productData.imageUrls || [],
|
||||
tagList: productData.tagList || []
|
||||
}
|
||||
|
||||
// 加载所有图片的签名链接
|
||||
await loadProductImages()
|
||||
} catch (error) {
|
||||
console.error('获取商品详情失败:', error)
|
||||
@@ -156,26 +141,9 @@ const fetchProductDetail = async () => {
|
||||
|
||||
// 加载商品图片
|
||||
const loadProductImages = async () => {
|
||||
if (product.value.imageFileIds) {
|
||||
const ids = product.value.imageFileIds.split(',')
|
||||
const urls: string[] = []
|
||||
|
||||
for (const id of ids) {
|
||||
if (id) {
|
||||
try {
|
||||
// 检查缓存
|
||||
if (!imageIdCache.value[id]) {
|
||||
const signedUrl = await fileApi.getSignedUrl(Number(id))
|
||||
imageIdCache.value[id] = signedUrl
|
||||
}
|
||||
urls.push(imageIdCache.value[id])
|
||||
} catch (error) {
|
||||
console.error('获取签名链接失败:', error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
productImages.value = urls
|
||||
// 由于ProductVO类型中没有imageFileIds属性,这里简化处理
|
||||
if (product.value.image) {
|
||||
productImages.value = [product.value.image]
|
||||
currentImageIndex.value = 0
|
||||
}
|
||||
}
|
||||
@@ -199,14 +167,14 @@ const handleFavoriteClick = () => {
|
||||
const handleAddToCart = async () => {
|
||||
console.log('添加到购物车按钮被点击')
|
||||
console.log('商品ID:', product.value.id)
|
||||
console.log('商品标题:', product.value.title)
|
||||
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.title} 添加到购物车`)
|
||||
ElMessage.success(`已将 ${product.value.name} 添加到购物车`)
|
||||
} catch (error) {
|
||||
console.error('添加到购物车失败:', error)
|
||||
ElMessage.error('添加到购物车失败,请重试')
|
||||
@@ -216,7 +184,7 @@ const handleAddToCart = async () => {
|
||||
const handleBuyNow = async () => {
|
||||
console.log('立即购买按钮被点击')
|
||||
console.log('商品ID:', product.value.id)
|
||||
console.log('商品标题:', product.value.title)
|
||||
console.log('商品标题:', product.value.name)
|
||||
try {
|
||||
console.log('开始调用cartApi.addToCart')
|
||||
await cartApi.addToCart({ productId: product.value.id, quantity: 1 })
|
||||
|
||||
Reference in New Issue
Block a user