CertificateForm.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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="供应商" prop="supplierId">
  11. <el-select v-model="formData.supplierId" filterable placeholder="请选择">
  12. <el-option
  13. v-for="dict in supplierList"
  14. :key="dict.id"
  15. :label="dict.name"
  16. :value="dict.id"
  17. />
  18. </el-select>
  19. </el-form-item>
  20. <el-form-item label="证件类型" prop="type">
  21. <el-select v-model="formData.type" placeholder="请选择">
  22. <el-option
  23. v-for="dict in getIntDictOptions(DICT_TYPE.SUPPLIER_CERT)"
  24. :key="dict.value"
  25. :label="dict.label"
  26. :value="dict.value"
  27. />
  28. </el-select>
  29. </el-form-item>
  30. <el-form-item label="证件名称" prop="name">
  31. <el-input v-model="formData.name" placeholder="请输入证件名称" />
  32. </el-form-item>
  33. <el-form-item label="附件" prop="urls">
  34. <UploadFile :is-show-tip="false" v-model="formData.urls"/>
  35. </el-form-item>
  36. </el-form>
  37. <template #footer>
  38. <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
  39. <el-button @click="dialogVisible = false">取 消</el-button>
  40. </template>
  41. </Dialog>
  42. </template>
  43. <script lang="ts" setup>
  44. import {DICT_TYPE, getIntDictOptions} from "@/utils/dict";
  45. import { CertificateApi, CertificateVO } from '@/api/supplier/certificate'
  46. import {SupplierVO} from "@/api/supplier/base";
  47. import * as SupplierBaseApi from "@/api/supplier/base";
  48. import {FormRules} from "element-plus";
  49. /** 供应商证件资质 表单 */
  50. defineOptions({ name: 'CertificateForm' })
  51. const { t } = useI18n() // 国际化
  52. const message = useMessage() // 消息弹窗
  53. const supplierList = ref([] as SupplierVO[])
  54. const dialogVisible = ref(false) // 弹窗的是否展示
  55. const dialogTitle = ref('') // 弹窗的标题
  56. const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
  57. const formType = ref('') // 表单的类型:create - 新增;update - 修改
  58. const formData = ref({
  59. id: undefined,
  60. supplierId: undefined,
  61. supplierName: undefined,
  62. type: undefined,
  63. name: undefined,
  64. urls: undefined,
  65. })
  66. const formRules = reactive<FormRules>({
  67. supplierId: [{ required: true, message: '供应商不能为空', trigger: 'blur' }],
  68. type: [{ required: true, message: '证件类型不能为空', trigger: 'blur' }],
  69. name: [{ required: true, message: '证件名称不能为空', trigger: 'blur' }],
  70. })
  71. const formRef = ref() // 表单 Ref
  72. /** 打开弹窗 */
  73. const open = async (type: string, id?: number) => {
  74. dialogVisible.value = true
  75. dialogTitle.value = t('action.' + type)
  76. formType.value = type
  77. resetForm()
  78. // 修改时,设置数据
  79. if (id) {
  80. formLoading.value = true
  81. try {
  82. formData.value = await CertificateApi.getCertificate(id)
  83. } finally {
  84. formLoading.value = false
  85. }
  86. }
  87. supplierList.value = await SupplierBaseApi.Api.getAll()
  88. }
  89. defineExpose({ open }) // 提供 open 方法,用于打开弹窗
  90. /** 提交表单 */
  91. const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
  92. const submitForm = async () => {
  93. // 校验表单
  94. await formRef.value.validate()
  95. // 提交请求
  96. formLoading.value = true
  97. try {
  98. const data = formData.value as unknown as CertificateVO
  99. // const supplier = data.supplierId
  100. // data.supplierId = supplier.id
  101. // data.supplierName = supplier.name
  102. if (data.urls) {
  103. if (Array.isArray(data.urls)) {
  104. data.urls = data.urls.join(",")
  105. }
  106. }
  107. if (formType.value === 'create') {
  108. await CertificateApi.createCertificate(data)
  109. message.success(t('common.createSuccess'))
  110. } else {
  111. await CertificateApi.updateCertificate(data)
  112. message.success(t('common.updateSuccess'))
  113. }
  114. dialogVisible.value = false
  115. // 发送操作成功的事件
  116. emit('success')
  117. } finally {
  118. formLoading.value = false
  119. }
  120. }
  121. /** 重置表单 */
  122. const resetForm = () => {
  123. formData.value = {
  124. id: undefined,
  125. supplierId: undefined,
  126. type: undefined,
  127. name: undefined,
  128. urls: undefined,
  129. }
  130. formRef.value?.resetFields()
  131. }
  132. </script>