AttrTemplateModelForm.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <!-- 设备分类属性表单 -->
  2. <template>
  3. <Dialog v-model="dialogVisible" :title="dialogTitle">
  4. <el-form
  5. ref="formRef"
  6. v-loading="formLoading"
  7. :model="formData"
  8. :rules="DeviceAttrModelFormRules"
  9. label-width="100px"
  10. >
  11. <el-form-item label="属性名称" prop="name">
  12. <el-input v-model="formData.name" placeholder="请输入属性名称" />
  13. </el-form-item>
  14. <el-form-item label="标识符" prop="code">
  15. <el-input v-model="formData.code" placeholder="请输入标识符" />
  16. </el-form-item>
  17. <!-- 属性配置 -->
  18. <DeviceAttrModelProperty
  19. v-model="formData.selectOptions"
  20. />
  21. <el-form-item label="是否数采" prop="isCollection">
  22. <el-radio-group v-model="formData.isCollection">
  23. <el-radio
  24. v-for="dict in getIntDictOptions(DICT_TYPE.RQ_IOT_ISCOLLECTION)"
  25. :key="dict.value"
  26. :value="dict.value"
  27. @click="radioChange(dict.value)"
  28. >
  29. {{ dict.label }}
  30. </el-radio>
  31. </el-radio-group>
  32. </el-form-item>
  33. <el-form-item label="物属性" prop="modelAttr">
  34. <el-select v-model="formData.modelAttr" placeholder="请选择">
  35. <el-option
  36. v-for="dict in thingsModelData"
  37. :key="dict.modelName"
  38. :label="dict.modelName"
  39. :value="dict.identifier"
  40. />
  41. </el-select>
  42. </el-form-item>
  43. <el-form-item label="默认值" prop="defaultValue">
  44. <el-input v-model="formData.defaultValue" placeholder="请输入默认值" />
  45. </el-form-item>
  46. <el-form-item label="描述" prop="description">
  47. <el-input
  48. v-model="formData.description"
  49. :maxlength="200"
  50. :rows="3"
  51. placeholder="请输入属性描述"
  52. type="textarea"
  53. />
  54. </el-form-item>
  55. </el-form>
  56. <template #footer>
  57. <el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
  58. <el-button @click="dialogVisible = false">取 消</el-button>
  59. </template>
  60. </Dialog>
  61. </template>
  62. <script lang="ts" setup>
  63. import DeviceAttrModelProperty from './AttrTemplateModelProperty.vue'
  64. import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
  65. import { DeviceAttrModelApi, DeviceAttrModelData } from '@/api/pms/modelattrtemplate'
  66. import { DataSpecsDataType, DeviceAttrModelFormRules } from './config'
  67. import { cloneDeep } from 'lodash-es'
  68. import { isEmpty } from '@/utils/is'
  69. import { defineProps } from 'vue'
  70. import * as ModelTemplateApi from "@/api/pms/modeltemplate";
  71. /** 设备属性 模型数据表单 */
  72. defineOptions({ name: 'AttrTemplateModelForm' })
  73. const { t } = useI18n() // 国际化
  74. const message = useMessage() // 消息弹窗
  75. const dialogVisible = ref(false) // 弹窗的是否展示
  76. const dialogTitle = ref('') // 弹窗的标题
  77. const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
  78. const formType = ref('') // 表单的类型:create - 新增;update - 修改
  79. const formData = ref<DeviceAttrModelData>({
  80. deviceCategoryId: -1,
  81. dataType: DataSpecsDataType.DOUBLE,
  82. type: DataSpecsDataType.DOUBLE,
  83. requiredFlag: 0,
  84. description: '',
  85. defaultValue: '',
  86. selectOptions: {
  87. type: DataSpecsDataType.DOUBLE,
  88. defaultValue: '',
  89. requiredFlag: '',
  90. dataSpecs: {
  91. dataType: DataSpecsDataType.DOUBLE
  92. }
  93. },
  94. isCollection:1,
  95. modelAttr:'',
  96. })
  97. const thingsModelData = ref([{
  98. modelName:'',
  99. identifier:''
  100. }]
  101. )
  102. let deviceCategoryName: string;
  103. const radioChange = (event) => {
  104. if(event==1){
  105. getAttrList();
  106. }
  107. // 在这里添加你的逻辑处理代码
  108. };
  109. const getAttrList = async () => {
  110. try {
  111. const data = await DeviceAttrModelApi.getThingsModelAttr(deviceCategoryName);
  112. thingsModelData.value = data
  113. } finally {
  114. }
  115. }
  116. const props = defineProps({
  117. deviceCategoryId: [String, Number]
  118. })
  119. const formRef = ref() // 表单 Ref
  120. /** 打开弹窗 */
  121. const open = async (type: string, id?: number,name?:string) => {
  122. deviceCategoryName = name;
  123. if(type=='update'){
  124. await getAttrList();
  125. }
  126. dialogVisible.value = true
  127. dialogTitle.value = t('action.' + type)
  128. formType.value = type
  129. resetForm()
  130. if (id) {
  131. formLoading.value = true
  132. try {
  133. formData.value = await DeviceAttrModelApi.getDeviceAttrModel(id)
  134. // 情况一:属性初始化
  135. if (isEmpty(formData.value.selectOptions)) {
  136. formData.value.type = DataSpecsDataType.DOUBLE
  137. formData.value.selectOptions = {
  138. type: DataSpecsDataType.DOUBLE,
  139. dataSpecs: {
  140. dataType: DataSpecsDataType.DOUBLE
  141. }
  142. }
  143. }
  144. } finally {
  145. formLoading.value = false
  146. }
  147. }
  148. }
  149. defineExpose({ open, close: () => (dialogVisible.value = false) })
  150. /** 提交表单 */
  151. const emit = defineEmits(['success'])
  152. const submitForm = async () => {
  153. await formRef.value.validate()
  154. formLoading.value = true
  155. try {
  156. const data = cloneDeep(formData.value) as DeviceAttrModelData
  157. data.deviceCategoryId = props.deviceCategoryId
  158. data.requiredFlag = formData.value.selectOptions.requiredFlag
  159. fillExtraAttributes(data)
  160. if (formType.value === 'create') {
  161. await DeviceAttrModelApi.createDeviceAttrModel(data)
  162. message.success(t('common.createSuccess'))
  163. } else {
  164. await DeviceAttrModelApi.updateDeviceAttrModel(data)
  165. message.success(t('common.updateSuccess'))
  166. }
  167. } finally {
  168. dialogVisible.value = false // 确保关闭弹框
  169. emit('success')
  170. formLoading.value = false
  171. }
  172. }
  173. /** 填写额外的属性 */
  174. const fillExtraAttributes = (data: any) => {
  175. // 处理不同类型的情况
  176. // 属性
  177. removeDataSpecs(data.selectOptions)
  178. data.type = data.selectOptions.type
  179. data.selectOptions.identifier = data.code
  180. data.selectOptions.code = data.code
  181. data.selectOptions.name = data.name
  182. }
  183. /** 处理 dataSpecs 为空的情况 */
  184. const removeDataSpecs = (val: any) => {
  185. if (isEmpty(val.dataSpecs)) {
  186. delete val.dataSpecs
  187. }
  188. if (isEmpty(val.dataSpecsList)) {
  189. delete val.dataSpecsList
  190. }
  191. }
  192. /** 重置表单 */
  193. const resetForm = () => {
  194. formData.value = {
  195. dataType: DataSpecsDataType.DOUBLE,
  196. type: DataSpecsDataType.DOUBLE,
  197. selectOptions: {
  198. type: DataSpecsDataType.DOUBLE,
  199. dataSpecs: {
  200. dataType: DataSpecsDataType.DOUBLE
  201. }
  202. },
  203. }
  204. formRef.value?.resetFields()
  205. }
  206. </script>