IotInfoForm.vue 3.9 KB

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