功能:为编辑表单增加前端暂存
This commit is contained in:
@@ -10,6 +10,11 @@
|
||||
|
||||
<div class="password-form">
|
||||
<a-card :bordered="false" class="form-card">
|
||||
<DraftActions
|
||||
:saved-at="draftSavedAt"
|
||||
@save="handleSaveDraft"
|
||||
@clear="handleClearDraft"
|
||||
/>
|
||||
<a-form
|
||||
ref="formRef"
|
||||
:model="formState"
|
||||
@@ -92,13 +97,16 @@ import { LeftOutlined, LockOutlined } from '@ant-design/icons-vue'
|
||||
import { message } from 'ant-design-vue'
|
||||
import type { FormInstance } from 'ant-design-vue'
|
||||
import type { Rule } from 'ant-design-vue/es/form'
|
||||
import DraftActions from '@/components/DraftActions.vue'
|
||||
import { userApi } from '@/api/user'
|
||||
import { buildDraftStorageKey, clearDraftRecord, loadDraftRecord, saveDraftRecord } from '@/utils/form-draft'
|
||||
|
||||
const router = useRouter()
|
||||
const formRef = ref<FormInstance>()
|
||||
const loading = ref(false)
|
||||
const countdown = ref(0)
|
||||
const userInfo = ref<{ phone?: string } | null>(null)
|
||||
const draftSavedAt = ref<number | null>(null)
|
||||
let countdownTimer: ReturnType<typeof setInterval> | null = null
|
||||
|
||||
const formState = reactive({
|
||||
@@ -107,6 +115,33 @@ const formState = reactive({
|
||||
confirmPassword: ''
|
||||
})
|
||||
|
||||
const draftKey = buildDraftStorageKey('password-settings', 'set-password')
|
||||
|
||||
const restoreDraft = () => {
|
||||
const draft = loadDraftRecord<typeof formState>(draftKey, 'session')
|
||||
if (!draft) {
|
||||
draftSavedAt.value = null
|
||||
return
|
||||
}
|
||||
Object.assign(formState, draft.payload)
|
||||
draftSavedAt.value = draft.savedAt
|
||||
message.success('已恢复暂存内容')
|
||||
}
|
||||
|
||||
const handleSaveDraft = () => {
|
||||
const draft = saveDraftRecord(draftKey, { ...formState }, 'session')
|
||||
draftSavedAt.value = draft.savedAt
|
||||
message.success('已暂存到当前会话')
|
||||
}
|
||||
|
||||
const handleClearDraft = (showMessage = true) => {
|
||||
clearDraftRecord(draftKey, 'session')
|
||||
draftSavedAt.value = null
|
||||
if (showMessage) {
|
||||
message.success('已清除暂存')
|
||||
}
|
||||
}
|
||||
|
||||
const userPhone = computed(() => userInfo.value?.phone || '')
|
||||
|
||||
const validateConfirmPassword = (_rule: any, value: string) => {
|
||||
@@ -189,6 +224,7 @@ const handleSubmit = async () => {
|
||||
})
|
||||
|
||||
message.success('密码设置成功')
|
||||
handleClearDraft(false)
|
||||
localStorage.removeItem('needSetPassword')
|
||||
router.push('/profile')
|
||||
} catch (error: any) {
|
||||
@@ -205,6 +241,7 @@ const handleBackClick = () => {
|
||||
|
||||
onMounted(() => {
|
||||
fetchUserInfo()
|
||||
restoreDraft()
|
||||
})
|
||||
|
||||
onUnmounted(() => {
|
||||
|
||||
Reference in New Issue
Block a user