商品展示支持多张图片

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
+8
View File
@@ -138,4 +138,12 @@ api.interceptors.response.use(
}
)
export const fileApi = {
getSignedUrl: (fileId: number, expiryTimeInMinutes?: number) => {
return api.get<string>(`/file/get-signed-url/${fileId}`, {
params: { expiryTimeInMinutes }
})
}
}
export default api
+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%;
+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('返回按钮被点击')
+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>
@@ -16,7 +16,7 @@ import java.util.Map;
public class JwtUtil {
private static final String SECRET = "yuminail-secret-key-for-jwt-token-generation-2026-must-be-at-least-512-bits-long-for-hs512-algorithm-security-requirements";
private static final long ACCESS_TOKEN_EXPIRATION = 20 * 60 * 1000;
private static final long ACCESS_TOKEN_EXPIRATION = 2 * 60 * 60 * 1000;
private static final long REFRESH_TOKEN_EXPIRATION = 7 * 24 * 60 * 60 * 1000;
private final SecretKey key;