功能:为编辑表单增加前端暂存
This commit is contained in:
@@ -103,6 +103,11 @@
|
||||
@ok="handleModalOk"
|
||||
@cancel="handleModalCancel"
|
||||
>
|
||||
<DraftActions
|
||||
:saved-at="draftSavedAt"
|
||||
@save="handleSaveDraft"
|
||||
@clear="handleClearDraft"
|
||||
/>
|
||||
<a-form
|
||||
ref="formRef"
|
||||
:model="formState"
|
||||
@@ -264,6 +269,7 @@ import { computed, onMounted, reactive, ref, watch } from 'vue'
|
||||
import { message, Modal } 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 dayjs, { type Dayjs } from 'dayjs'
|
||||
import {
|
||||
adminCouponApi,
|
||||
@@ -275,6 +281,7 @@ import {
|
||||
type Tag,
|
||||
type UserVO
|
||||
} from '@/api/admin'
|
||||
import { buildDraftStorageKey, clearDraftRecord, loadDraftRecord, saveDraftRecord } from '@/utils/form-draft'
|
||||
import { usePermission } from '@/utils/permission'
|
||||
|
||||
const { hasPermission, loadUserPermissions } = usePermission()
|
||||
@@ -317,6 +324,7 @@ const searchText = ref('')
|
||||
const formRef = ref<FormInstance>()
|
||||
const distributeFormRef = ref<FormInstance>()
|
||||
const currentCoupon = ref<Coupon | null>(null)
|
||||
const draftSavedAt = ref<number | null>(null)
|
||||
const couponList = ref<Coupon[]>([])
|
||||
const tagList = ref<Tag[]>([])
|
||||
const userList = ref<UserVO[]>([])
|
||||
@@ -444,6 +452,56 @@ const resetFormState = () => {
|
||||
})
|
||||
}
|
||||
|
||||
const getDraftKey = () => buildDraftStorageKey('admin-coupons', isEdit.value ? 'edit' : 'create', currentCoupon.value?.id ?? 'new')
|
||||
|
||||
const buildDraftPayload = () => ({
|
||||
couponCode: formState.couponCode,
|
||||
couponName: formState.couponName,
|
||||
couponType: formState.couponType,
|
||||
discountValue: formState.discountValue,
|
||||
minAmount: formState.minAmount,
|
||||
reduceAmount: formState.reduceAmount,
|
||||
totalCount: formState.totalCount,
|
||||
startTime: formState.startTime?.format('YYYY-MM-DD HH:mm:ss'),
|
||||
endTime: formState.endTime?.format('YYYY-MM-DD HH:mm:ss'),
|
||||
availableTagIds: [...formState.availableTagIds],
|
||||
unavailableTagIds: [...formState.unavailableTagIds],
|
||||
shopId: formState.shopId,
|
||||
couponScene: formState.couponScene,
|
||||
requiredVipLevel: formState.requiredVipLevel,
|
||||
autoMatch: formState.autoMatch
|
||||
})
|
||||
|
||||
const restoreDraft = () => {
|
||||
const draft = loadDraftRecord<ReturnType<typeof buildDraftPayload>>(getDraftKey())
|
||||
if (!draft) {
|
||||
draftSavedAt.value = null
|
||||
return
|
||||
}
|
||||
|
||||
Object.assign(formState, {
|
||||
...draft.payload,
|
||||
startTime: draft.payload.startTime ? dayjs(draft.payload.startTime) : undefined,
|
||||
endTime: draft.payload.endTime ? dayjs(draft.payload.endTime) : undefined
|
||||
})
|
||||
draftSavedAt.value = draft.savedAt
|
||||
message.success('已恢复暂存内容')
|
||||
}
|
||||
|
||||
const handleSaveDraft = () => {
|
||||
const draft = saveDraftRecord(getDraftKey(), buildDraftPayload())
|
||||
draftSavedAt.value = draft.savedAt
|
||||
message.success('已暂存到本地')
|
||||
}
|
||||
|
||||
const handleClearDraft = (showMessage = true) => {
|
||||
clearDraftRecord(getDraftKey())
|
||||
draftSavedAt.value = null
|
||||
if (showMessage) {
|
||||
message.success('已清除暂存')
|
||||
}
|
||||
}
|
||||
|
||||
watch(
|
||||
() => formState.couponScene,
|
||||
value => {
|
||||
@@ -533,6 +591,7 @@ const showAddModal = () => {
|
||||
isEdit.value = false
|
||||
currentCoupon.value = null
|
||||
resetFormState()
|
||||
restoreDraft()
|
||||
modalVisible.value = true
|
||||
}
|
||||
|
||||
@@ -556,6 +615,7 @@ const showEditModal = (record: any) => {
|
||||
requiredVipLevel: record.requiredVipLevel || undefined,
|
||||
autoMatch: record.autoMatch || 0
|
||||
})
|
||||
restoreDraft()
|
||||
modalVisible.value = true
|
||||
}
|
||||
|
||||
@@ -596,6 +656,7 @@ const handleModalOk = async () => {
|
||||
message.success('创建成功')
|
||||
}
|
||||
|
||||
handleClearDraft(false)
|
||||
modalVisible.value = false
|
||||
formRef.value?.resetFields()
|
||||
await fetchCoupons()
|
||||
|
||||
Reference in New Issue
Block a user