FormModal.vue.vm 2.1 KB

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