form.vue 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <template>
  2. <!-- 弹窗 -->
  3. <XModal :title="modelTitle" :loading="modelLoading" v-model="modelVisible">
  4. <!-- 表单:添加/修改 -->
  5. <Form
  6. ref="formRef"
  7. v-if="['create', 'update'].includes(actionType)"
  8. :schema="allSchemas.formSchema"
  9. :rules="rules"
  10. />
  11. <!-- 表单:详情 -->
  12. <Descriptions
  13. v-if="actionType === 'detail'"
  14. :schema="allSchemas.detailSchema"
  15. :data="detailData"
  16. />
  17. <template #footer>
  18. <!-- 按钮:保存 -->
  19. <XButton
  20. v-if="['create', 'update'].includes(actionType)"
  21. type="primary"
  22. :title="t('action.save')"
  23. :loading="actionLoading"
  24. @click="submitForm()"
  25. />
  26. <!-- 按钮:关闭 -->
  27. <XButton :loading="actionLoading" :title="t('dialog.close')" @click="modelVisible = false" />
  28. </template>
  29. </XModal>
  30. </template>
  31. <script setup lang="ts">
  32. import type { FormExpose } from '@/components/Form'
  33. import * as PostApi from '@/api/system/post'
  34. import { rules, allSchemas } from './post.data'
  35. const { t } = useI18n() // 国际化
  36. const message = useMessage() // 消息弹窗
  37. // 弹窗相关的变量
  38. const modelVisible = ref(false) // 是否显示弹出层
  39. const modelTitle = ref('') // 弹出层标题
  40. const modelLoading = ref(false) // 弹出层loading
  41. const actionType = ref('') // 操作按钮的类型
  42. const actionLoading = ref(false) // 按钮 Loading
  43. const formRef = ref<FormExpose>() // 表单 Ref
  44. const detailData = ref() // 详情 Ref
  45. // 打开弹窗
  46. const openModal = async (type: string, id?: number) => {
  47. modelVisible.value = true
  48. modelLoading.value = true
  49. modelTitle.value = t('action.' + type)
  50. actionType.value = type
  51. // 设置数据
  52. if (id) {
  53. const res = await PostApi.getPostApi(id)
  54. if (type === 'update') {
  55. unref(formRef)?.setValues(res)
  56. } else if (type === 'detail') {
  57. detailData.value = res
  58. }
  59. }
  60. modelLoading.value = false
  61. }
  62. defineExpose({ openModal }) // 提供 openModal 方法,用于打开弹窗
  63. // 提交新增/修改的表单
  64. const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
  65. const submitForm = async () => {
  66. // 校验表单
  67. const elForm = unref(formRef)?.getElFormRef()
  68. if (!elForm) return
  69. const valid = await elForm.validate()
  70. if (!valid) return
  71. // 提交请求
  72. actionLoading.value = true
  73. try {
  74. const data = unref(formRef)?.formModel as PostApi.PostVO
  75. if (actionType.value === 'create') {
  76. await PostApi.createPostApi(data)
  77. message.success(t('common.createSuccess'))
  78. } else {
  79. await PostApi.updatePostApi(data)
  80. message.success(t('common.updateSuccess'))
  81. }
  82. modelVisible.value = false
  83. emit('success')
  84. } finally {
  85. actionLoading.value = false
  86. }
  87. }
  88. </script>