IotInspectItemForm.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <template>
  2. <Dialog :title="dialogTitle" v-model="dialogVisible" style="width: 50vw;min-height: 50vh" >
  3. <el-form
  4. ref="formRef"
  5. :model="formData"
  6. :rules="formRules"
  7. label-width="100px"
  8. v-loading="formLoading"
  9. >
  10. <el-row>
  11. <el-col :span="12">
  12. <el-form-item :label="t('monitor.category')" prop="deviceClassify">
  13. <el-tree-select
  14. filterable
  15. v-model="formData.deviceClassify"
  16. :data="productClassifyList"
  17. :props="defaultProps"
  18. check-strictly
  19. node-key="id"
  20. :placeholder="t('deviceForm.categoryHolder')"
  21. clearable
  22. />
  23. </el-form-item>
  24. </el-col>
  25. <el-col :span="12">
  26. <el-form-item :label="t('monitor.deviceName')" prop="deviceId">
  27. <el-select
  28. v-model="formData.deviceId"
  29. :model-value="deviceLabel"
  30. :placeholder="t('monitor.nameHolder')"
  31. @click="openForm"
  32. />
  33. </el-form-item>
  34. </el-col>
  35. <el-col :span="12">
  36. <el-form-item :label="t('inspect.InspectionItems')" prop="item">
  37. <el-input v-model="formData.item" :placeholder="t('inspect.itemHolder')" />
  38. </el-form-item>
  39. </el-col>
  40. <el-col :span="12">
  41. <el-form-item :label="t('common.sort')" prop="sort">
  42. <el-input v-model="formData.sort" type="number" :placeholder="t('common.sortHolder')" />
  43. </el-form-item>
  44. </el-col>
  45. <el-col :span="24">
  46. <el-form-item :label="t('inspect.InspectionStandards')" prop="standard">
  47. <el-input v-model="formData.standard" type="textarea" :placeholder="t('inspect.stanHolder')" />
  48. </el-form-item>
  49. </el-col>
  50. <el-col :span="24">
  51. <el-form-item :label="t('fileInfo.appendix')" prop="urls">
  52. <UploadFile v-model="formData.urls" :limit="1" :is-show-tip="false" class="min-w-80px" />
  53. </el-form-item>
  54. </el-col>
  55. <el-col :span="24">
  56. <el-form-item :label="t('form.remark')" prop="remark">
  57. <el-input v-model="formData.remark" type="textarea" :placeholder="t('deviceForm.remarkHolder')" />
  58. </el-form-item>
  59. </el-col>
  60. </el-row>
  61. </el-form>
  62. <template #footer>
  63. <el-button @click="submitForm" type="primary" :disabled="formLoading">{{ t('faultForm.ok') }}</el-button>
  64. <el-button @click="dialogVisible = false">{{ t('faultForm.cancel') }}</el-button>
  65. </template>
  66. </Dialog>
  67. <DeviceList ref="deviceFormRef" @choose="deviceChoose" />
  68. </template>
  69. <script setup lang="ts">
  70. import { IotInspectItemApi, IotInspectItemVO } from '@/api/pms/inspect/item'
  71. import {defaultProps, handleTree} from "@/utils/tree";
  72. import * as ProductClassifyApi from "@/api/pms/productclassify";
  73. import DeviceList from "@/views/pms/failure/DeviceList.vue";
  74. import {CACHE_KEY, useCache} from "@/hooks/web/useCache";
  75. /** 巡检项 表单 */
  76. defineOptions({ name: 'IotInspectItemForm' })
  77. const { t } = useI18n() // 国际化
  78. const message = useMessage() // 消息弹窗
  79. const dialogVisible = ref(false) // 弹窗的是否展示
  80. const dialogTitle = ref('') // 弹窗的标题
  81. const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
  82. const formType = ref('') // 表单的类型:create - 新增;update - 修改
  83. const productClassifyList = ref<Tree[]>([]) // 树形结构
  84. const deviceLabel = ref('') // 表单的类型:create - 新增;update - 修改
  85. const formData = ref({
  86. id: undefined,
  87. item: undefined,
  88. standard: undefined,
  89. sort: undefined,
  90. remark: undefined,
  91. deptId: undefined,
  92. deviceClassify: undefined,
  93. deviceClassifyName: undefined,
  94. deviceId: undefined,
  95. deviceName: undefined,
  96. deviceCode: undefined,
  97. urls: undefined,
  98. })
  99. const formRules = reactive({
  100. deviceClassify: [{ required: true, message: '设备类别不能为空', trigger: 'blur' }],
  101. item: [{ required: true, message: '巡检项不能为空', trigger: 'blur' }],
  102. standard: [{ required: true, message: '巡检标准不能为空', trigger: 'blur' }],
  103. sort: [{ required: true, message: '排序不能为空', trigger: 'blur' }],
  104. })
  105. const formRef = ref() // 表单 Ref
  106. const deviceChoose = (row) => {
  107. formData.value.deviceId = row.id
  108. formData.value.deviceName = row.deviceName
  109. formData.value.deviceCode = row.deviceCode
  110. formData.value.deptId = row.deptId;
  111. deviceLabel.value = row.deviceName
  112. }
  113. /** 打开弹窗 */
  114. const open = async (type: string, id?: number) => {
  115. dialogVisible.value = true
  116. dialogTitle.value = t('action.' + type)
  117. formType.value = type
  118. resetForm()
  119. // 修改时,设置数据
  120. if (id) {
  121. formLoading.value = true
  122. try {
  123. formData.value = await IotInspectItemApi.getIotInspectItem(id)
  124. deviceLabel.value = formData.value.deviceName
  125. } finally {
  126. formLoading.value = false
  127. }
  128. }else {
  129. formData.value.sort = 0
  130. deviceLabel.value = ''
  131. }
  132. productClassifyList.value = handleTree(
  133. await ProductClassifyApi.IotProductClassifyApi.getSimpleProductClassifyList()
  134. )
  135. }
  136. defineExpose({ open }) // 提供 open 方法,用于打开弹窗
  137. const deviceFormRef = ref()
  138. const openForm = () => {
  139. deviceFormRef.value.open(formData.value.deviceClassify)
  140. }
  141. /** 提交表单 */
  142. const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
  143. const { wsCache } = useCache()
  144. const submitForm = async () => {
  145. // 校验表单
  146. await formRef.value.validate()
  147. // 提交请求
  148. formLoading.value = true
  149. try {
  150. const data = formData.value as unknown as IotInspectItemVO
  151. if (formType.value === 'create') {
  152. await IotInspectItemApi.createIotInspectItem(data)
  153. message.success(t('common.createSuccess'))
  154. } else {
  155. await IotInspectItemApi.updateIotInspectItem(data)
  156. message.success(t('common.updateSuccess'))
  157. }
  158. dialogVisible.value = false
  159. // 发送操作成功的事件
  160. emit('success')
  161. } finally {
  162. formLoading.value = false
  163. }
  164. }
  165. /** 重置表单 */
  166. const resetForm = () => {
  167. formData.value = {
  168. id: undefined,
  169. item: undefined,
  170. standard: undefined,
  171. sort: undefined,
  172. remark: undefined,
  173. deptId: undefined,
  174. }
  175. formRef.value?.resetFields()
  176. }
  177. </script>