formModal.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <template>
  2. <div class="modals">
  3. <BasicModal
  4. v-bind="$attrs"
  5. destroyOnClose
  6. @register="registerModal"
  7. :title="getTitle"
  8. @ok="handleSubmit"
  9. :width="880"
  10. @cancel="handleCancel"
  11. >
  12. <div class="!pl-8 !pt-4">
  13. <BasicForm @register="registerForm">
  14. <template #deviceInfo="{ model, field }">
  15. <span>&nbsp;&nbsp;&nbsp;&nbsp;{{ model[field] }}</span>
  16. </template>
  17. </BasicForm>
  18. </div>
  19. </BasicModal>
  20. </div>
  21. </template>
  22. <script lang="ts" setup>
  23. import { ref, computed, unref } from 'vue';
  24. import { BasicModal, useModalInner } from '/@/components/Modal';
  25. import { BasicForm, useForm } from '/@/components/Form';
  26. import { useMessage } from '/@/hooks/web/useMessage';
  27. import { dataFormSchema } from './data';
  28. import {
  29. microbialDetectionWaterAdd,
  30. microbialDetectionWaterEdit,
  31. microbialDetectionWaterDetail,
  32. } from '/@/api/biz/bio/microbialDetectionWaterApi';
  33. import { waterDetail } from '@/api/biz/engineer/waterApi';
  34. const emit = defineEmits(['success', 'cancel', 'register']);
  35. const getTitle = computed(() => (!unref(isUpdate) ? '新增检测' : '编辑检测'));
  36. const isUpdate = ref(false);
  37. const rowId = ref();
  38. const { createMessage } = useMessage();
  39. const [registerForm, { resetFields, validate, setFieldsValue }] = useForm({
  40. layout: 'vertical',
  41. showResetButton: true,
  42. labelWidth: 320,
  43. schemas: dataFormSchema,
  44. showActionButtonGroup: false,
  45. actionColOptions: {
  46. span: 24,
  47. },
  48. baseColProps: {
  49. span: 12,
  50. },
  51. wrapperCol: {
  52. span: 22,
  53. },
  54. });
  55. const [registerModal, { setModalProps, closeModal }] = useModalInner(async data => {
  56. await resetFields();
  57. setModalProps({ confirmLoading: false });
  58. isUpdate.value = !!data?.isUpdate;
  59. if (unref(isUpdate)) {
  60. rowId.value = data.record.id;
  61. const resData = await microbialDetectionWaterDetail(data.record.id);
  62. const deviceInfos = await waterDetail(resData?.deviceId);
  63. resData.deviceInfo = deviceInfos?.name + '(' + deviceInfos?.model + ')';
  64. console.log('resData::::', resData);
  65. await setFieldsValue({
  66. ...resData,
  67. });
  68. }
  69. });
  70. // 提交按钮事件
  71. async function handleSubmit() {
  72. try {
  73. const values = await validate();
  74. setModalProps({ confirmLoading: true });
  75. !unref(isUpdate)
  76. ? await microbialDetectionWaterAdd({ ...values })
  77. : await microbialDetectionWaterEdit({ ...values, id: rowId.value });
  78. !unref(isUpdate) ? createMessage.success('新增成功!') : createMessage.success('编辑成功!');
  79. closeModal();
  80. emit('success', { isUpdate: unref(isUpdate), values: { ...values, id: rowId.value } });
  81. } finally {
  82. setModalProps({ confirmLoading: false, canFullscreen: false });
  83. }
  84. }
  85. async function handleCancel() {
  86. closeModal();
  87. emit('cancel');
  88. }
  89. </script>
  90. <style lang="less" scoped>
  91. ::v-deep(.ant-modal) {
  92. background-color: #000;
  93. }
  94. </style>