功能:为编辑表单增加前端暂存
This commit is contained in:
@@ -105,6 +105,12 @@
|
||||
:confirmLoading="modalLoading"
|
||||
:ok-button-props="{ disabled: isUploadPending }"
|
||||
>
|
||||
<DraftActions
|
||||
:saved-at="draftSavedAt"
|
||||
:disabled="isUploadPending"
|
||||
@save="handleSaveDraft"
|
||||
@clear="handleClearDraft"
|
||||
/>
|
||||
<a-form
|
||||
ref="formRef"
|
||||
:model="formState"
|
||||
@@ -268,9 +274,11 @@ import { computed, ref, reactive, onMounted } from 'vue'
|
||||
import { message } from 'ant-design-vue'
|
||||
import type { FormInstance } from 'ant-design-vue'
|
||||
import { PlusOutlined } from '@ant-design/icons-vue'
|
||||
import DraftActions from '@/components/DraftActions.vue'
|
||||
import { DEFAULT_SHOP_IMAGE } from '@/constants'
|
||||
import { adminShopApi, adminFileApi, type Shop } 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'
|
||||
|
||||
const { hasPermission, loadUserPermissions } = usePermission()
|
||||
@@ -307,6 +315,7 @@ const businessEndTime = ref('')
|
||||
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({
|
||||
@@ -692,8 +701,9 @@ const handleBusinessHoursChange = async () => {
|
||||
await formRef.value?.validateFields?.(['businessHours']).catch(() => undefined)
|
||||
}
|
||||
|
||||
const showAddModal = () => {
|
||||
const showAddModal = async () => {
|
||||
isEdit.value = false
|
||||
currentShop.value = null
|
||||
Object.assign(formState, {
|
||||
shopName: '',
|
||||
description: '',
|
||||
@@ -708,6 +718,7 @@ const showAddModal = () => {
|
||||
syncUploadedBannerIds([])
|
||||
imageFileList.value = []
|
||||
syncUploadedFileIds([])
|
||||
await restoreDraft()
|
||||
modalVisible.value = true
|
||||
}
|
||||
|
||||
@@ -730,35 +741,7 @@ const showEditModal = async (record: Shop | Record<string, any>) => {
|
||||
if (coverImageFileId) {
|
||||
const ids = coverImageFileId.split(',').filter((id: string) => id)
|
||||
syncUploadedBannerIds(ids)
|
||||
|
||||
// 加载所有图片签名链接
|
||||
const fileList = []
|
||||
for (const [index, id] of ids.entries()) {
|
||||
try {
|
||||
// 检查缓存
|
||||
if (!imageIdCache.value[id]) {
|
||||
const response = await adminFileApi.getSignedUrl(Number(id || '0')) as any
|
||||
imageIdCache.value[id] = response
|
||||
}
|
||||
fileList.push({
|
||||
uid: id,
|
||||
fileId: id,
|
||||
name: `Banner${index + 1}`,
|
||||
status: 'done',
|
||||
url: imageIdCache.value[id]
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('获取签名链接失败:', error)
|
||||
fileList.push({
|
||||
uid: id,
|
||||
fileId: id,
|
||||
name: `Banner${index + 1}`,
|
||||
status: 'done',
|
||||
url: ''
|
||||
})
|
||||
}
|
||||
}
|
||||
bannerFileList.value = fileList
|
||||
bannerFileList.value = await buildShopUploadFileList(ids, 'Banner')
|
||||
} else {
|
||||
bannerFileList.value = []
|
||||
syncUploadedBannerIds([])
|
||||
@@ -769,40 +752,13 @@ const showEditModal = async (record: Shop | Record<string, any>) => {
|
||||
if (imageFileIds) {
|
||||
const ids = imageFileIds.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 || '0')) 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 buildShopUploadFileList(ids, '图片')
|
||||
} else {
|
||||
imageFileList.value = []
|
||||
syncUploadedFileIds([])
|
||||
}
|
||||
|
||||
await restoreDraft()
|
||||
modalVisible.value = true
|
||||
}
|
||||
|
||||
@@ -878,6 +834,89 @@ const cancelAllPendingUploads = () => {
|
||||
uploadTaskMap.forEach(task => task.cancel())
|
||||
}
|
||||
|
||||
const getDraftKey = () => buildDraftStorageKey('admin-shops', isEdit.value ? 'edit' : 'create', currentShop.value?.id ?? 'new')
|
||||
|
||||
const buildDraftPayload = () => ({
|
||||
formState: {
|
||||
shopName: formState.shopName,
|
||||
description: formState.description,
|
||||
address: formState.address,
|
||||
businessHours: formState.businessHours,
|
||||
status: formState.status,
|
||||
coverImageFileId: formState.coverImageFileId,
|
||||
imageFileIds: formState.imageFileIds
|
||||
},
|
||||
businessStartTime: businessStartTime.value,
|
||||
businessEndTime: businessEndTime.value,
|
||||
uploadedBannerIds: [...uploadedBannerIds.value],
|
||||
uploadedFileIds: [...uploadedFileIds.value]
|
||||
})
|
||||
|
||||
const buildShopUploadFileList = async (ids: string[], fileNamePrefix: string) => {
|
||||
const fileList = []
|
||||
for (const [index, id] of ids.entries()) {
|
||||
try {
|
||||
if (!imageIdCache.value[id]) {
|
||||
const response = await adminFileApi.getSignedUrl(Number(id || '0')) as any
|
||||
imageIdCache.value[id] = response
|
||||
}
|
||||
fileList.push({
|
||||
uid: id,
|
||||
fileId: id,
|
||||
name: `${fileNamePrefix}${index + 1}`,
|
||||
status: 'done',
|
||||
url: imageIdCache.value[id]
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('获取签名链接失败:', error)
|
||||
fileList.push({
|
||||
uid: id,
|
||||
fileId: id,
|
||||
name: `${fileNamePrefix}${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)
|
||||
businessStartTime.value = draft.payload.businessStartTime || ''
|
||||
businessEndTime.value = draft.payload.businessEndTime || ''
|
||||
syncUploadedBannerIds(draft.payload.uploadedBannerIds || [])
|
||||
syncUploadedFileIds(draft.payload.uploadedFileIds || [])
|
||||
bannerFileList.value = await buildShopUploadFileList(uploadedBannerIds.value, 'Banner')
|
||||
imageFileList.value = await buildShopUploadFileList(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 syncUploadedBannerIds = (fileIds: string[]) => {
|
||||
uploadedBannerIds.value = Array.from(new Set(fileIds.filter(id => Boolean(id))))
|
||||
formState.coverImageFileId = uploadedBannerIds.value.join(',')
|
||||
@@ -947,6 +986,7 @@ const handleModalOk = async () => {
|
||||
message.success('创建成功')
|
||||
}
|
||||
|
||||
handleClearDraft(false)
|
||||
modalVisible.value = false
|
||||
fetchShopList()
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user