商品展示支持多张图片

This commit is contained in:
xiaowang
2026-02-26 11:16:24 +08:00
parent 501aeb8ea3
commit 98c9463efa
5 changed files with 228 additions and 34 deletions
+119 -7
View File
@@ -12,14 +12,32 @@
</div>
</header>
<!-- Product Image -->
<!-- Product Image Carousel -->
<div class="product-image-container">
<img
v-if="product.imageUrls && product.imageUrls.length > 0"
:src="product.imageUrls[0]"
:alt="product.title"
class="product-image"
/>
<div v-if="productImages.length > 0" class="image-carousel">
<div
v-for="(url, index) in productImages"
:key="index"
class="carousel-item"
:class="{ active: currentImageIndex === index }"
@click="handleImageClick(index)"
>
<img
:src="url"
:alt="product.title"
class="product-image"
/>
</div>
</div>
<div v-if="productImages.length > 1" class="carousel-indicators">
<div
v-for="(url, 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.title ? product.title[0] : '商' }}</span>
</div>
@@ -78,6 +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'
const router = useRouter()
const route = useRoute()
@@ -103,6 +122,9 @@ 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 () => {
@@ -119,12 +141,46 @@ const fetchProductDetail = async () => {
imageUrls: productData.imageUrls || [],
tagList: productData.tagList || []
}
// 加载所有图片的签名链接
await loadProductImages()
} catch (error) {
console.error('获取商品详情失败:', error)
ElMessage.error('未登录,请先登录')
}
}
// 加载商品图片
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
currentImageIndex.value = 0
}
}
// 处理图片点击
const handleImageClick = (index: number) => {
currentImageIndex.value = index
}
const handleBackClick = () => {
console.log('返回按钮被点击')
router.back()
@@ -202,6 +258,38 @@ onMounted(() => {
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 {
@@ -210,6 +298,30 @@ onMounted(() => {
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%;