form.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <template>
  2. <!-- 表单 -->
  3. <div class="about main-background main-background-type0">
  4. <div class="nav-placeholder"></div>
  5. <!-- 表单 -->
  6. <section class="main-section ">
  7. <div class="content">
  8. <div class="title">
  9. <h2>{{ title }}</h2>
  10. </div>
  11. <a-spin v-if="loadingData" />
  12. <template v-else>
  13. <DynamicForm
  14. ref="form"
  15. :model="(formModel as any)"
  16. :options="formOptions"
  17. />
  18. <a-button
  19. type="primary"
  20. block
  21. :loading="loading" class="mt-3"
  22. @click="handleSubmit"
  23. >
  24. 提交
  25. </a-button>
  26. </template>
  27. </div>
  28. </section>
  29. </div>
  30. </template>
  31. <script setup lang="ts" generic="T extends DataModel">
  32. import InheritorContent from '@/api/inheritor/InheritorContent';
  33. import type { DataModel } from '@imengyu/js-request-transform';
  34. import { DynamicForm, type IDynamicFormOptions, type IDynamicFormRef } from '@imengyu/vue-dynamic-form';
  35. import { message, Modal, type FormInstance } from 'ant-design-vue';
  36. import { onMounted, ref, toRefs, type PropType } from 'vue';
  37. import { useRouter } from 'vue-router';
  38. const props = defineProps({
  39. title: {
  40. type: String,
  41. default: '项目申报'
  42. },
  43. formModel: {
  44. type: Object as PropType<T>,
  45. required: true
  46. },
  47. formOptions: {
  48. type: Object as PropType<IDynamicFormOptions>,
  49. required: true
  50. },
  51. load: {
  52. type: Function as PropType<() => Promise<T|undefined|void>>,
  53. default: () => Promise.resolve()
  54. },
  55. save: {
  56. type: Function as PropType<(model: T) => Promise<void>>,
  57. default: () => Promise.resolve()
  58. }
  59. })
  60. const { formModel, formOptions, load } = toRefs(props);
  61. const form = ref<IDynamicFormRef>();
  62. const router = useRouter();
  63. const loading = ref(false);
  64. const loadingData = ref(false);
  65. async function handleSubmit() {
  66. loading.value = true;
  67. const ref = (form.value?.getFormRef() as FormInstance);
  68. try {
  69. await ref.validate();
  70. } catch (e) {
  71. message.warn('请填写完整信息');
  72. loading.value = false;
  73. if ((e as any).errorFields)
  74. ref.scrollToField((e as any).errorFields[0].name, { block: 'center' })
  75. return;
  76. }
  77. try {
  78. const result = await InheritorContent.saveBaseInfo(formModel.value);
  79. await props.save(formModel.value);
  80. Modal.success({
  81. title: '提交成功',
  82. content: result.message,
  83. onOk() {
  84. router.push({ path: '/' })
  85. },
  86. });
  87. } catch (error) {
  88. Modal.error({
  89. title: '提交失败',
  90. content: '' + error,
  91. });
  92. } finally {
  93. loading.value = false;
  94. }
  95. }
  96. async function loadData() {
  97. loadingData.value = true;
  98. try {
  99. await load.value();
  100. } catch (error) {
  101. Modal.error({
  102. title: '加载失败',
  103. content: '' + error,
  104. });
  105. } finally {
  106. loadingData.value = false;
  107. }
  108. }
  109. onMounted(async () => {
  110. await loadData();
  111. })
  112. </script>