Files
iot-device-management-frontend/apps/web-antd/src/views/iot/thingmodel/modules/ThingModelInputOutputParam.vue

164 lines
4.6 KiB
Vue
Raw Normal View History

<!-- 产品的物模型表单eventservice 项里的参数 -->
2025-10-10 20:26:17 +08:00
<script lang="ts" setup>
2025-10-13 10:41:08 +08:00
import type { Ref } from 'vue';
import { ref, unref } from 'vue';
2025-10-10 20:26:17 +08:00
import { isEmpty } from '@vben/utils';
2025-10-10 20:26:17 +08:00
import { useVModel } from '@vueuse/core';
2025-10-13 10:41:08 +08:00
import { Button, Divider, Form, Input, Modal } from 'ant-design-vue';
2025-10-10 20:26:17 +08:00
import { IoTDataSpecsDataTypeEnum } from '#/views/iot/utils/constants';
import ThingModelProperty from './ThingModelProperty.vue';
/** 输入输出参数配置组件 */
2025-10-10 20:26:17 +08:00
defineOptions({ name: 'ThingModelInputOutputParam' });
2025-10-10 20:26:17 +08:00
const props = defineProps<{ direction: string; modelValue: any }>();
const emits = defineEmits(['update:modelValue']);
const thingModelParams = useVModel(props, 'modelValue', emits) as Ref<any[]>;
const dialogVisible = ref(false); // 弹窗的是否展示
const formLoading = ref(false); // 表单的加载中1修改时的数据加载2提交的按钮禁用
const paramFormRef = ref(); // 表单 ref
const formData = ref<any>({
dataType: IoTDataSpecsDataTypeEnum.INT,
property: {
dataType: IoTDataSpecsDataTypeEnum.INT,
dataSpecs: {
2025-10-10 20:26:17 +08:00
dataType: IoTDataSpecsDataTypeEnum.INT,
},
dataSpecsList: [],
2025-10-10 20:26:17 +08:00
},
});
/** 打开 param 表单 */
2025-10-13 10:41:08 +08:00
function openParamForm(val: any) {
2025-10-10 20:26:17 +08:00
dialogVisible.value = true;
resetForm();
if (isEmpty(val)) {
2025-10-10 20:26:17 +08:00
return;
}
// 编辑时回显数据
const valData = val as any;
formData.value = {
identifier: valData?.identifier || '',
name: valData?.name || '',
description: valData?.description || '',
property: {
dataType: valData?.dataType || IoTDataSpecsDataTypeEnum.INT,
dataSpecs: valData?.dataSpecs ?? {},
dataSpecsList: valData?.dataSpecsList ?? [],
2025-10-10 20:26:17 +08:00
},
};
// 确保 property.dataType 有值
if (!formData.value.property.dataType) {
formData.value.property.dataType = IoTDataSpecsDataTypeEnum.INT;
}
2025-10-13 10:41:08 +08:00
}
/** 删除 param 项 */
2025-10-13 10:41:08 +08:00
function deleteParamItem(index: number) {
2025-10-10 20:26:17 +08:00
thingModelParams.value.splice(index, 1);
2025-10-13 10:41:08 +08:00
}
/** 添加参数 */
2025-10-13 10:41:08 +08:00
async function submitForm() {
// 初始化参数列表
if (isEmpty(thingModelParams.value)) {
2025-10-10 20:26:17 +08:00
thingModelParams.value = [];
}
// 校验参数
2025-10-10 20:26:17 +08:00
await paramFormRef.value.validate();
try {
// 构建数据对象
2025-10-10 20:26:17 +08:00
const data = unref(formData);
const item = {
identifier: data.identifier,
name: data.name,
description: data.description,
dataType: data.property.dataType,
paraOrder: 0, // TODO @puhui999: 先写死默认看看后续
direction: props.direction,
dataSpecs:
2025-10-10 20:26:17 +08:00
!!data.property.dataSpecs &&
Object.keys(data.property.dataSpecs).length > 1
? data.property.dataSpecs
: undefined,
2025-10-10 20:26:17 +08:00
dataSpecsList: isEmpty(data.property.dataSpecsList)
? undefined
: data.property.dataSpecsList,
};
// 新增或修改同 identifier 的参数
const existingIndex = thingModelParams.value.findIndex(
2025-10-10 20:26:17 +08:00
(spec) => spec.identifier === data.identifier,
);
if (existingIndex === -1) {
thingModelParams.value.push(item);
} else {
2025-10-10 20:26:17 +08:00
thingModelParams.value[existingIndex] = item;
}
} finally {
2025-10-10 20:26:17 +08:00
dialogVisible.value = false;
}
2025-10-13 10:41:08 +08:00
}
/** 重置表单 */
2025-10-13 10:41:08 +08:00
function resetForm() {
formData.value = {
dataType: IoTDataSpecsDataTypeEnum.INT,
property: {
dataType: IoTDataSpecsDataTypeEnum.INT,
dataSpecs: {
2025-10-10 20:26:17 +08:00
dataType: IoTDataSpecsDataTypeEnum.INT,
},
dataSpecsList: [],
2025-10-10 20:26:17 +08:00
},
};
paramFormRef.value?.resetFields();
2025-10-13 10:41:08 +08:00
}
</script>
2025-10-10 20:26:17 +08:00
<template>
<div
v-for="(item, index) in thingModelParams"
:key="index"
2025-10-13 10:41:08 +08:00
class="w-1/1 px-10px mb-10px flex justify-between bg-gray-100"
2025-10-10 20:26:17 +08:00
>
<span>参数名称{{ item.name }}</span>
<div class="btn">
<Button type="link" @click="openParamForm(item)">编辑</Button>
<Divider type="vertical" />
<Button type="link" danger @click="deleteParamItem(index)">删除</Button>
2025-10-10 20:26:17 +08:00
</div>
</div>
<Button type="link" @click="openParamForm(null)">+新增参数</Button>
2025-10-10 20:26:17 +08:00
<!-- param 表单 -->
<Modal
v-model:open="dialogVisible"
title="新增参数"
:confirm-loading="formLoading"
@ok="submitForm"
>
2025-10-13 10:41:08 +08:00
<Form
2025-10-10 20:26:17 +08:00
ref="paramFormRef"
:model="formData"
:label-col="{ span: 6 }"
:wrapper-col="{ span: 18 }"
2025-10-10 20:26:17 +08:00
>
<Form.Item label="参数名称" name="name">
<Input v-model:value="formData.name" placeholder="请输入功能名称" />
2025-10-13 10:41:08 +08:00
</Form.Item>
<Form.Item label="标识符" name="identifier">
<Input v-model:value="formData.identifier" placeholder="请输入标识符" />
2025-10-13 10:41:08 +08:00
</Form.Item>
2025-10-10 20:26:17 +08:00
<!-- 属性配置 -->
<ThingModelProperty v-model="formData.property" is-params />
2025-10-13 10:41:08 +08:00
</Form>
</Modal>
2025-10-10 20:26:17 +08:00
</template>