功能:支持作品图片多选上传与压缩
This commit is contained in:
@@ -0,0 +1,196 @@
|
|||||||
|
const DEFAULT_MAX_IMAGE_SIZE = 5 * 1024 * 1024
|
||||||
|
const AUTO_COMPRESS_TYPES = new Set(['image/jpeg', 'image/jpg', 'image/png', 'image/webp'])
|
||||||
|
|
||||||
|
export interface ImageCompressionResult {
|
||||||
|
file: File
|
||||||
|
originalSize: number
|
||||||
|
finalSize: number
|
||||||
|
compressed: boolean
|
||||||
|
converted: boolean
|
||||||
|
skippedReason?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
const QUALITY_STEPS = [0.92, 0.84, 0.76, 0.68, 0.6, 0.52]
|
||||||
|
const SCALE_STEPS = [1, 0.9, 0.8, 0.7, 0.6, 0.5]
|
||||||
|
|
||||||
|
const loadImageElement = (file: File) => {
|
||||||
|
return new Promise<HTMLImageElement>((resolve, reject) => {
|
||||||
|
const image = new Image()
|
||||||
|
const objectUrl = URL.createObjectURL(file)
|
||||||
|
|
||||||
|
image.onload = () => {
|
||||||
|
URL.revokeObjectURL(objectUrl)
|
||||||
|
resolve(image)
|
||||||
|
}
|
||||||
|
|
||||||
|
image.onerror = () => {
|
||||||
|
URL.revokeObjectURL(objectUrl)
|
||||||
|
reject(new Error('图片读取失败'))
|
||||||
|
}
|
||||||
|
|
||||||
|
image.src = objectUrl
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const canvasToBlob = (canvas: HTMLCanvasElement, type: string, quality?: number) => {
|
||||||
|
return new Promise<Blob>((resolve, reject) => {
|
||||||
|
canvas.toBlob(
|
||||||
|
(blob) => {
|
||||||
|
if (!blob) {
|
||||||
|
reject(new Error('图片压缩失败'))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
resolve(blob)
|
||||||
|
},
|
||||||
|
type,
|
||||||
|
quality
|
||||||
|
)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const getOutputExtension = (type: string) => {
|
||||||
|
if (type === 'image/png') {
|
||||||
|
return '.png'
|
||||||
|
}
|
||||||
|
if (type === 'image/webp') {
|
||||||
|
return '.webp'
|
||||||
|
}
|
||||||
|
return '.jpg'
|
||||||
|
}
|
||||||
|
|
||||||
|
const buildOutputName = (fileName: string, type: string) => {
|
||||||
|
const extension = getOutputExtension(type)
|
||||||
|
const dotIndex = fileName.lastIndexOf('.')
|
||||||
|
const baseName = dotIndex > -1 ? fileName.slice(0, dotIndex) : fileName
|
||||||
|
return `${baseName}${extension}`
|
||||||
|
}
|
||||||
|
|
||||||
|
const drawImageToCanvas = (image: HTMLImageElement, width: number, height: number, mimeType: string) => {
|
||||||
|
const canvas = document.createElement('canvas')
|
||||||
|
canvas.width = width
|
||||||
|
canvas.height = height
|
||||||
|
|
||||||
|
const context = canvas.getContext('2d')
|
||||||
|
if (!context) {
|
||||||
|
throw new Error('无法初始化图片压缩画布')
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mimeType !== 'image/png') {
|
||||||
|
context.fillStyle = '#ffffff'
|
||||||
|
context.fillRect(0, 0, width, height)
|
||||||
|
}
|
||||||
|
|
||||||
|
context.drawImage(image, 0, 0, width, height)
|
||||||
|
return canvas
|
||||||
|
}
|
||||||
|
|
||||||
|
const toCompressedFile = (blob: Blob, sourceFile: File, mimeType: string) => {
|
||||||
|
return new File([blob], buildOutputName(sourceFile.name, mimeType), {
|
||||||
|
type: mimeType,
|
||||||
|
lastModified: Date.now()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const getPreferredMimeTypes = (file: File) => {
|
||||||
|
if (file.type === 'image/png') {
|
||||||
|
return ['image/png', 'image/jpeg']
|
||||||
|
}
|
||||||
|
if (file.type === 'image/webp') {
|
||||||
|
return ['image/webp', 'image/jpeg']
|
||||||
|
}
|
||||||
|
return ['image/jpeg']
|
||||||
|
}
|
||||||
|
|
||||||
|
export const formatFileSize = (bytes: number) => {
|
||||||
|
if (bytes >= 1024 * 1024) {
|
||||||
|
return `${(bytes / 1024 / 1024).toFixed(2)}MB`
|
||||||
|
}
|
||||||
|
return `${(bytes / 1024).toFixed(0)}KB`
|
||||||
|
}
|
||||||
|
|
||||||
|
export const compressImageIfNeeded = async (
|
||||||
|
file: File,
|
||||||
|
maxSize = DEFAULT_MAX_IMAGE_SIZE
|
||||||
|
): Promise<ImageCompressionResult> => {
|
||||||
|
if (!file.type.startsWith('image/')) {
|
||||||
|
return {
|
||||||
|
file,
|
||||||
|
originalSize: file.size,
|
||||||
|
finalSize: file.size,
|
||||||
|
compressed: false,
|
||||||
|
converted: false,
|
||||||
|
skippedReason: '仅支持图片文件上传'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (file.size <= maxSize) {
|
||||||
|
return {
|
||||||
|
file,
|
||||||
|
originalSize: file.size,
|
||||||
|
finalSize: file.size,
|
||||||
|
compressed: false,
|
||||||
|
converted: false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!AUTO_COMPRESS_TYPES.has(file.type)) {
|
||||||
|
return {
|
||||||
|
file,
|
||||||
|
originalSize: file.size,
|
||||||
|
finalSize: file.size,
|
||||||
|
compressed: false,
|
||||||
|
converted: false,
|
||||||
|
skippedReason: '当前图片格式暂不支持自动压缩,请手动处理后重试'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const image = await loadImageElement(file)
|
||||||
|
const preferredMimeTypes = getPreferredMimeTypes(file)
|
||||||
|
let bestBlob: Blob | null = null
|
||||||
|
let bestMimeType = file.type
|
||||||
|
|
||||||
|
for (const scale of SCALE_STEPS) {
|
||||||
|
const width = Math.max(1, Math.round(image.naturalWidth * scale))
|
||||||
|
const height = Math.max(1, Math.round(image.naturalHeight * scale))
|
||||||
|
|
||||||
|
for (const mimeType of preferredMimeTypes) {
|
||||||
|
const canvas = drawImageToCanvas(image, width, height, mimeType)
|
||||||
|
const qualitySteps = mimeType === 'image/png' ? [undefined] : QUALITY_STEPS
|
||||||
|
|
||||||
|
for (const quality of qualitySteps) {
|
||||||
|
const blob = await canvasToBlob(canvas, mimeType, quality)
|
||||||
|
|
||||||
|
if (!bestBlob || blob.size < bestBlob.size) {
|
||||||
|
bestBlob = blob
|
||||||
|
bestMimeType = mimeType
|
||||||
|
}
|
||||||
|
|
||||||
|
if (blob.size <= maxSize) {
|
||||||
|
const compressedFile = toCompressedFile(blob, file, mimeType)
|
||||||
|
return {
|
||||||
|
file: compressedFile,
|
||||||
|
originalSize: file.size,
|
||||||
|
finalSize: blob.size,
|
||||||
|
compressed: true,
|
||||||
|
converted: mimeType !== file.type
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!bestBlob) {
|
||||||
|
throw new Error('图片压缩失败')
|
||||||
|
}
|
||||||
|
|
||||||
|
const compressedFile = toCompressedFile(bestBlob, file, bestMimeType)
|
||||||
|
return {
|
||||||
|
file: compressedFile,
|
||||||
|
originalSize: file.size,
|
||||||
|
finalSize: bestBlob.size,
|
||||||
|
compressed: true,
|
||||||
|
converted: bestMimeType !== file.type
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const MAX_IMAGE_UPLOAD_SIZE = DEFAULT_MAX_IMAGE_SIZE
|
||||||
@@ -136,17 +136,20 @@
|
|||||||
<a-upload
|
<a-upload
|
||||||
v-model:file-list="imageFileList"
|
v-model:file-list="imageFileList"
|
||||||
list-type="picture-card"
|
list-type="picture-card"
|
||||||
|
multiple
|
||||||
|
accept="image/*"
|
||||||
:custom-request="handleUploadImage"
|
:custom-request="handleUploadImage"
|
||||||
:before-upload="beforeUpload"
|
:before-upload="beforeUpload"
|
||||||
@preview="handlePreview"
|
@preview="handlePreview"
|
||||||
@remove="handleRemove"
|
@remove="handleRemove"
|
||||||
:max-count="5"
|
:max-count="MAX_PRODUCT_IMAGE_COUNT"
|
||||||
>
|
>
|
||||||
<div v-if="imageFileList.length < 5">
|
<div v-if="imageFileList.length < MAX_PRODUCT_IMAGE_COUNT">
|
||||||
<plus-outlined />
|
<plus-outlined />
|
||||||
<div style="margin-top: 8px">上传图片</div>
|
<div style="margin-top: 8px">上传图片</div>
|
||||||
</div>
|
</div>
|
||||||
</a-upload>
|
</a-upload>
|
||||||
|
<div class="upload-tip">支持多选上传,单张图片超过 5MB 会自动压缩后再上传。</div>
|
||||||
<a-modal v-model:open="previewVisible" :footer="null" @cancel="previewVisible = false">
|
<a-modal v-model:open="previewVisible" :footer="null" @cancel="previewVisible = false">
|
||||||
<img alt="预览" style="width: 100%" :src="previewImage" />
|
<img alt="预览" style="width: 100%" :src="previewImage" />
|
||||||
</a-modal>
|
</a-modal>
|
||||||
@@ -365,6 +368,7 @@ import { PlusOutlined } from '@ant-design/icons-vue'
|
|||||||
import { DEFAULT_PRODUCT_IMAGE } from '@/constants'
|
import { DEFAULT_PRODUCT_IMAGE } from '@/constants'
|
||||||
import { adminProductApi, type Product, adminShopApi, type Shop, adminFileApi, adminTagApi, adminArtistApi, adminCouponApi } from '@/api/admin'
|
import { adminProductApi, type Product, adminShopApi, type Shop, adminFileApi, adminTagApi, adminArtistApi, adminCouponApi } from '@/api/admin'
|
||||||
import { usePermission } from '@/utils/permission'
|
import { usePermission } from '@/utils/permission'
|
||||||
|
import { MAX_IMAGE_UPLOAD_SIZE, compressImageIfNeeded, formatFileSize } from '@/utils/image-upload'
|
||||||
|
|
||||||
const { hasPermission, loadUserPermissions } = usePermission()
|
const { hasPermission, loadUserPermissions } = usePermission()
|
||||||
const loading = ref(false)
|
const loading = ref(false)
|
||||||
@@ -392,6 +396,7 @@ const viewImageUrls = ref<string[]>([])
|
|||||||
const imageIdCache = ref<Record<string, string>>({})
|
const imageIdCache = ref<Record<string, string>>({})
|
||||||
const isAdmin = ref(false)
|
const isAdmin = ref(false)
|
||||||
const formRef = ref<any>(null)
|
const formRef = ref<any>(null)
|
||||||
|
const MAX_PRODUCT_IMAGE_COUNT = 5
|
||||||
|
|
||||||
const viewFormState = reactive({
|
const viewFormState = reactive({
|
||||||
shopId: undefined,
|
shopId: undefined,
|
||||||
@@ -644,7 +649,7 @@ const showAddModal = () => {
|
|||||||
availableCoupons: []
|
availableCoupons: []
|
||||||
})
|
})
|
||||||
imageFileList.value = []
|
imageFileList.value = []
|
||||||
uploadedFileIds.value = []
|
syncUploadedFileIds([])
|
||||||
// 如果有店铺ID,获取该店铺的美甲师列表
|
// 如果有店铺ID,获取该店铺的美甲师列表
|
||||||
if (shopId) {
|
if (shopId) {
|
||||||
fetchArtists(shopId)
|
fetchArtists(shopId)
|
||||||
@@ -688,8 +693,8 @@ const showEditModal = async (record: Product | Record<string, any>) => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
if (recordImageFileIds) {
|
if (recordImageFileIds) {
|
||||||
const ids = recordImageFileIds.split(',')
|
const ids = recordImageFileIds.split(',').filter((id: string) => id)
|
||||||
uploadedFileIds.value = ids
|
syncUploadedFileIds(ids)
|
||||||
|
|
||||||
// 加载所有图片签名链接
|
// 加载所有图片签名链接
|
||||||
const fileList = []
|
const fileList = []
|
||||||
@@ -702,6 +707,7 @@ const showEditModal = async (record: Product | Record<string, any>) => {
|
|||||||
}
|
}
|
||||||
fileList.push({
|
fileList.push({
|
||||||
uid: id,
|
uid: id,
|
||||||
|
fileId: id,
|
||||||
name: `图片${index + 1}`,
|
name: `图片${index + 1}`,
|
||||||
status: 'done',
|
status: 'done',
|
||||||
url: imageIdCache.value[id]
|
url: imageIdCache.value[id]
|
||||||
@@ -710,6 +716,7 @@ const showEditModal = async (record: Product | Record<string, any>) => {
|
|||||||
console.error('获取签名链接失败:', error)
|
console.error('获取签名链接失败:', error)
|
||||||
fileList.push({
|
fileList.push({
|
||||||
uid: id,
|
uid: id,
|
||||||
|
fileId: id,
|
||||||
name: `图片${index + 1}`,
|
name: `图片${index + 1}`,
|
||||||
status: 'done',
|
status: 'done',
|
||||||
url: ''
|
url: ''
|
||||||
@@ -719,7 +726,7 @@ const showEditModal = async (record: Product | Record<string, any>) => {
|
|||||||
imageFileList.value = fileList
|
imageFileList.value = fileList
|
||||||
} else {
|
} else {
|
||||||
imageFileList.value = []
|
imageFileList.value = []
|
||||||
uploadedFileIds.value = []
|
syncUploadedFileIds([])
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取该店铺的美甲师列表
|
// 获取该店铺的美甲师列表
|
||||||
@@ -764,15 +771,15 @@ const showViewModal = async (record: Product | Record<string, any>) => {
|
|||||||
|
|
||||||
// 加载图片URL
|
// 加载图片URL
|
||||||
if (recordImageFileIds) {
|
if (recordImageFileIds) {
|
||||||
const ids = recordImageFileIds.split(',')
|
const ids = recordImageFileIds.split(',').filter((id: string) => id)
|
||||||
const urls: string[] = []
|
const urls: string[] = []
|
||||||
|
|
||||||
for (const id of ids) {
|
for (const id of ids) {
|
||||||
try {
|
try {
|
||||||
// 检查缓存
|
// 检查缓存
|
||||||
if (!imageIdCache.value[id]) {
|
if (!imageIdCache.value[id]) {
|
||||||
const response = await adminFileApi.getSignedUrl(Number(id))
|
const response = await adminFileApi.getSignedUrl(Number(id)) as unknown as string
|
||||||
imageIdCache.value[id] = response.data
|
imageIdCache.value[id] = response
|
||||||
}
|
}
|
||||||
urls.push(imageIdCache.value[id])
|
urls.push(imageIdCache.value[id])
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -798,54 +805,129 @@ const handleViewModalCancel = () => {
|
|||||||
viewImageUrls.value = []
|
viewImageUrls.value = []
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const syncUploadedFileIds = (fileIds: string[]) => {
|
||||||
|
uploadedFileIds.value = Array.from(new Set(fileIds.filter(id => Boolean(id))))
|
||||||
|
formState.imageFileIds = uploadedFileIds.value.join(',')
|
||||||
|
}
|
||||||
|
|
||||||
|
const getUploadResponse = (file: any) => {
|
||||||
|
if (!file || typeof file !== 'object') {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
return file.response || null
|
||||||
|
}
|
||||||
|
|
||||||
|
const resolveUploadedFileId = (file: any) => {
|
||||||
|
const response = getUploadResponse(file)
|
||||||
|
const responseId = response?.id ?? response?.data?.id ?? file?.fileId
|
||||||
|
if (responseId !== undefined && responseId !== null && responseId !== '') {
|
||||||
|
return String(responseId)
|
||||||
|
}
|
||||||
|
|
||||||
|
const fileUid = String(file?.uid || '')
|
||||||
|
if (/^\d+$/.test(fileUid)) {
|
||||||
|
return fileUid
|
||||||
|
}
|
||||||
|
|
||||||
|
const fileUrl = String(file?.url || '')
|
||||||
|
if (/^\d+$/.test(fileUrl)) {
|
||||||
|
return fileUrl
|
||||||
|
}
|
||||||
|
|
||||||
|
return ''
|
||||||
|
}
|
||||||
|
|
||||||
|
const getPreviewUrlFromFile = (file: any) => {
|
||||||
|
const fileUrl = typeof file?.url === 'string' ? file.url : ''
|
||||||
|
if (fileUrl && !/^\d+$/.test(fileUrl)) {
|
||||||
|
return fileUrl
|
||||||
|
}
|
||||||
|
|
||||||
|
const thumbUrl = typeof file?.thumbUrl === 'string' ? file.thumbUrl : ''
|
||||||
|
if (thumbUrl) {
|
||||||
|
return thumbUrl
|
||||||
|
}
|
||||||
|
|
||||||
|
return ''
|
||||||
|
}
|
||||||
|
|
||||||
const beforeUpload = (file: File) => {
|
const beforeUpload = (file: File) => {
|
||||||
const isImage = file.type.startsWith('image/')
|
const isImage = file.type.startsWith('image/')
|
||||||
if (!isImage) {
|
if (!isImage) {
|
||||||
message.error('只能上传图片文件!')
|
message.error('只能上传图片文件')
|
||||||
}
|
}
|
||||||
const isLt2M = file.size / 1024 / 1024 < 2
|
return isImage
|
||||||
if (!isLt2M) {
|
|
||||||
message.error('图片大小不能超过 2MB!')
|
|
||||||
}
|
|
||||||
return isImage && isLt2M
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleUploadImage = async (options: any) => {
|
const handleUploadImage = async (options: any) => {
|
||||||
const { file, onSuccess, onError } = options
|
const { file, onSuccess, onError } = options
|
||||||
try {
|
try {
|
||||||
const response = await adminFileApi.uploadFile(file) as any
|
const originalFile = file as File
|
||||||
uploadedFileIds.value.push(String(response.id))
|
const compressedResult = await compressImageIfNeeded(originalFile, MAX_IMAGE_UPLOAD_SIZE)
|
||||||
formState.imageFileIds = uploadedFileIds.value.join(',')
|
|
||||||
onSuccess({ url: String(response.id) })
|
if (compressedResult.skippedReason) {
|
||||||
|
throw new Error(compressedResult.skippedReason)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (compressedResult.finalSize > MAX_IMAGE_UPLOAD_SIZE) {
|
||||||
|
throw new Error('图片压缩后仍超过 5MB,请裁剪后重试')
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await adminFileApi.uploadFile(compressedResult.file) as any
|
||||||
|
const fileId = String(response.id)
|
||||||
|
|
||||||
|
syncUploadedFileIds([...uploadedFileIds.value, fileId])
|
||||||
|
onSuccess?.({ id: fileId, fileId })
|
||||||
|
|
||||||
|
if (compressedResult.compressed && compressedResult.originalSize !== compressedResult.finalSize) {
|
||||||
|
message.success(
|
||||||
|
`${originalFile.name} 已压缩并上传成功(${formatFileSize(compressedResult.originalSize)} -> ${formatFileSize(compressedResult.finalSize)})`
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
message.success('图片上传成功')
|
message.success('图片上传成功')
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('图片上传失败:', error)
|
console.error('图片上传失败:', error)
|
||||||
onError(error)
|
onError?.(error)
|
||||||
message.error('图片上传失败')
|
message.error((error as Error)?.message || '图片上传失败')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const handlePreview = async (file: any) => {
|
const handlePreview = async (file: any) => {
|
||||||
try {
|
try {
|
||||||
// 检查缓存
|
const cachedPreviewUrl = getPreviewUrlFromFile(file)
|
||||||
if (!imageIdCache.value[file.uid]) {
|
if (cachedPreviewUrl) {
|
||||||
const response = await adminFileApi.getSignedUrl(Number(file.uid)) as any
|
previewImage.value = cachedPreviewUrl
|
||||||
imageIdCache.value[file.uid] = response
|
previewVisible.value = true
|
||||||
|
return
|
||||||
}
|
}
|
||||||
previewImage.value = imageIdCache.value[file.uid] || ''
|
|
||||||
|
const fileId = resolveUploadedFileId(file)
|
||||||
|
if (!fileId) {
|
||||||
|
throw new Error('未找到图片文件标识')
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!imageIdCache.value[fileId]) {
|
||||||
|
const response = await adminFileApi.getSignedUrl(Number(fileId)) as any
|
||||||
|
imageIdCache.value[fileId] = response
|
||||||
|
}
|
||||||
|
|
||||||
|
previewImage.value = imageIdCache.value[fileId] || ''
|
||||||
previewVisible.value = true
|
previewVisible.value = true
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('获取预览链接失败:', error)
|
console.error('获取预览链接失败:', error)
|
||||||
message.error('获取预览链接失败')
|
message.error((error as Error)?.message || '获取预览链接失败')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleRemove = (file: any) => {
|
const handleRemove = (file: any) => {
|
||||||
const index = uploadedFileIds.value.indexOf(file.uid)
|
const fileId = resolveUploadedFileId(file)
|
||||||
if (index > -1) {
|
if (fileId) {
|
||||||
uploadedFileIds.value.splice(index, 1)
|
syncUploadedFileIds(uploadedFileIds.value.filter(id => id !== fileId))
|
||||||
formState.imageFileIds = uploadedFileIds.value.join(',')
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleModalOk = async () => {
|
const handleModalOk = async () => {
|
||||||
@@ -945,4 +1027,11 @@ watch(() => formState.shopId, (newShopId) => {
|
|||||||
.product-images :deep(.ant-image) {
|
.product-images :deep(.ant-image) {
|
||||||
border-radius: 4px;
|
border-radius: 4px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.upload-tip {
|
||||||
|
color: #8c8c8c;
|
||||||
|
font-size: 12px;
|
||||||
|
line-height: 1.6;
|
||||||
|
margin-top: 8px;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|||||||
@@ -0,0 +1,37 @@
|
|||||||
|
# 2026-03-26 作品图片多选上传与 5MB 压缩
|
||||||
|
|
||||||
|
## 本次调整
|
||||||
|
|
||||||
|
- 后台作品新增/编辑时,作品图片上传改为支持多选。
|
||||||
|
- 单张图片大小限制统一调整为 `5MB`。
|
||||||
|
- 当单张图片超过 `5MB` 时,前端会先自动压缩,再执行上传。
|
||||||
|
- 顺手修复了作品图片在“新上传后预览/删除”场景下依赖 `uid` 导致的文件 ID 识别不稳定问题。
|
||||||
|
|
||||||
|
## 前端修改
|
||||||
|
|
||||||
|
- 新增 [image-upload.ts](/E:/code/xiaowang/Frontend/src/utils/image-upload.ts)
|
||||||
|
- 封装图片大小格式化方法。
|
||||||
|
- 封装浏览器端图片压缩逻辑,优先在原格式内压缩,必要时降尺寸并兜底转为 `JPEG`。
|
||||||
|
- 对不支持自动压缩的超大图片格式返回明确提示,避免静默失败。
|
||||||
|
|
||||||
|
- 调整 [ProductsView.vue](/E:/code/xiaowang/Frontend/src/views/admin/ProductsView.vue)
|
||||||
|
- 作品图片上传组件开启 `multiple`,并限制为图片类型。
|
||||||
|
- 上传提示改为“支持多选上传,单张图片超过 5MB 会自动压缩后再上传”。
|
||||||
|
- 上传前仅校验是否为图片,不再按旧的 `2MB` 直接拦截。
|
||||||
|
- 自定义上传时先执行压缩流程,压缩后仍超过 `5MB` 的图片会明确提示用户裁剪后重试。
|
||||||
|
- 上传成功后改为记录真实文件 ID,并同步维护 `imageFileIds`。
|
||||||
|
- 预览和删除时新增真实文件 ID 解析,兼容:
|
||||||
|
- 已存在作品回填的图片
|
||||||
|
- 新上传图片的接口返回
|
||||||
|
- 本地缩略图预览
|
||||||
|
- 修正作品详情弹窗中图片签名 URL 的读取方式,避免因返回值取错导致图片为空。
|
||||||
|
|
||||||
|
## 影响范围
|
||||||
|
|
||||||
|
- 仅涉及后台作品管理页面上传体验,不改动后端上传接口与数据库结构。
|
||||||
|
- 现有作品图片回显、编辑、预览、删除逻辑保持兼容。
|
||||||
|
|
||||||
|
## 验证
|
||||||
|
|
||||||
|
- 已执行前端构建:`npm run build`
|
||||||
|
- 构建结果通过,`vue-tsc` 与 `vite build` 均成功完成。
|
||||||
Reference in New Issue
Block a user