refactor: 通知公告 useBeforeCloseDiff
This commit is contained in:
parent
5510b6dea4
commit
f7128b099e
@ -18,6 +18,7 @@ import { pick } from 'lodash-es';
|
|||||||
import { noticeAdd, noticeInfo, noticeUpdate } from '#/api/system/notice';
|
import { noticeAdd, noticeInfo, noticeUpdate } from '#/api/system/notice';
|
||||||
import { Tinymce } from '#/components/tinymce';
|
import { Tinymce } from '#/components/tinymce';
|
||||||
import { getDictOptions } from '#/utils/dict';
|
import { getDictOptions } from '#/utils/dict';
|
||||||
|
import { useBeforeCloseDiff } from '#/utils/popup';
|
||||||
|
|
||||||
const emit = defineEmits<{ reload: [] }>();
|
const emit = defineEmits<{ reload: [] }>();
|
||||||
|
|
||||||
@ -74,17 +75,29 @@ const { validate, validateInfos, resetFields } = Form.useForm(
|
|||||||
formRules,
|
formRules,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
function customFormValueGetter() {
|
||||||
|
return JSON.stringify(formData.value);
|
||||||
|
}
|
||||||
|
|
||||||
|
const { onBeforeClose, markInitialized, resetInitialized } = useBeforeCloseDiff(
|
||||||
|
{
|
||||||
|
initializedGetter: customFormValueGetter,
|
||||||
|
currentGetter: customFormValueGetter,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
const [BasicModal, modalApi] = useVbenModal({
|
const [BasicModal, modalApi] = useVbenModal({
|
||||||
class: 'w-[800px]',
|
class: 'w-[800px]',
|
||||||
fullscreenButton: true,
|
fullscreenButton: true,
|
||||||
closeOnClickModal: false,
|
onBeforeClose,
|
||||||
onClosed: handleCancel,
|
onClosed: handleClosed,
|
||||||
onConfirm: handleConfirm,
|
onConfirm: handleConfirm,
|
||||||
onOpenChange: async (isOpen) => {
|
onOpenChange: async (isOpen) => {
|
||||||
if (!isOpen) {
|
if (!isOpen) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
modalApi.modalLoading(true);
|
modalApi.modalLoading(true);
|
||||||
|
|
||||||
const { id } = modalApi.getData() as { id?: number | string };
|
const { id } = modalApi.getData() as { id?: number | string };
|
||||||
isUpdate.value = !!id;
|
isUpdate.value = !!id;
|
||||||
if (isUpdate.value && id) {
|
if (isUpdate.value && id) {
|
||||||
@ -93,30 +106,33 @@ const [BasicModal, modalApi] = useVbenModal({
|
|||||||
const filterRecord = pick(record, Object.keys(defaultValues));
|
const filterRecord = pick(record, Object.keys(defaultValues));
|
||||||
formData.value = filterRecord;
|
formData.value = filterRecord;
|
||||||
}
|
}
|
||||||
|
await markInitialized();
|
||||||
|
|
||||||
modalApi.modalLoading(false);
|
modalApi.modalLoading(false);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
async function handleConfirm() {
|
async function handleConfirm() {
|
||||||
try {
|
try {
|
||||||
modalApi.modalLoading(true);
|
modalApi.lock(true);
|
||||||
await validate();
|
await validate();
|
||||||
// 可能会做数据处理 使用cloneDeep深拷贝
|
// 可能会做数据处理 使用cloneDeep深拷贝
|
||||||
const data = cloneDeep(formData.value);
|
const data = cloneDeep(formData.value);
|
||||||
await (isUpdate.value ? noticeUpdate(data) : noticeAdd(data));
|
await (isUpdate.value ? noticeUpdate(data) : noticeAdd(data));
|
||||||
|
resetInitialized();
|
||||||
emit('reload');
|
emit('reload');
|
||||||
await handleCancel();
|
modalApi.close();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
} finally {
|
} finally {
|
||||||
modalApi.modalLoading(false);
|
modalApi.lock(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleCancel() {
|
async function handleClosed() {
|
||||||
modalApi.close();
|
|
||||||
formData.value = defaultValues;
|
formData.value = defaultValues;
|
||||||
resetFields();
|
resetFields();
|
||||||
|
resetInitialized();
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user