formModal.vue 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <template>
  2. <BasicModal
  3. v-bind="$attrs"
  4. destroyOnClose
  5. @register="registerModal"
  6. :title="getTitle"
  7. :width="width"
  8. @ok="handleSubmit"
  9. :showFooter="true"
  10. >
  11. <BasicForm @register="registerForm" layout="vertical" />
  12. </BasicModal>
  13. </template>
  14. <script lang="ts" setup>
  15. import { ref, computed, unref } from 'vue';
  16. import { BasicModal } from '/@/components/Modal';
  17. import { useModalInner } from '/@/components/Modal';
  18. import { BasicForm, useForm } from '/@/components/Form';
  19. import { useMessage } from '/@/hooks/web/useMessage';
  20. import { dataFormSchema } from './data';
  21. import {
  22. archivesPatrolWardAdd,
  23. archivesPatrolWardEdit,
  24. archivesPatrolWardDetail,
  25. } from '/@/api/biz/archives/patrolWardApi';
  26. const emit = defineEmits(['success', 'register']);
  27. const getTitle = computed(() => (!unref(isUpdate) ? '新增查房' : '编辑查房'));
  28. const width = '45%';
  29. const isUpdate = ref(false);
  30. const rowId = ref();
  31. const { createMessage } = useMessage();
  32. const [registerForm, { setFieldsValue, resetFields, validate }] = useForm({
  33. labelWidth: 100,
  34. schemas: dataFormSchema,
  35. showActionButtonGroup: false,
  36. actionColOptions: {
  37. span: 23,
  38. },
  39. });
  40. const [registerModal, { setModalProps, closeModal }] = useModalInner(async data => {
  41. await resetFields();
  42. setModalProps({ confirmLoading: false });
  43. isUpdate.value = !!data?.isUpdate;
  44. if (unref(isUpdate)) {
  45. const resData = await archivesPatrolWardDetail(data.record.id);
  46. rowId.value = resData.id;
  47. await setFieldsValue({
  48. ...resData,
  49. });
  50. }
  51. });
  52. // 提交按钮事件
  53. async function handleSubmit() {
  54. try {
  55. const values = await validate();
  56. setModalProps({ confirmLoading: true });
  57. !unref(isUpdate)
  58. ? await archivesPatrolWardAdd({ ...values })
  59. : await archivesPatrolWardEdit({ ...values, id: rowId.value });
  60. !unref(isUpdate) ? createMessage.success('新增成功!') : createMessage.success('编辑成功!');
  61. closeModal();
  62. emit('success', {
  63. isUpdate: unref(isUpdate),
  64. values: { ...values, id: rowId.value },
  65. });
  66. } finally {
  67. setModalProps({ confirmLoading: false });
  68. }
  69. }
  70. </script>