form.vue 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <template>
  2. <Dialog :title="modelTitle" v-model="modelVisible">
  3. <!-- TODO 芋艿:loading -->
  4. <Form ref="formRef" :schema="allSchemas.formSchema" :rules="rules" :isCol="false" />
  5. <template #footer>
  6. <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
  7. <el-button @click="modelVisible = false">取 消</el-button>
  8. </template>
  9. </Dialog>
  10. </template>
  11. <script setup lang="ts">
  12. import * as MailAccountApi from '@/api/system/mail/account'
  13. import { rules, allSchemas } from './account.data'
  14. const formRef = ref() // 表单 Ref
  15. const { t } = useI18n() // 国际化
  16. const message = useMessage() // 消息弹窗
  17. const modelVisible = ref(false) // 弹窗的是否展示
  18. const modelTitle = ref('') // 弹窗的标题
  19. const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
  20. const formType = ref('') // 表单的类型:create - 新增;update - 修改
  21. /** 打开弹窗 */
  22. const openModal = async (type: string, id?: number) => {
  23. modelVisible.value = true
  24. modelTitle.value = t('action.' + type)
  25. formType.value = type
  26. // resetForm()
  27. // 修改时,设置数据
  28. if (id) {
  29. formLoading.value = true
  30. try {
  31. const data = await MailAccountApi.getMailAccountApi(id)
  32. formRef.value.setValues(data)
  33. } finally {
  34. formLoading.value = false
  35. }
  36. }
  37. }
  38. defineExpose({ openModal }) // 提供 openModal 方法,用于打开弹窗
  39. /** 提交表单 */
  40. const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
  41. const submitForm = async () => {
  42. // 校验表单
  43. if (!formRef) return
  44. const valid = await formRef.value.getElFormRef().validate()
  45. if (!valid) return
  46. // 提交请求
  47. formLoading.value = true
  48. try {
  49. const data = formRef.value.formModel as MailAccountApi.MailAccountVO
  50. if (formType.value === 'create') {
  51. await MailAccountApi.createMailAccountApi(data)
  52. message.success(t('common.createSuccess'))
  53. } else {
  54. await MailAccountApi.updateMailAccountApi(data)
  55. message.success(t('common.updateSuccess'))
  56. }
  57. modelVisible.value = false
  58. // 发送操作成功的事件
  59. emit('success')
  60. } finally {
  61. formLoading.value = false
  62. }
  63. }
  64. </script>