bannerModal.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <template>
  2. <BasicModal v-bind="$attrs" @register="registerModal" destroyOnClose :title="title" :width="800" @ok="handleSubmit">
  3. <BasicForm @register="registerForm" name="bannerForm" />
  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 '../banner.data';
  11. import { saveOrUpdate } from '../banner.api';
  12. import { useMessage } from '/@/hooks/web/useMessage';
  13. const { createMessage } = useMessage();
  14. // Emits声明
  15. const emit = defineEmits(['register', 'success']);
  16. const isUpdate = ref(true);
  17. const isDetail = ref(false);
  18. //表单配置
  19. const [registerForm, { setProps, resetFields, setFieldsValue, validate, scrollToField }] = useForm({
  20. labelWidth: 150,
  21. schemas: formSchema,
  22. showActionButtonGroup: false,
  23. baseColProps: { span: 24 },
  24. });
  25. //表单赋值
  26. const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
  27. //重置表单
  28. await resetFields();
  29. setModalProps({ confirmLoading: false, showCancelBtn: !!data?.showFooter, showOkBtn: !!data?.showFooter });
  30. isUpdate.value = !!data?.isUpdate;
  31. isDetail.value = !!data?.showFooter;
  32. if (unref(isUpdate)) {
  33. //表单赋值
  34. await setFieldsValue({
  35. ...data.record,
  36. });
  37. }
  38. // 隐藏底部时禁用整个表单
  39. setProps({ disabled: !data?.showFooter });
  40. });
  41. //设置标题
  42. const title = computed(() => (!unref(isUpdate) ? '新增' : !unref(isDetail) ? '详情' : '编辑'));
  43. //表单提交事件
  44. async function handleSubmit(v) {
  45. try {
  46. let values = await validate();
  47. setModalProps({ confirmLoading: true });
  48. //提交表单
  49. await saveOrUpdate(values, isUpdate.value);
  50. //关闭弹窗
  51. closeModal();
  52. //刷新列表
  53. emit('success');
  54. } catch ({ errorFields }) {
  55. if (errorFields) {
  56. const firstField = errorFields[0];
  57. if (firstField) {
  58. scrollToField(firstField.name, { behavior: 'smooth', block: 'center' });
  59. }
  60. }
  61. return Promise.reject(errorFields);
  62. } finally {
  63. setModalProps({ confirmLoading: false });
  64. }
  65. }
  66. </script>
  67. <style lang="less" scoped>
  68. /** 时间和数字输入框样式 */
  69. :deep(.ant-input-number) {
  70. width: 100%;
  71. }
  72. :deep(.ant-calendar-picker) {
  73. width: 100%;
  74. }
  75. </style>