FillRuleModal.vue 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <template>
  2. <BasicModal v-bind="$attrs" @register="registerModal" :title="title" @ok="handleSubmit" :width="800" destroyOnClose>
  3. <BasicForm @register="registerForm" />
  4. </BasicModal>
  5. </template>
  6. <script lang="ts" setup>
  7. import { ref, computed, unref } from 'vue';
  8. import { BasicModal, useModalInner } from '/@/components/Modal';
  9. import { BasicForm, useForm } from '/@/components/Form/index';
  10. import { formSchema } from './fill.rule.data';
  11. import { saveFillRule, updateFillRule } from './fill.rule.api';
  12. //设置标题
  13. const title = computed(() => (!unref(isUpdate) ? '新增' : '编辑'));
  14. // 声明Emits
  15. const emit = defineEmits(['register', 'success']);
  16. const isUpdate = ref(true);
  17. //表单配置
  18. const [registerForm, { resetFields, setFieldsValue, validate, getFieldsValue }] = useForm({
  19. schemas: formSchema,
  20. showActionButtonGroup: false,
  21. baseColProps: { span: 12 },
  22. });
  23. //表单赋值
  24. const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
  25. //重置表单
  26. await resetFields();
  27. setModalProps({ confirmLoading: false });
  28. isUpdate.value = !!data?.isUpdate;
  29. if (unref(isUpdate)) {
  30. //表单赋值
  31. await setFieldsValue({
  32. ...data.record,
  33. });
  34. }
  35. });
  36. //表单提交事件
  37. async function handleSubmit() {
  38. try {
  39. let formValue = await validate();
  40. setModalProps({ confirmLoading: true });
  41. if (isUpdate.value) {
  42. let allFieldsValue = getFieldsValue();
  43. // 编辑页面 如果表单没有父级下拉框 则提交时候 validate方法不返该值 需要手动设置
  44. if (!formValue.parentId && allFieldsValue.parentId) {
  45. formValue.parentId = allFieldsValue.parentId;
  46. }
  47. await updateFillRule(formValue);
  48. } else {
  49. await saveFillRule(formValue);
  50. }
  51. //关闭弹窗
  52. closeModal();
  53. //刷新列表
  54. emit('success');
  55. } finally {
  56. setModalProps({ confirmLoading: false });
  57. }
  58. }
  59. </script>