恢复后台表单弹窗草稿

This commit is contained in:
2026-07-01 12:49:18 +08:00
parent 975cdb20cb
commit 9f1d2dec77
6 changed files with 409 additions and 28 deletions
+91 -4
View File
@@ -25,10 +25,11 @@
</section>
<BaseModal
v-model="editModalVisible"
:model-value="editModalVisible"
title="个人信息"
:close-disabled="profileSaving || avatarUploading"
:has-unsaved-changes="profileFormChanged"
@update:model-value="updateEditModalVisible"
>
<div class="profile-avatar-row">
<img v-if="profileForm.avatar" class="profile-avatar profile-avatar--image" :src="profileForm.avatar" alt="头像" />
@@ -81,7 +82,7 @@
</template>
<script setup lang="ts">
import { computed, onMounted, reactive, ref } from 'vue'
import { computed, onMounted, reactive, ref, watch } from 'vue'
import { useRouter } from 'vue-router'
import { ChevronRight } from 'lucide-vue-next'
import BaseModal from '@/components/BaseModal.vue'
@@ -92,6 +93,7 @@ import { userApi } from '@/api/user'
import { useSessionStore } from '@/stores/session'
import { useToastStore } from '@/stores/toast'
import type { UserInfo } from '@/types/common'
import { readFormModalDraft, removeFormModalDraft, writeFormModalDraft } from '@/utils/form-modal-draft'
interface ProfileMenu {
title: string
@@ -99,6 +101,21 @@ interface ProfileMenu {
action?: 'editProfile'
}
interface ProfileDraftForm {
nickname: string
avatar: string
contactPhone: string
wechatId: string
signature: string
}
interface ProfileDraftData {
form: ProfileDraftForm
baseline: ProfileDraftForm
}
const PROFILE_EDIT_DRAFT_NAME = 'profile-edit'
const router = useRouter()
const session = useSessionStore()
const toast = useToastStore()
@@ -122,6 +139,69 @@ const profileBaseline = reactive({
signature: ''
})
const snapshotProfileForm = (): ProfileDraftForm => ({
nickname: profileForm.nickname,
avatar: profileForm.avatar,
contactPhone: profileForm.contactPhone,
wechatId: profileForm.wechatId,
signature: profileForm.signature
})
const snapshotProfileBaseline = (): ProfileDraftForm => ({
nickname: profileBaseline.nickname,
avatar: profileBaseline.avatar,
contactPhone: profileBaseline.contactPhone,
wechatId: profileBaseline.wechatId,
signature: profileBaseline.signature
})
const applyProfileForm = (form: ProfileDraftForm): void => {
profileForm.nickname = form.nickname || ''
profileForm.avatar = form.avatar || ''
profileForm.contactPhone = form.contactPhone || ''
profileForm.wechatId = form.wechatId || ''
profileForm.signature = form.signature || ''
}
const applyProfileBaseline = (form: ProfileDraftForm): void => {
profileBaseline.nickname = form.nickname || ''
profileBaseline.avatar = form.avatar || ''
profileBaseline.contactPhone = form.contactPhone || ''
profileBaseline.wechatId = form.wechatId || ''
profileBaseline.signature = form.signature || ''
}
const syncProfileDraft = (): void => {
if (!editModalVisible.value || !profileFormChanged.value) {
return
}
writeFormModalDraft(session.userInfo, PROFILE_EDIT_DRAFT_NAME, {
form: snapshotProfileForm(),
baseline: snapshotProfileBaseline()
}, editModalVisible.value)
}
const restoreProfileDraft = (): void => {
const draft = readFormModalDraft<ProfileDraftData>(session.userInfo, PROFILE_EDIT_DRAFT_NAME)
if (!draft?.open) {
return
}
applyProfileBaseline(draft.form.baseline)
applyProfileForm(draft.form.form)
editModalVisible.value = true
}
const clearProfileDraft = (): void => {
removeFormModalDraft(session.userInfo, PROFILE_EDIT_DRAFT_NAME)
}
const updateEditModalVisible = (value: boolean): void => {
editModalVisible.value = value
if (!value) {
clearProfileDraft()
}
}
const menus: ProfileMenu[] = [
{ title: '个人信息', action: 'editProfile' },
{ title: '店铺设置', url: '/shop-settings' },
@@ -170,6 +250,7 @@ const loadProfile = async (): Promise<void> => {
}
const openEditProfile = (): void => {
clearProfileDraft()
const profile: Partial<UserInfo> = userInfo.value || {}
profileForm.nickname = profile.nickname || ''
profileForm.avatar = profile.avatar || ''
@@ -252,14 +333,20 @@ const saveProfile = async (): Promise<void> => {
})
const profile = await userApi.info()
syncProfileToSession(profile)
editModalVisible.value = false
updateEditModalVisible(false)
toast.success('已保存')
} finally {
profileSaving.value = false
}
}
onMounted(loadProfile)
onMounted(async () => {
await loadProfile()
restoreProfileDraft()
})
watch(profileForm, syncProfileDraft, { deep: true })
watch(editModalVisible, syncProfileDraft)
</script>
<style scoped>