ThingModelInputOutputParam.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <template>
  2. <div
  3. v-for="(item, index) in thingModelParams"
  4. :key="index"
  5. class="w-1/1 param-item flex justify-between px-10px mb-10px"
  6. >
  7. <span>参数名称:{{ item.name }}</span>
  8. <div class="btn">
  9. <el-button link type="primary" @click="openParamForm(item)">编辑</el-button>
  10. <el-divider direction="vertical" />
  11. <el-button link type="danger" @click="deleteParamItem(index)">删除</el-button>
  12. </div>
  13. </div>
  14. <el-button link type="primary" @click="openParamForm(null)">+新增参数</el-button>
  15. <!-- param 表单 -->
  16. <Dialog v-model="dialogVisible" :title="dialogTitle" append-to-body>
  17. <el-form
  18. ref="paramFormRef"
  19. v-loading="formLoading"
  20. :model="formData"
  21. :rules="ThingModelFormRules"
  22. label-width="100px"
  23. >
  24. <el-form-item label="参数名称" prop="name">
  25. <el-input v-model="formData.name" placeholder="请输入功能名称" />
  26. </el-form-item>
  27. <el-form-item label="标识符" prop="identifier">
  28. <el-input v-model="formData.identifier" placeholder="请输入标识符" />
  29. </el-form-item>
  30. <!-- 属性配置 -->
  31. <ThingModelProperty v-model="formData.property" is-params />
  32. </el-form>
  33. <template #footer>
  34. <el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
  35. <el-button @click="dialogVisible = false">取 消</el-button>
  36. </template>
  37. </Dialog>
  38. </template>
  39. <script lang="ts" setup>
  40. import { useVModel } from '@vueuse/core'
  41. import ThingModelProperty from './ThingModelProperty.vue'
  42. import { DataSpecsDataType, ThingModelFormRules } from './config'
  43. import { isEmpty } from '@/utils/is'
  44. /** 输入输出参数配置组件 */
  45. defineOptions({ name: 'ThingModelInputOutputParam' })
  46. const props = defineProps<{ modelValue: any; direction: string }>()
  47. const emits = defineEmits(['update:modelValue'])
  48. const thingModelParams = useVModel(props, 'modelValue', emits) as Ref<any[]>
  49. const dialogVisible = ref(false) // 弹窗的是否展示
  50. const dialogTitle = ref('新增参数') // 弹窗的标题
  51. const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
  52. const paramFormRef = ref() // 表单 ref
  53. const formData = ref<any>({
  54. dataType: DataSpecsDataType.INT,
  55. property: {
  56. dataType: DataSpecsDataType.INT,
  57. dataSpecs: {
  58. dataType: DataSpecsDataType.INT
  59. }
  60. }
  61. })
  62. /** 打开 param 表单 */
  63. const openParamForm = (val: any) => {
  64. dialogVisible.value = true
  65. resetForm()
  66. if (isEmpty(val)) {
  67. return
  68. }
  69. // 编辑时回显数据
  70. formData.value = {
  71. identifier: val.identifier,
  72. name: val.name,
  73. description: val.description,
  74. property: {
  75. dataType: val.dataType,
  76. dataSpecs: val.dataSpecs,
  77. dataSpecsList: val.dataSpecsList
  78. }
  79. }
  80. }
  81. /** 删除 param 项 */
  82. const deleteParamItem = (index: number) => {
  83. thingModelParams.value.splice(index, 1)
  84. }
  85. /** 添加参数 */
  86. const submitForm = async () => {
  87. // 初始化参数列表
  88. if (isEmpty(thingModelParams.value)) {
  89. thingModelParams.value = []
  90. }
  91. // 校验参数
  92. await paramFormRef.value.validate()
  93. try {
  94. const data = unref(formData)
  95. // 构建数据对象
  96. const item = {
  97. identifier: data.identifier,
  98. name: data.name,
  99. description: data.description,
  100. dataType: data.property.dataType,
  101. paraOrder: 0, // TODO @puhui999: 先写死默认看看后续
  102. direction: props.direction,
  103. dataSpecs:
  104. !!data.property.dataSpecs && Object.keys(data.property.dataSpecs).length > 1
  105. ? data.property.dataSpecs
  106. : undefined,
  107. dataSpecsList: isEmpty(data.property.dataSpecsList) ? undefined : data.property.dataSpecsList
  108. }
  109. // 查找是否已有相同 identifier 的项
  110. const existingIndex = thingModelParams.value.findIndex(
  111. (spec) => spec.identifier === data.identifier
  112. )
  113. if (existingIndex > -1) {
  114. // 更新已有项
  115. thingModelParams.value[existingIndex] = item
  116. } else {
  117. // 添加新项
  118. thingModelParams.value.push(item)
  119. }
  120. } finally {
  121. // 隐藏对话框
  122. dialogVisible.value = false
  123. }
  124. }
  125. /** 重置表单 */
  126. const resetForm = () => {
  127. formData.value = {
  128. dataType: DataSpecsDataType.INT,
  129. property: {
  130. dataType: DataSpecsDataType.INT,
  131. dataSpecs: {
  132. dataType: DataSpecsDataType.INT
  133. }
  134. }
  135. }
  136. paramFormRef.value?.resetFields()
  137. }
  138. </script>
  139. <style lang="scss" scoped>
  140. .param-item {
  141. background-color: #e4f2fd;
  142. }
  143. </style>