2025-09-20 16:37:26 +08:00
|
|
|
|
<template>
|
|
|
|
|
|
<el-dialog
|
|
|
|
|
|
:model-value="visible"
|
|
|
|
|
|
:title="title"
|
|
|
|
|
|
@close="handleClose"
|
|
|
|
|
|
:close-on-click-modal="false"
|
|
|
|
|
|
width="600px"
|
|
|
|
|
|
>
|
|
|
|
|
|
<el-form
|
|
|
|
|
|
ref="formRef"
|
|
|
|
|
|
:model="formData"
|
|
|
|
|
|
:rules="rules"
|
|
|
|
|
|
label-width="120px"
|
|
|
|
|
|
@submit.prevent
|
|
|
|
|
|
>
|
|
|
|
|
|
<!-- 基础信息 -->
|
2025-09-30 22:20:50 +08:00
|
|
|
|
<el-form-item label="名称" prop="name">
|
2025-09-20 16:37:26 +08:00
|
|
|
|
<el-input v-model="formData.name" />
|
|
|
|
|
|
</el-form-item>
|
|
|
|
|
|
|
2025-09-30 22:20:50 +08:00
|
|
|
|
<el-form-item label="类型" prop="type">
|
|
|
|
|
|
<el-select v-model="formData.type" @change="handleTypeChange" :disabled="isEdit">
|
2025-09-20 16:37:26 +08:00
|
|
|
|
<el-option label="区域主控" value="area_controller" />
|
|
|
|
|
|
<el-option label="普通设备" value="device" />
|
|
|
|
|
|
</el-select>
|
|
|
|
|
|
</el-form-item>
|
|
|
|
|
|
|
2025-09-30 22:20:50 +08:00
|
|
|
|
<el-form-item label="位置描述" prop="location">
|
2025-09-20 16:37:26 +08:00
|
|
|
|
<el-input v-model="formData.location" type="textarea" />
|
|
|
|
|
|
</el-form-item>
|
|
|
|
|
|
|
|
|
|
|
|
<!-- 区域主控类型额外字段 -->
|
|
|
|
|
|
<div v-if="formData.type === 'area_controller'">
|
2025-09-30 22:20:50 +08:00
|
|
|
|
<el-form-item label="网络ID" prop="network_id">
|
|
|
|
|
|
<el-input v-model="formData.network_id" />
|
2025-09-20 16:37:26 +08:00
|
|
|
|
</el-form-item>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<!-- 普通设备类型额外字段 -->
|
|
|
|
|
|
<div v-if="formData.type === 'device'">
|
2025-09-30 22:20:50 +08:00
|
|
|
|
<el-form-item label="所属区域主控" prop="area_controller_id">
|
|
|
|
|
|
<el-select v-model="formData.area_controller_id" placeholder="请选择区域主控">
|
2025-09-20 16:37:26 +08:00
|
|
|
|
<el-option
|
|
|
|
|
|
v-for="controller in areaControllers"
|
|
|
|
|
|
:key="controller.id"
|
|
|
|
|
|
:label="controller.name"
|
|
|
|
|
|
:value="controller.id"
|
|
|
|
|
|
/>
|
|
|
|
|
|
</el-select>
|
|
|
|
|
|
</el-form-item>
|
|
|
|
|
|
|
2025-09-30 22:20:50 +08:00
|
|
|
|
<el-form-item label="设备模板" prop="device_template_id">
|
|
|
|
|
|
<el-select v-model="formData.device_template_id" placeholder="请选择设备模板">
|
|
|
|
|
|
<!-- 假设设备模板列表从API获取或硬编码 -->
|
|
|
|
|
|
<el-option label="温度传感器" :value="1" />
|
|
|
|
|
|
<el-option label="湿度传感器" :value="2" />
|
|
|
|
|
|
<el-option label="氨气传感器" :value="3" />
|
|
|
|
|
|
<el-option label="饲料阀门" :value="4" />
|
|
|
|
|
|
<el-option label="风扇" :value="5" />
|
|
|
|
|
|
<el-option label="水帘" :value="6" />
|
2025-09-20 16:37:26 +08:00
|
|
|
|
</el-select>
|
|
|
|
|
|
</el-form-item>
|
|
|
|
|
|
|
2025-09-30 22:20:50 +08:00
|
|
|
|
<el-form-item label="485总线号" prop="properties.bus_number">
|
2025-09-20 16:37:26 +08:00
|
|
|
|
<el-input-number
|
2025-09-30 22:20:50 +08:00
|
|
|
|
v-model="formData.properties.bus_number"
|
2025-09-20 16:37:26 +08:00
|
|
|
|
:min="0"
|
|
|
|
|
|
controls-position="right"
|
|
|
|
|
|
style="width: 100%"
|
|
|
|
|
|
/>
|
|
|
|
|
|
</el-form-item>
|
|
|
|
|
|
|
2025-09-30 22:20:50 +08:00
|
|
|
|
<el-form-item label="485总线地址" prop="properties.bus_address">
|
2025-09-20 16:37:26 +08:00
|
|
|
|
<el-input-number
|
2025-09-30 22:20:50 +08:00
|
|
|
|
v-model="formData.properties.bus_address"
|
2025-09-20 16:37:26 +08:00
|
|
|
|
:min="0"
|
|
|
|
|
|
controls-position="right"
|
|
|
|
|
|
style="width: 100%"
|
|
|
|
|
|
/>
|
|
|
|
|
|
</el-form-item>
|
|
|
|
|
|
|
2025-09-30 22:20:50 +08:00
|
|
|
|
<el-form-item label="继电器通道号" prop="properties.relay_channel">
|
2025-09-20 16:37:26 +08:00
|
|
|
|
<el-input-number
|
2025-09-30 22:20:50 +08:00
|
|
|
|
v-model="formData.properties.relay_channel"
|
2025-09-20 16:37:26 +08:00
|
|
|
|
:min="0"
|
|
|
|
|
|
controls-position="right"
|
|
|
|
|
|
style="width: 100%"
|
|
|
|
|
|
/>
|
|
|
|
|
|
</el-form-item>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</el-form>
|
|
|
|
|
|
|
|
|
|
|
|
<template #footer>
|
|
|
|
|
|
<span class="dialog-footer">
|
|
|
|
|
|
<el-button @click="handleClose">取消</el-button>
|
|
|
|
|
|
<el-button type="primary" @click="handleSubmit" :loading="loading">确定</el-button>
|
|
|
|
|
|
</span>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
</el-dialog>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
|
|
<script>
|
2025-09-30 22:20:50 +08:00
|
|
|
|
import { ref, reactive, onMounted, watch, computed, nextTick } from 'vue';
|
2025-09-20 16:37:26 +08:00
|
|
|
|
import { ElMessage } from 'element-plus';
|
2025-09-30 22:20:50 +08:00
|
|
|
|
import { AreaControllerApi, DeviceApi } from '../api/device.js';
|
2025-09-20 16:37:26 +08:00
|
|
|
|
|
|
|
|
|
|
export default {
|
|
|
|
|
|
name: 'DeviceForm',
|
|
|
|
|
|
props: {
|
|
|
|
|
|
visible: {
|
|
|
|
|
|
type: Boolean,
|
|
|
|
|
|
default: false
|
|
|
|
|
|
},
|
|
|
|
|
|
deviceData: {
|
|
|
|
|
|
type: Object,
|
|
|
|
|
|
default: () => ({})
|
|
|
|
|
|
},
|
|
|
|
|
|
isEdit: {
|
|
|
|
|
|
type: Boolean,
|
|
|
|
|
|
default: false
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
emits: ['update:visible', 'success', 'cancel'],
|
|
|
|
|
|
setup(props, { emit }) {
|
|
|
|
|
|
const formRef = ref(null);
|
|
|
|
|
|
const loading = ref(false);
|
|
|
|
|
|
const areaControllers = ref([]);
|
|
|
|
|
|
|
2025-09-30 22:20:50 +08:00
|
|
|
|
const initialFormData = () => ({
|
2025-09-20 16:37:26 +08:00
|
|
|
|
id: '',
|
|
|
|
|
|
name: '',
|
2025-09-30 22:20:50 +08:00
|
|
|
|
type: 'device', // 默认创建普通设备
|
2025-09-20 16:37:26 +08:00
|
|
|
|
location: '',
|
2025-09-30 22:20:50 +08:00
|
|
|
|
network_id: '', // 区域主控字段
|
|
|
|
|
|
area_controller_id: '', // 普通设备字段
|
|
|
|
|
|
device_template_id: '', // 普通设备字段
|
|
|
|
|
|
properties: { // 嵌套的properties对象
|
|
|
|
|
|
bus_number: 0,
|
|
|
|
|
|
bus_address: 0,
|
|
|
|
|
|
relay_channel: 0,
|
|
|
|
|
|
}
|
2025-09-20 16:37:26 +08:00
|
|
|
|
});
|
2025-09-30 22:20:50 +08:00
|
|
|
|
|
|
|
|
|
|
const formData = reactive(initialFormData());
|
2025-09-20 16:37:26 +08:00
|
|
|
|
|
2025-09-30 22:20:50 +08:00
|
|
|
|
const rules = computed(() => ({
|
2025-09-20 16:37:26 +08:00
|
|
|
|
name: [
|
2025-09-30 22:20:50 +08:00
|
|
|
|
{ required: true, message: '请输入名称', trigger: 'blur' }
|
2025-09-20 16:37:26 +08:00
|
|
|
|
],
|
|
|
|
|
|
type: [
|
2025-09-30 22:20:50 +08:00
|
|
|
|
{ required: true, message: '请选择类型', trigger: 'change' }
|
2025-09-20 16:37:26 +08:00
|
|
|
|
],
|
|
|
|
|
|
location: [
|
2025-09-30 22:20:50 +08:00
|
|
|
|
{ required: true, message: '请输入位置描述', trigger: 'blur' }
|
2025-09-20 16:37:26 +08:00
|
|
|
|
],
|
2025-09-30 22:20:50 +08:00
|
|
|
|
network_id: [
|
|
|
|
|
|
{ required: formData.type === 'area_controller', message: '请输入网络ID', trigger: 'blur' }
|
2025-09-20 16:37:26 +08:00
|
|
|
|
],
|
2025-09-30 22:20:50 +08:00
|
|
|
|
area_controller_id: [
|
|
|
|
|
|
{ required: formData.type === 'device', message: '请选择所属区域主控', trigger: 'change' }
|
2025-09-20 16:37:26 +08:00
|
|
|
|
],
|
2025-09-30 22:20:50 +08:00
|
|
|
|
device_template_id: [
|
|
|
|
|
|
{ required: formData.type === 'device', message: '请选择设备模板', trigger: 'change' }
|
2025-09-20 16:37:26 +08:00
|
|
|
|
],
|
2025-09-30 22:20:50 +08:00
|
|
|
|
'properties.bus_number': [
|
|
|
|
|
|
{ required: formData.type === 'device', message: '请输入485总线号', trigger: 'blur' }
|
2025-09-20 16:37:26 +08:00
|
|
|
|
],
|
2025-09-30 22:20:50 +08:00
|
|
|
|
'properties.bus_address': [
|
|
|
|
|
|
{ required: formData.type === 'device', message: '请输入485总线地址', trigger: 'blur' }
|
2025-09-20 16:37:26 +08:00
|
|
|
|
],
|
2025-09-30 22:20:50 +08:00
|
|
|
|
'properties.relay_channel': [
|
|
|
|
|
|
{ required: formData.type === 'device', message: '请输入继电器通道号', trigger: 'blur' }
|
2025-09-20 16:37:26 +08:00
|
|
|
|
]
|
2025-09-30 22:20:50 +08:00
|
|
|
|
}));
|
2025-09-20 16:37:26 +08:00
|
|
|
|
|
|
|
|
|
|
const title = computed(() => {
|
|
|
|
|
|
return props.isEdit ? '编辑设备' : '添加设备';
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
const handleTypeChange = (value) => {
|
2025-09-30 22:20:50 +08:00
|
|
|
|
// 清除不同类型特有的字段
|
2025-09-20 16:37:26 +08:00
|
|
|
|
if (value === 'area_controller') {
|
2025-09-30 22:20:50 +08:00
|
|
|
|
formData.area_controller_id = '';
|
|
|
|
|
|
formData.device_template_id = '';
|
|
|
|
|
|
formData.properties = { bus_number: 0, bus_address: 0, relay_channel: 0 };
|
2025-09-20 16:37:26 +08:00
|
|
|
|
} else {
|
2025-09-30 22:20:50 +08:00
|
|
|
|
formData.network_id = '';
|
2025-09-20 16:37:26 +08:00
|
|
|
|
}
|
2025-09-30 22:20:50 +08:00
|
|
|
|
// 触发验证规则更新
|
|
|
|
|
|
nextTick(() => {
|
|
|
|
|
|
formRef.value.clearValidate();
|
|
|
|
|
|
});
|
2025-09-20 16:37:26 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const loadAreaControllers = async () => {
|
|
|
|
|
|
try {
|
2025-09-30 22:20:50 +08:00
|
|
|
|
const response = await AreaControllerApi.list();
|
|
|
|
|
|
areaControllers.value = response.data || [];
|
2025-09-20 16:37:26 +08:00
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('获取区域主控列表失败:', error);
|
|
|
|
|
|
areaControllers.value = [];
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const handleClose = () => {
|
|
|
|
|
|
emit('update:visible', false);
|
|
|
|
|
|
emit('cancel');
|
2025-09-30 22:20:50 +08:00
|
|
|
|
// 重置表单
|
|
|
|
|
|
Object.assign(formData, initialFormData());
|
|
|
|
|
|
formRef.value.resetFields();
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const getSubmitData = () => {
|
|
|
|
|
|
const data = {
|
|
|
|
|
|
name: formData.name,
|
|
|
|
|
|
location: formData.location,
|
|
|
|
|
|
properties: formData.properties // properties直接作为对象传递
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
if (formData.type === 'area_controller') {
|
|
|
|
|
|
data.network_id = formData.network_id;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
data.area_controller_id = formData.area_controller_id;
|
|
|
|
|
|
data.device_template_id = formData.device_template_id;
|
|
|
|
|
|
}
|
|
|
|
|
|
return data;
|
2025-09-20 16:37:26 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const handleSubmit = async () => {
|
|
|
|
|
|
await formRef.value.validate(async (valid) => {
|
|
|
|
|
|
if (valid) {
|
|
|
|
|
|
loading.value = true;
|
|
|
|
|
|
try {
|
|
|
|
|
|
let result;
|
2025-09-30 22:20:50 +08:00
|
|
|
|
const submitData = getSubmitData();
|
|
|
|
|
|
|
2025-09-20 16:37:26 +08:00
|
|
|
|
if (props.isEdit) {
|
2025-09-30 22:20:50 +08:00
|
|
|
|
if (formData.type === 'area_controller') {
|
|
|
|
|
|
result = await AreaControllerApi.update(formData.id, submitData);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
result = await DeviceApi.update(formData.id, submitData);
|
|
|
|
|
|
}
|
2025-09-20 16:37:26 +08:00
|
|
|
|
} else {
|
2025-09-30 22:20:50 +08:00
|
|
|
|
if (formData.type === 'area_controller') {
|
|
|
|
|
|
result = await AreaControllerApi.create(submitData);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
result = await DeviceApi.create(submitData);
|
|
|
|
|
|
}
|
2025-09-20 16:37:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-30 22:20:50 +08:00
|
|
|
|
// 适配DeviceList的树形结构,添加type和parent_id
|
|
|
|
|
|
const processedResult = {
|
|
|
|
|
|
...result.data,
|
|
|
|
|
|
type: formData.type
|
|
|
|
|
|
};
|
|
|
|
|
|
if (formData.type === 'device') {
|
|
|
|
|
|
processedResult.parent_id = processedResult.area_controller_id;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
emit('success', processedResult);
|
2025-09-20 16:37:26 +08:00
|
|
|
|
handleClose();
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('保存设备失败:', error);
|
2025-09-30 22:20:50 +08:00
|
|
|
|
ElMessage.error(props.isEdit ? '编辑设备失败: ' + (error.message || '未知错误') : '创建设备失败: ' + (error.message || '未知错误'));
|
2025-09-20 16:37:26 +08:00
|
|
|
|
} finally {
|
|
|
|
|
|
loading.value = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
watch(() => props.deviceData, (newVal) => {
|
|
|
|
|
|
if (newVal && Object.keys(newVal).length > 0) {
|
2025-09-30 22:20:50 +08:00
|
|
|
|
// 重置表单以清除旧数据和验证状态
|
|
|
|
|
|
Object.assign(formData, initialFormData());
|
|
|
|
|
|
nextTick(() => {
|
|
|
|
|
|
formRef.value.clearValidate();
|
2025-09-20 16:37:26 +08:00
|
|
|
|
});
|
2025-09-30 22:20:50 +08:00
|
|
|
|
|
|
|
|
|
|
formData.id = newVal.id;
|
|
|
|
|
|
formData.name = newVal.name;
|
|
|
|
|
|
formData.type = newVal.type;
|
|
|
|
|
|
formData.location = newVal.location;
|
|
|
|
|
|
|
|
|
|
|
|
if (newVal.type === 'area_controller') {
|
|
|
|
|
|
formData.network_id = newVal.network_id || '';
|
|
|
|
|
|
} else if (newVal.type === 'device') {
|
|
|
|
|
|
formData.area_controller_id = newVal.area_controller_id || newVal.parent_id || '';
|
|
|
|
|
|
formData.device_template_id = newVal.device_template_id || '';
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-20 16:37:26 +08:00
|
|
|
|
// 处理properties对象的数据填充
|
|
|
|
|
|
if (newVal.properties) {
|
2025-09-30 22:20:50 +08:00
|
|
|
|
// 确保properties是一个对象,如果API返回的是字符串则尝试解析
|
|
|
|
|
|
const propsData = typeof newVal.properties === 'string' ? JSON.parse(newVal.properties) : newVal.properties;
|
|
|
|
|
|
Object.assign(formData.properties, propsData);
|
2025-09-20 16:37:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
} else {
|
2025-09-30 22:20:50 +08:00
|
|
|
|
// 如果没有传入deviceData,则重置为初始状态
|
|
|
|
|
|
Object.assign(formData, initialFormData());
|
|
|
|
|
|
nextTick(() => {
|
|
|
|
|
|
formRef.value.clearValidate();
|
2025-09-20 16:37:26 +08:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}, { immediate: true });
|
|
|
|
|
|
|
2025-09-20 17:30:45 +08:00
|
|
|
|
watch(() => props.visible, (newVal) => {
|
|
|
|
|
|
if (newVal) {
|
|
|
|
|
|
loadAreaControllers();
|
2025-09-30 22:20:50 +08:00
|
|
|
|
// 如果是新增,确保type是默认值,且清空所有字段
|
|
|
|
|
|
if (!props.isEdit) {
|
|
|
|
|
|
Object.assign(formData, initialFormData());
|
|
|
|
|
|
nextTick(() => {
|
|
|
|
|
|
formRef.value.clearValidate();
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2025-09-20 17:30:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
}, { immediate: true });
|
|
|
|
|
|
|
2025-09-20 16:37:26 +08:00
|
|
|
|
onMounted(() => {
|
|
|
|
|
|
loadAreaControllers();
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
formRef,
|
|
|
|
|
|
loading,
|
|
|
|
|
|
areaControllers,
|
|
|
|
|
|
formData,
|
|
|
|
|
|
rules,
|
|
|
|
|
|
title,
|
|
|
|
|
|
handleTypeChange,
|
|
|
|
|
|
handleClose,
|
|
|
|
|
|
handleSubmit
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
|
|
<style scoped>
|
|
|
|
|
|
.dialog-footer {
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
justify-content: flex-end;
|
|
|
|
|
|
gap: 10px;
|
|
|
|
|
|
}
|
|
|
|
|
|
</style>
|