功能:为编辑表单增加前端暂存
This commit is contained in:
@@ -91,6 +91,12 @@
|
||||
:confirmLoading="modalLoading"
|
||||
:ok-button-props="{ disabled: isUploadPending }"
|
||||
>
|
||||
<DraftActions
|
||||
:saved-at="draftSavedAt"
|
||||
:disabled="isUploadPending"
|
||||
@save="handleSaveDraft"
|
||||
@clear="handleClearDraft"
|
||||
/>
|
||||
<a-form
|
||||
ref="formRef"
|
||||
:model="formState"
|
||||
@@ -367,9 +373,11 @@
|
||||
import { computed, ref, reactive, onMounted, watch } from 'vue'
|
||||
import { message } from 'ant-design-vue'
|
||||
import { PlusOutlined } from '@ant-design/icons-vue'
|
||||
import DraftActions from '@/components/DraftActions.vue'
|
||||
import { DEFAULT_PRODUCT_IMAGE } from '@/constants'
|
||||
import { adminProductApi, type Product, adminShopApi, type Shop, adminFileApi, adminTagApi, adminArtistApi, adminCouponApi } from '@/api/admin'
|
||||
import { createChunkUploadTask, type ChunkUploadTask } from '@/utils/chunk-upload'
|
||||
import { buildDraftStorageKey, clearDraftRecord, loadDraftRecord, saveDraftRecord } from '@/utils/form-draft'
|
||||
import { usePermission } from '@/utils/permission'
|
||||
import { MAX_IMAGE_UPLOAD_SIZE, compressImageIfNeeded, formatFileSize } from '@/utils/image-upload'
|
||||
|
||||
@@ -403,6 +411,7 @@ const MAX_PRODUCT_IMAGE_COUNT = 5
|
||||
const uploadTaskMap = new Map<string, ChunkUploadTask<any>>()
|
||||
const activeUploadCount = ref(0)
|
||||
const isUploadPending = computed(() => activeUploadCount.value > 0)
|
||||
const draftSavedAt = ref<number | null>(null)
|
||||
|
||||
const viewFormState = reactive({
|
||||
shopId: undefined,
|
||||
@@ -639,8 +648,9 @@ const handleTableChange = (pag: any) => {
|
||||
fetchProductList()
|
||||
}
|
||||
|
||||
const showAddModal = () => {
|
||||
const showAddModal = async () => {
|
||||
isEdit.value = false
|
||||
currentProduct.value = null
|
||||
const currentUser = getCurrentUserInfo()
|
||||
const shopId = (currentUser && currentUser.type !== 4 && currentUser.shopId) ? currentUser.shopId : undefined
|
||||
Object.assign(formState, {
|
||||
@@ -660,6 +670,7 @@ const showAddModal = () => {
|
||||
if (shopId) {
|
||||
fetchArtists(shopId)
|
||||
}
|
||||
await restoreDraft()
|
||||
modalVisible.value = true
|
||||
}
|
||||
|
||||
@@ -701,35 +712,7 @@ const showEditModal = async (record: Product | Record<string, any>) => {
|
||||
if (recordImageFileIds) {
|
||||
const ids = recordImageFileIds.split(',').filter((id: string) => id)
|
||||
syncUploadedFileIds(ids)
|
||||
|
||||
// 加载所有图片签名链接
|
||||
const fileList = []
|
||||
for (const [index, id] of ids.entries()) {
|
||||
try {
|
||||
// 检查缓存
|
||||
if (!imageIdCache.value[id]) {
|
||||
const response = await adminFileApi.getSignedUrl(Number(id)) as any
|
||||
imageIdCache.value[id] = response
|
||||
}
|
||||
fileList.push({
|
||||
uid: id,
|
||||
fileId: id,
|
||||
name: `图片${index + 1}`,
|
||||
status: 'done',
|
||||
url: imageIdCache.value[id]
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('获取签名链接失败:', error)
|
||||
fileList.push({
|
||||
uid: id,
|
||||
fileId: id,
|
||||
name: `图片${index + 1}`,
|
||||
status: 'done',
|
||||
url: ''
|
||||
})
|
||||
}
|
||||
}
|
||||
imageFileList.value = fileList
|
||||
imageFileList.value = await buildImageFileListByIds(ids)
|
||||
} else {
|
||||
imageFileList.value = []
|
||||
syncUploadedFileIds([])
|
||||
@@ -739,6 +722,7 @@ const showEditModal = async (record: Product | Record<string, any>) => {
|
||||
if (recordShopId) {
|
||||
await fetchArtists(recordShopId)
|
||||
}
|
||||
await restoreDraft()
|
||||
|
||||
modalVisible.value = true
|
||||
}
|
||||
@@ -815,6 +799,84 @@ const cancelAllPendingUploads = () => {
|
||||
uploadTaskMap.forEach(task => task.cancel())
|
||||
}
|
||||
|
||||
const getDraftKey = () => buildDraftStorageKey('admin-products', isEdit.value ? 'edit' : 'create', currentProduct.value?.id ?? 'new')
|
||||
|
||||
const buildDraftPayload = () => ({
|
||||
formState: {
|
||||
shopId: formState.shopId,
|
||||
title: formState.title,
|
||||
description: formState.description,
|
||||
imageFileIds: formState.imageFileIds,
|
||||
price: formState.price,
|
||||
tags: [...formState.tags],
|
||||
approximateDuration: formState.approximateDuration,
|
||||
bookableArtists: [...formState.bookableArtists],
|
||||
availableCoupons: [...formState.availableCoupons]
|
||||
},
|
||||
uploadedFileIds: [...uploadedFileIds.value]
|
||||
})
|
||||
|
||||
const buildImageFileListByIds = async (ids: string[]) => {
|
||||
const fileList = []
|
||||
for (const [index, id] of ids.entries()) {
|
||||
try {
|
||||
if (!imageIdCache.value[id]) {
|
||||
const response = await adminFileApi.getSignedUrl(Number(id)) as any
|
||||
imageIdCache.value[id] = response
|
||||
}
|
||||
fileList.push({
|
||||
uid: id,
|
||||
fileId: id,
|
||||
name: `图片${index + 1}`,
|
||||
status: 'done',
|
||||
url: imageIdCache.value[id]
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('获取签名链接失败:', error)
|
||||
fileList.push({
|
||||
uid: id,
|
||||
fileId: id,
|
||||
name: `图片${index + 1}`,
|
||||
status: 'done',
|
||||
url: ''
|
||||
})
|
||||
}
|
||||
}
|
||||
return fileList
|
||||
}
|
||||
|
||||
const restoreDraft = async () => {
|
||||
const draft = loadDraftRecord<ReturnType<typeof buildDraftPayload>>(getDraftKey())
|
||||
if (!draft) {
|
||||
draftSavedAt.value = null
|
||||
return
|
||||
}
|
||||
|
||||
Object.assign(formState, draft.payload.formState)
|
||||
syncUploadedFileIds(draft.payload.uploadedFileIds || [])
|
||||
imageFileList.value = await buildImageFileListByIds(uploadedFileIds.value)
|
||||
draftSavedAt.value = draft.savedAt
|
||||
message.success('已恢复暂存内容')
|
||||
}
|
||||
|
||||
const handleSaveDraft = () => {
|
||||
if (isUploadPending.value) {
|
||||
message.warning('图片上传完成后才能暂存')
|
||||
return
|
||||
}
|
||||
const draft = saveDraftRecord(getDraftKey(), buildDraftPayload())
|
||||
draftSavedAt.value = draft.savedAt
|
||||
message.success('已暂存到本地')
|
||||
}
|
||||
|
||||
const handleClearDraft = (showMessage = true) => {
|
||||
clearDraftRecord(getDraftKey())
|
||||
draftSavedAt.value = null
|
||||
if (showMessage) {
|
||||
message.success('已清除暂存')
|
||||
}
|
||||
}
|
||||
|
||||
const syncUploadedFileIds = (fileIds: string[]) => {
|
||||
uploadedFileIds.value = Array.from(new Set(fileIds.filter(id => Boolean(id))))
|
||||
formState.imageFileIds = uploadedFileIds.value.join(',')
|
||||
@@ -1003,7 +1065,8 @@ const handleModalOk = async () => {
|
||||
await adminProductApi.createProduct(requestData)
|
||||
message.success('创建成功')
|
||||
}
|
||||
|
||||
|
||||
handleClearDraft(false)
|
||||
modalVisible.value = false
|
||||
fetchProductList()
|
||||
} catch (error: any) {
|
||||
|
||||
Reference in New Issue
Block a user