gaoluyang
2026-06-24 712aa51536236d43e87273e4ce45ac5691dffad8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<script lang="ts" setup>
import type { Ref } from 'vue';
 
import type { IotProductApi } from '#/api/iot/product/product';
import type { ThingModelApi } from '#/api/iot/thingmodel';
 
import { computed, inject, ref } from 'vue';
 
import { useVbenModal } from '..\..\..\..\packages\effects\common-ui\src';
import {
  DICT_TYPE,
  IOT_PROVIDE_KEY,
  IoTDataSpecsDataTypeEnum,
  IoTThingModelTypeEnum,
} from '..\..\..\..\packages\constants\src';
import { getDictOptions } from '..\..\..\..\packages\effects\hooks\src';
import { $t } from '..\..\..\..\packages\locales\src';
import { cloneDeep, isEmpty } from '..\..\..\..\packages\utils\src';
 
import { Form, Input, message, Radio } from 'ant-design-vue';
 
import {
  createThingModel,
  getThingModel,
  ThingModelFormRules,
  updateThingModel,
} from '#/api/iot/thingmodel';
 
import ThingModelEvent from './event.vue';
import ThingModelProperty from './property.vue';
import ThingModelService from './service.vue';
 
const emit = defineEmits(['success']);
 
const product = inject<Ref<IotProductApi.Product>>(IOT_PROVIDE_KEY.PRODUCT);
 
const formRef = ref();
const formData = ref<ThingModelApi.ThingModel>(buildEmptyFormData());
 
const getTitle = computed(() =>
  formData.value.id
    ? $t('ui.actionTitle.edit', ['物模型'])
    : $t('ui.actionTitle.create', ['物模型']),
);
 
const [Modal, modalApi] = useVbenModal({
  async onConfirm() {
    try {
      await formRef.value?.validate();
    } catch {
      return;
    }
    modalApi.lock();
    // 提交表单
    const data = cloneDeep(formData.value);
    data.productId = product!.value.id;
    data.productKey = product!.value.productKey;
    fillExtraAttributes(data);
    try {
      await (data.id ? updateThingModel(data) : createThingModel(data));
      // 关闭并提示
      await modalApi.close();
      emit('success');
      message.success($t('ui.actionMessage.operationSuccess'));
    } finally {
      modalApi.unlock();
    }
  },
  async onOpenChange(isOpen: boolean) {
    if (!isOpen) {
      return;
    }
    // 每次打开都重置;避免上一次的状态残留
    formData.value = buildEmptyFormData();
    formRef.value?.clearValidate?.();
    // 加载数据
    const data = modalApi.getData<{ id?: number }>();
    if (!data?.id) {
      return;
    }
    modalApi.lock();
    try {
      const result = await getThingModel(data.id);
      // 设置到 values
      formData.value = normalizeFormData(result);
    } finally {
      modalApi.unlock();
    }
  },
});
 
/** 构造空白表单数据 */
function buildEmptyFormData(): ThingModelApi.ThingModel {
  return {
    type: IoTThingModelTypeEnum.PROPERTY,
    dataType: IoTDataSpecsDataTypeEnum.INT,
    property: {
      dataType: IoTDataSpecsDataTypeEnum.INT,
      dataSpecs: {
        dataType: IoTDataSpecsDataTypeEnum.INT,
      },
    },
    service: {
      inputParams: [],
      outputParams: [],
    },
    event: {
      outputParams: [],
    },
  };
}
 
/** 回显数据时,规整各分支字段确保子表单可绑定 */
function normalizeFormData(
  result: ThingModelApi.ThingModel,
): ThingModelApi.ThingModel {
  const next: any = { ...result, type: Number(result.type) };
  if (isEmpty(next.property)) {
    next.dataType = IoTDataSpecsDataTypeEnum.INT;
    next.property = {
      dataType: IoTDataSpecsDataTypeEnum.INT,
      dataSpecs: { dataType: IoTDataSpecsDataTypeEnum.INT },
    };
  } else {
    next.property.dataSpecs ??= {};
    next.property.dataSpecsList ??= [];
    next.property.dataType ??= IoTDataSpecsDataTypeEnum.INT;
  }
  if (isEmpty(next.service)) {
    next.service = { inputParams: [], outputParams: [] };
  } else {
    next.service.inputParams ??= [];
    next.service.outputParams ??= [];
  }
  if (isEmpty(next.event)) {
    next.event = { outputParams: [] };
  } else {
    next.event.outputParams ??= [];
  }
  return next;
}
 
/** 按功能类型将子表单数据回写到顶层,并清理无关分支 */
function fillExtraAttributes(data: any) {
  switch (data.type) {
    case IoTThingModelTypeEnum.EVENT: {
      removeDataSpecs(data.event);
      data.dataType = data.event.dataType;
      data.event.identifier = data.identifier;
      data.event.name = data.name;
      delete data.property;
      delete data.service;
 
      break;
    }
    case IoTThingModelTypeEnum.PROPERTY: {
      removeDataSpecs(data.property);
      data.dataType = data.property.dataType;
      data.property.identifier = data.identifier;
      data.property.name = data.name;
      delete data.service;
      delete data.event;
 
      break;
    }
    case IoTThingModelTypeEnum.SERVICE: {
      removeDataSpecs(data.service);
      data.dataType = data.service.dataType;
      data.service.identifier = data.identifier;
      data.service.name = data.name;
      delete data.property;
      delete data.event;
 
      break;
    }
    // No default
  }
}
 
/** 清理空的 dataSpecs / dataSpecsList */
function removeDataSpecs(val: any) {
  if (isEmpty(val.dataSpecs)) {
    delete val.dataSpecs;
  }
  if (isEmpty(val.dataSpecsList)) {
    delete val.dataSpecsList;
  }
}
</script>
 
<template>
  <Modal :title="getTitle" class="w-2/5">
    <Form
      ref="formRef"
      :label-col="{ span: 6 }"
      :model="formData"
      :wrapper-col="{ span: 18 }"
      class="mx-4"
    >
      <Form.Item :rules="ThingModelFormRules.type" label="功能类型" name="type">
        <Radio.Group v-model:value="formData.type">
          <Radio.Button
            v-for="dict in getDictOptions(DICT_TYPE.IOT_THING_MODEL_TYPE)"
            :key="String(dict.value)"
            :value="Number(dict.value)"
          >
            {{ dict.label }}
          </Radio.Button>
        </Radio.Group>
      </Form.Item>
      <Form.Item :rules="ThingModelFormRules.name" label="功能名称" name="name">
        <Input v-model:value="formData.name" placeholder="请输入功能名称" />
      </Form.Item>
      <Form.Item
        :rules="ThingModelFormRules.identifier"
        label="标识符"
        name="identifier"
      >
        <Input v-model:value="formData.identifier" placeholder="请输入标识符" />
      </Form.Item>
      <!-- 属性配置 -->
      <ThingModelProperty
        v-if="formData.type === IoTThingModelTypeEnum.PROPERTY"
        v-model="formData.property"
      />
      <!-- 服务配置 -->
      <ThingModelService
        v-if="formData.type === IoTThingModelTypeEnum.SERVICE"
        v-model="formData.service"
      />
      <!-- 事件配置 -->
      <ThingModelEvent
        v-if="formData.type === IoTThingModelTypeEnum.EVENT"
        v-model="formData.event"
      />
      <Form.Item label="描述" name="description">
        <Input.TextArea
          v-model:value="formData.description"
          :maxlength="200"
          :rows="3"
          placeholder="请输入物模型描述"
        />
      </Form.Item>
    </Form>
  </Modal>
</template>