DemoStudentContactForm.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <template>
  2. <Dialog :title="dialogTitle" v-model="dialogVisible">
  3. <el-form
  4. ref="formRef"
  5. :model="formData"
  6. :rules="formRules"
  7. label-width="100px"
  8. v-loading="formLoading"
  9. >
  10. <el-form-item label="字段 1" prop="field1">
  11. <el-input v-model="formData.field1" placeholder="请输入字段 1" />
  12. </el-form-item>
  13. </el-form>
  14. <template #footer>
  15. <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
  16. <el-button @click="dialogVisible = false">取 消</el-button>
  17. </template>
  18. </Dialog>
  19. </template>
  20. <script setup lang="ts">
  21. import * as DemoStudentApi from '@/api/infra/demo02'
  22. import DemoStudentContactForm from './DemoStudentContactForm.vue'
  23. import DemoStudentAddressForm from './DemoStudentAddressForm.vue'
  24. const { t } = useI18n() // 国际化
  25. const message = useMessage() // 消息弹窗
  26. const dialogVisible = ref(false) // 弹窗的是否展示
  27. const dialogTitle = ref('') // 弹窗的标题
  28. const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
  29. const formType = ref('') // 表单的类型:create - 新增;update - 修改
  30. const formData = ref({
  31. id: undefined
  32. })
  33. const formRules = reactive({
  34. field2: [required]
  35. })
  36. const formRef = ref() // 表单 Ref
  37. /** 打开弹窗 */
  38. const open = async (type: string, id?: number) => {
  39. dialogVisible.value = true
  40. dialogTitle.value = t('action.' + type)
  41. formType.value = type
  42. resetForm()
  43. // 修改时,设置数据
  44. if (id) {
  45. // debugger
  46. formLoading.value = true
  47. try {
  48. // formData.value = await DemoStudentApi.getDemoStudent(id)
  49. formData.value = {
  50. id: id,
  51. field1: '1',
  52. field2: '22',
  53. field3: '333'
  54. }
  55. } finally {
  56. formLoading.value = false
  57. }
  58. }
  59. }
  60. defineExpose({ open }) // 提供 open 方法,用于打开弹窗
  61. /** 提交表单 */
  62. const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
  63. const submitForm = async () => {
  64. // 校验表单
  65. await formRef.value.validate()
  66. // 提交请求
  67. formLoading.value = true
  68. try {
  69. const data = formData.value as unknown as DemoStudentApi.DemoStudentVO
  70. if (formType.value === 'create') {
  71. // await DemoStudentApi.createDemoStudent(data) // TODO 芋艿:临时去掉
  72. message.success(t('common.createSuccess'))
  73. } else {
  74. await DemoStudentApi.updateDemoStudent(data)
  75. message.success(t('common.updateSuccess'))
  76. }
  77. dialogVisible.value = false
  78. // 发送操作成功的事件
  79. emit('success')
  80. } finally {
  81. formLoading.value = false
  82. }
  83. }
  84. /** 重置表单 */
  85. const resetForm = () => {
  86. formData.value = {
  87. id: undefined
  88. }
  89. formRef.value?.resetFields()
  90. }
  91. </script>