商品展示支持多张图片
This commit is contained in:
@@ -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
|
export default api
|
||||||
|
|||||||
@@ -12,14 +12,32 @@
|
|||||||
</div>
|
</div>
|
||||||
</header>
|
</header>
|
||||||
|
|
||||||
<!-- Product Image -->
|
<!-- Product Image Carousel -->
|
||||||
<div class="product-image-container">
|
<div class="product-image-container">
|
||||||
|
<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
|
<img
|
||||||
v-if="product.imageUrls && product.imageUrls.length > 0"
|
:src="url"
|
||||||
:src="product.imageUrls[0]"
|
|
||||||
:alt="product.title"
|
:alt="product.title"
|
||||||
class="product-image"
|
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">
|
<div v-else class="product-image-placeholder">
|
||||||
<span class="product-letter">{{ product.title ? product.title[0] : '商' }}</span>
|
<span class="product-letter">{{ product.title ? product.title[0] : '商' }}</span>
|
||||||
</div>
|
</div>
|
||||||
@@ -78,6 +96,7 @@ import { ArrowLeft, Star, Clock, Collection, ShoppingCart, Right } from '@elemen
|
|||||||
import { useRouter, useRoute } from 'vue-router'
|
import { useRouter, useRoute } from 'vue-router'
|
||||||
import { ElMessage } from 'element-plus'
|
import { ElMessage } from 'element-plus'
|
||||||
import { productApi, type ProductVO } from '@/api/product'
|
import { productApi, type ProductVO } from '@/api/product'
|
||||||
|
import { fileApi } from '@/api/index'
|
||||||
|
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
const route = useRoute()
|
const route = useRoute()
|
||||||
@@ -103,6 +122,9 @@ const product = ref<ProductVO>({
|
|||||||
})
|
})
|
||||||
|
|
||||||
const isFavorite = ref(false)
|
const isFavorite = ref(false)
|
||||||
|
const currentImageIndex = ref(0)
|
||||||
|
const productImages = ref<string[]>([])
|
||||||
|
const imageIdCache = ref<Record<string, string>>({})
|
||||||
|
|
||||||
// 从后端获取商品详情
|
// 从后端获取商品详情
|
||||||
const fetchProductDetail = async () => {
|
const fetchProductDetail = async () => {
|
||||||
@@ -119,12 +141,46 @@ const fetchProductDetail = async () => {
|
|||||||
imageUrls: productData.imageUrls || [],
|
imageUrls: productData.imageUrls || [],
|
||||||
tagList: productData.tagList || []
|
tagList: productData.tagList || []
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 加载所有图片的签名链接
|
||||||
|
await loadProductImages()
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('获取商品详情失败:', error)
|
console.error('获取商品详情失败:', error)
|
||||||
ElMessage.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 = () => {
|
const handleBackClick = () => {
|
||||||
console.log('返回按钮被点击')
|
console.log('返回按钮被点击')
|
||||||
router.back()
|
router.back()
|
||||||
@@ -202,6 +258,38 @@ onMounted(() => {
|
|||||||
height: 300px;
|
height: 300px;
|
||||||
background: white;
|
background: white;
|
||||||
position: relative;
|
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 {
|
.product-image {
|
||||||
@@ -210,6 +298,30 @@ onMounted(() => {
|
|||||||
object-fit: cover;
|
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 {
|
.product-image-placeholder {
|
||||||
width: 100%;
|
width: 100%;
|
||||||
height: 100%;
|
height: 100%;
|
||||||
|
|||||||
@@ -42,8 +42,8 @@
|
|||||||
<!-- 商品图片 -->
|
<!-- 商品图片 -->
|
||||||
<div class="product-img">
|
<div class="product-img">
|
||||||
<img
|
<img
|
||||||
v-if="item.imageUrls && item.imageUrls.length > 0"
|
v-if="getProductImages(item).length > 0"
|
||||||
:src="item.imageUrls[0]"
|
:src="getProductImages(item)[0]"
|
||||||
:alt="item.title"
|
:alt="item.title"
|
||||||
class="product-image"
|
class="product-image"
|
||||||
/>
|
/>
|
||||||
@@ -138,6 +138,7 @@ import {
|
|||||||
import { message } from 'ant-design-vue'
|
import { message } from 'ant-design-vue'
|
||||||
import { productApi, type ProductVO } from '@/api/product'
|
import { productApi, type ProductVO } from '@/api/product'
|
||||||
import { tagApi, type TagVO } from '@/api/tag'
|
import { tagApi, type TagVO } from '@/api/tag'
|
||||||
|
import { fileApi } from '@/api/index'
|
||||||
|
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
|
|
||||||
@@ -159,6 +160,10 @@ const hasMore = ref(true)
|
|||||||
// 加载状态
|
// 加载状态
|
||||||
const loading = ref(false)
|
const loading = ref(false)
|
||||||
|
|
||||||
|
// 图片缓存
|
||||||
|
const imageIdCache = ref<Record<string, string>>({})
|
||||||
|
const productImagesCache = ref<Record<number, string[]>>({})
|
||||||
|
|
||||||
// 从后端获取标签列表
|
// 从后端获取标签列表
|
||||||
const fetchTags = async () => {
|
const fetchTags = async () => {
|
||||||
try {
|
try {
|
||||||
@@ -200,6 +205,9 @@ const fetchProducts = async () => {
|
|||||||
|
|
||||||
console.log('Processed products:', products.value)
|
console.log('Processed products:', products.value)
|
||||||
|
|
||||||
|
// 预加载所有商品的图片
|
||||||
|
await loadProductImages(products.value)
|
||||||
|
|
||||||
hasMore.value = false
|
hasMore.value = false
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('获取商品失败:', 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 = () => {
|
const handleBackClick = () => {
|
||||||
console.log('返回按钮被点击')
|
console.log('返回按钮被点击')
|
||||||
|
|||||||
@@ -30,12 +30,25 @@
|
|||||||
>
|
>
|
||||||
<template #bodyCell="{ column, record }">
|
<template #bodyCell="{ column, record }">
|
||||||
<template v-if="column.key === 'image'">
|
<template v-if="column.key === 'image'">
|
||||||
|
<div class="product-images">
|
||||||
<a-image
|
<a-image
|
||||||
:width="60"
|
v-for="(url, index) in getProductImages(record)"
|
||||||
:height="60"
|
:key="index"
|
||||||
:src="imageUrls[record.id] || DEFAULT_PRODUCT_IMAGE"
|
:width="50"
|
||||||
style="object-fit: cover; border-radius: 4px;"
|
: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>
|
||||||
<template v-else-if="column.key === 'price'">
|
<template v-else-if="column.key === 'price'">
|
||||||
<span style="color: #cf1322; font-weight: 600;">
|
<span style="color: #cf1322; font-weight: 600;">
|
||||||
@@ -177,7 +190,7 @@ const previewVisible = ref(false)
|
|||||||
const previewImage = ref('')
|
const previewImage = ref('')
|
||||||
const imageFileList = ref<any[]>([])
|
const imageFileList = ref<any[]>([])
|
||||||
const uploadedFileIds = ref<string[]>([])
|
const uploadedFileIds = ref<string[]>([])
|
||||||
const imageUrls = ref<Record<number, string>>({})
|
const imageUrls = ref<Record<number, string[]>>({})
|
||||||
const imageIdCache = ref<Record<string, string>>({})
|
const imageIdCache = ref<Record<string, string>>({})
|
||||||
|
|
||||||
const pagination = reactive({
|
const pagination = reactive({
|
||||||
@@ -209,7 +222,7 @@ const formRules = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const columns = [
|
const columns = [
|
||||||
{ title: '图片', key: 'image', width: 100 },
|
{ title: '图片', key: 'image', width: 150 },
|
||||||
{ title: '商品名称', dataIndex: 'title', key: 'title' },
|
{ title: '商品名称', dataIndex: 'title', key: 'title' },
|
||||||
{ title: '描述', dataIndex: 'description', key: 'description' },
|
{ title: '描述', dataIndex: 'description', key: 'description' },
|
||||||
{ title: '价格', key: 'price', width: 100 },
|
{ title: '价格', key: 'price', width: 100 },
|
||||||
@@ -268,7 +281,7 @@ const fetchProductList = async () => {
|
|||||||
productList.value = res.list || []
|
productList.value = res.list || []
|
||||||
pagination.total = res.total || 0
|
pagination.total = res.total || 0
|
||||||
|
|
||||||
// 预加载图片签名链接
|
// 预加载所有图片签名链接
|
||||||
await loadImageUrls(res.list || [])
|
await loadImageUrls(res.list || [])
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
message.error('获取商品列表失败')
|
message.error('获取商品列表失败')
|
||||||
@@ -280,28 +293,36 @@ const fetchProductList = async () => {
|
|||||||
const loadImageUrls = async (products: Product[]) => {
|
const loadImageUrls = async (products: Product[]) => {
|
||||||
for (const product of products) {
|
for (const product of products) {
|
||||||
if (product.imageFileIds) {
|
if (product.imageFileIds) {
|
||||||
const firstId = product.imageFileIds.split(',')[0]
|
const ids = product.imageFileIds.split(',')
|
||||||
if (firstId) {
|
const urls: string[] = []
|
||||||
|
|
||||||
|
for (const id of ids) {
|
||||||
|
if (id) {
|
||||||
try {
|
try {
|
||||||
// 检查缓存
|
// 检查缓存
|
||||||
if (!imageIdCache.value[firstId]) {
|
if (!imageIdCache.value[id]) {
|
||||||
const signedUrl = await adminFileApi.getSignedUrl(Number(firstId))
|
const signedUrl = await adminFileApi.getSignedUrl(Number(id))
|
||||||
imageIdCache.value[firstId] = signedUrl
|
imageIdCache.value[id] = signedUrl
|
||||||
}
|
}
|
||||||
imageUrls.value[product.id] = imageIdCache.value[firstId]
|
urls.push(imageIdCache.value[id])
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('获取签名链接失败:', error)
|
console.error('获取签名链接失败:', error)
|
||||||
imageUrls.value[product.id] = DEFAULT_PRODUCT_IMAGE
|
urls.push(DEFAULT_PRODUCT_IMAGE)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
imageUrls.value[product.id] = urls
|
||||||
} else {
|
} else {
|
||||||
imageUrls.value[product.id] = DEFAULT_PRODUCT_IMAGE
|
imageUrls.value[product.id] = [DEFAULT_PRODUCT_IMAGE]
|
||||||
}
|
|
||||||
} else {
|
|
||||||
imageUrls.value[product.id] = DEFAULT_PRODUCT_IMAGE
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const getProductImages = (product: Product): string[] => {
|
||||||
|
return imageUrls.value[product.id] || [DEFAULT_PRODUCT_IMAGE]
|
||||||
|
}
|
||||||
|
|
||||||
const handleSearch = () => {
|
const handleSearch = () => {
|
||||||
pagination.current = 1
|
pagination.current = 1
|
||||||
fetchProductList()
|
fetchProductList()
|
||||||
@@ -351,7 +372,7 @@ const showEditModal = async (record: Product) => {
|
|||||||
const ids = record.imageFileIds.split(',')
|
const ids = record.imageFileIds.split(',')
|
||||||
uploadedFileIds.value = ids
|
uploadedFileIds.value = ids
|
||||||
|
|
||||||
// 加载图片签名链接
|
// 加载所有图片签名链接
|
||||||
const fileList = []
|
const fileList = []
|
||||||
for (const [index, id] of ids.entries()) {
|
for (const [index, id] of ids.entries()) {
|
||||||
try {
|
try {
|
||||||
@@ -485,4 +506,15 @@ onMounted(() => {
|
|||||||
.products {
|
.products {
|
||||||
padding: 0;
|
padding: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.product-images {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
gap: 4px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.product-images :deep(.ant-image) {
|
||||||
|
border-radius: 4px;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
@@ -16,7 +16,7 @@ import java.util.Map;
|
|||||||
public class JwtUtil {
|
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 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 static final long REFRESH_TOKEN_EXPIRATION = 7 * 24 * 60 * 60 * 1000;
|
||||||
|
|
||||||
private final SecretKey key;
|
private final SecretKey key;
|
||||||
|
|||||||
Reference in New Issue
Block a user