商品展示支持多张图片

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
+56 -24
View File
@@ -30,12 +30,25 @@
>
<template #bodyCell="{ column, record }">
<template v-if="column.key === 'image'">
<a-image
:width="60"
:height="60"
:src="imageUrls[record.id] || DEFAULT_PRODUCT_IMAGE"
style="object-fit: cover; border-radius: 4px;"
/>
<div class="product-images">
<a-image
v-for="(url, index) in getProductImages(record)"
:key="index"
:width="50"
:height="50"
:src="url"
style="object-fit: cover; border-radius: 4px; margin-right: 4px;"
:preview="false"
/>
<a-image
v-if="getProductImages(record).length === 0"
:width="50"
:height="50"
:src="DEFAULT_PRODUCT_IMAGE"
style="object-fit: cover; border-radius: 4px;"
:preview="false"
/>
</div>
</template>
<template v-else-if="column.key === 'price'">
<span style="color: #cf1322; font-weight: 600;">
@@ -177,7 +190,7 @@ const previewVisible = ref(false)
const previewImage = ref('')
const imageFileList = ref<any[]>([])
const uploadedFileIds = ref<string[]>([])
const imageUrls = ref<Record<number, string>>({})
const imageUrls = ref<Record<number, string[]>>({})
const imageIdCache = ref<Record<string, string>>({})
const pagination = reactive({
@@ -209,7 +222,7 @@ const formRules = {
}
const columns = [
{ title: '图片', key: 'image', width: 100 },
{ title: '图片', key: 'image', width: 150 },
{ title: '商品名称', dataIndex: 'title', key: 'title' },
{ title: '描述', dataIndex: 'description', key: 'description' },
{ title: '价格', key: 'price', width: 100 },
@@ -268,7 +281,7 @@ const fetchProductList = async () => {
productList.value = res.list || []
pagination.total = res.total || 0
// 预加载图片签名链接
// 预加载所有图片签名链接
await loadImageUrls(res.list || [])
} catch (error) {
message.error('获取商品列表失败')
@@ -280,28 +293,36 @@ const fetchProductList = async () => {
const loadImageUrls = async (products: Product[]) => {
for (const product of products) {
if (product.imageFileIds) {
const firstId = product.imageFileIds.split(',')[0]
if (firstId) {
try {
// 检查缓存
if (!imageIdCache.value[firstId]) {
const signedUrl = await adminFileApi.getSignedUrl(Number(firstId))
imageIdCache.value[firstId] = signedUrl
const ids = product.imageFileIds.split(',')
const urls: string[] = []
for (const id of ids) {
if (id) {
try {
// 检查缓存
if (!imageIdCache.value[id]) {
const signedUrl = await adminFileApi.getSignedUrl(Number(id))
imageIdCache.value[id] = signedUrl
}
urls.push(imageIdCache.value[id])
} catch (error) {
console.error('获取签名链接失败:', error)
urls.push(DEFAULT_PRODUCT_IMAGE)
}
imageUrls.value[product.id] = imageIdCache.value[firstId]
} catch (error) {
console.error('获取签名链接失败:', error)
imageUrls.value[product.id] = DEFAULT_PRODUCT_IMAGE
}
} else {
imageUrls.value[product.id] = DEFAULT_PRODUCT_IMAGE
}
imageUrls.value[product.id] = urls
} else {
imageUrls.value[product.id] = DEFAULT_PRODUCT_IMAGE
imageUrls.value[product.id] = [DEFAULT_PRODUCT_IMAGE]
}
}
}
const getProductImages = (product: Product): string[] => {
return imageUrls.value[product.id] || [DEFAULT_PRODUCT_IMAGE]
}
const handleSearch = () => {
pagination.current = 1
fetchProductList()
@@ -351,7 +372,7 @@ const showEditModal = async (record: Product) => {
const ids = record.imageFileIds.split(',')
uploadedFileIds.value = ids
// 加载图片签名链接
// 加载所有图片签名链接
const fileList = []
for (const [index, id] of ids.entries()) {
try {
@@ -485,4 +506,15 @@ onMounted(() => {
.products {
padding: 0;
}
.product-images {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 4px;
}
.product-images :deep(.ant-image) {
border-radius: 4px;
}
</style>