功能:为编辑表单增加前端暂存
This commit is contained in:
@@ -127,6 +127,12 @@
|
||||
:confirmLoading="saving"
|
||||
:ok-button-props="{ disabled: avatarUploadPending }"
|
||||
>
|
||||
<DraftActions
|
||||
:saved-at="draftSavedAt"
|
||||
:disabled="avatarUploadPending"
|
||||
@save="handleSaveDraft"
|
||||
@clear="handleClearDraft"
|
||||
/>
|
||||
<a-form layout="vertical">
|
||||
<a-form-item label="头像">
|
||||
<div class="avatar-upload-section">
|
||||
@@ -198,12 +204,14 @@ import {
|
||||
} from '@ant-design/icons-vue'
|
||||
import { message, Modal } from 'ant-design-vue'
|
||||
import type { UploadProps } from 'ant-design-vue'
|
||||
import DraftActions from '@/components/DraftActions.vue'
|
||||
import { authApi } from '@/api/auth'
|
||||
import { couponApi } from '@/api/coupon'
|
||||
import { fileApi } from '@/api/file'
|
||||
import { orderApi } from '@/api/order'
|
||||
import { userApi, type UserVO } from '@/api/user'
|
||||
import { createChunkUploadTask, type ChunkUploadTask } from '@/utils/chunk-upload'
|
||||
import { buildDraftStorageKey, clearDraftRecord, loadDraftRecord, saveDraftRecord } from '@/utils/form-draft'
|
||||
import { wishlistApi } from '@/api/wishlist'
|
||||
import { DEFAULT_AVATAR } from '@/constants'
|
||||
|
||||
@@ -233,6 +241,7 @@ const selectedShopId = ref<string>()
|
||||
const avatarUploadProgress = ref(0)
|
||||
const avatarUploadPending = ref(false)
|
||||
let avatarUploadTask: ChunkUploadTask<string> | null = null
|
||||
const draftSavedAt = ref<number | null>(null)
|
||||
|
||||
const editForm = reactive({
|
||||
nickname: '',
|
||||
@@ -240,6 +249,43 @@ const editForm = reactive({
|
||||
signature: ''
|
||||
})
|
||||
|
||||
const getDraftKey = () => buildDraftStorageKey('profile-edit')
|
||||
|
||||
const buildDraftPayload = () => ({
|
||||
nickname: editForm.nickname,
|
||||
avatar: editForm.avatar,
|
||||
signature: editForm.signature
|
||||
})
|
||||
|
||||
const restoreDraft = () => {
|
||||
const draft = loadDraftRecord<ReturnType<typeof buildDraftPayload>>(getDraftKey())
|
||||
if (!draft) {
|
||||
draftSavedAt.value = null
|
||||
return
|
||||
}
|
||||
Object.assign(editForm, draft.payload)
|
||||
draftSavedAt.value = draft.savedAt
|
||||
message.success('已恢复暂存内容')
|
||||
}
|
||||
|
||||
const handleSaveDraft = () => {
|
||||
if (avatarUploadPending.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 shopOptions = computed(() => {
|
||||
const ids = userInfo.value.shopIds || []
|
||||
const names = userInfo.value.shopNames || []
|
||||
@@ -345,6 +391,7 @@ const handleEditProfile = () => {
|
||||
editForm.nickname = userInfo.value.nickname || ''
|
||||
editForm.avatar = userInfo.value.avatar || ''
|
||||
editForm.signature = userInfo.value.signature || ''
|
||||
restoreDraft()
|
||||
editModalVisible.value = true
|
||||
}
|
||||
|
||||
@@ -445,6 +492,7 @@ const handleSaveProfile = async () => {
|
||||
signature: editForm.signature
|
||||
})
|
||||
message.success('资料更新成功')
|
||||
handleClearDraft(false)
|
||||
editModalVisible.value = false
|
||||
await fetchUserInfo()
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user