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

157 lines
4.5 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,
},
},
});
/** 打开 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;
}
// 编辑时回显数据
formData.value = {
identifier: val.identifier,
name: val.name,
description: val.description,
property: {
dataType: val.dataType,
dataSpecs: val.dataSpecs,
2025-10-10 20:26:17 +08:00
dataSpecsList: val.dataSpecsList,
},
};
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,
},
},
};
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">
2025-10-13 10:41:08 +08:00
<Button link type="primary" @click="openParamForm(item)"> 编辑 </Button>
<Divider direction="vertical" />
<Button link danger @click="deleteParamItem(index)"> 删除 </Button>
2025-10-10 20:26:17 +08:00
</div>
</div>
2025-10-13 10:41:08 +08:00
<Button link type="primary" @click="openParamForm(null)"> +新增参数 </Button>
2025-10-10 20:26:17 +08:00
<!-- param 表单 -->
2025-10-13 10:41:08 +08:00
<Modal v-model="dialogVisible" title="新增参数" append-to-body>
<Form
2025-10-10 20:26:17 +08:00
ref="paramFormRef"
v-loading="formLoading"
:model="formData"
label-width="100px"
>
2025-10-13 10:41:08 +08:00
<Form.Item label="参数名称" prop="name">
<Input v-model="formData.name" placeholder="请输入功能名称" />
</Form.Item>
<Form.Item label="标识符" prop="identifier">
<Input v-model="formData.identifier" placeholder="请输入标识符" />
</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>
2025-10-10 20:26:17 +08:00
<template #footer>
2025-10-13 10:41:08 +08:00
<Button :disabled="formLoading" type="primary" @click="submitForm">
2025-10-10 20:26:17 +08:00
2025-10-13 10:41:08 +08:00
</Button>
<Button @click="dialogVisible = false"> </Button>
2025-10-10 20:26:17 +08:00
</template>
2025-10-13 10:41:08 +08:00
</Modal>
2025-10-10 20:26:17 +08:00
</template>