商品展示支持多张图片

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
+44 -2
View File
@@ -42,8 +42,8 @@
<!-- 商品图片 -->
<div class="product-img">
<img
v-if="item.imageUrls && item.imageUrls.length > 0"
:src="item.imageUrls[0]"
v-if="getProductImages(item).length > 0"
:src="getProductImages(item)[0]"
:alt="item.title"
class="product-image"
/>
@@ -138,6 +138,7 @@ import {
import { message } from 'ant-design-vue'
import { productApi, type ProductVO } from '@/api/product'
import { tagApi, type TagVO } from '@/api/tag'
import { fileApi } from '@/api/index'
const router = useRouter()
@@ -159,6 +160,10 @@ const hasMore = ref(true)
// 加载状态
const loading = ref(false)
// 图片缓存
const imageIdCache = ref<Record<string, string>>({})
const productImagesCache = ref<Record<number, string[]>>({})
// 从后端获取标签列表
const fetchTags = async () => {
try {
@@ -200,6 +205,9 @@ const fetchProducts = async () => {
console.log('Processed products:', products.value)
// 预加载所有商品的图片
await loadProductImages(products.value)
hasMore.value = false
} catch (error) {
console.error('获取商品失败:', error)
@@ -209,6 +217,40 @@ const fetchProducts = async () => {
}
}
// 加载商品图片
const loadProductImages = async (products: ProductVO[]) => {
for (const product of products) {
if (product.imageFileIds) {
const ids = product.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)
}
}
}
productImagesCache.value[product.id] = urls
} else {
productImagesCache.value[product.id] = []
}
}
}
// 获取商品图片
const getProductImages = (product: ProductVO): string[] => {
return productImagesCache.value[product.id] || []
}
// 处理返回按钮点击
const handleBackClick = () => {
console.log('返回按钮被点击')