修复下拉框选择数据设备Id未生效的问题

This commit is contained in:
ChenYi 2025-07-16 16:11:21 +08:00
parent e12b7cedfa
commit 77e93996a3
3 changed files with 80 additions and 11 deletions

View File

@ -107,14 +107,26 @@ const handleValueChange = (value: string) => {
const selectedDevice = options.value.find(option => option.value === value); const selectedDevice = options.value.find(option => option.value === value);
if (selectedDevice) { if (selectedDevice) {
emit('device-change', selectedDevice); emit('device-change', selectedDevice);
console.log('DeviceSelect 发送设备信息:', selectedDevice);
} }
} else { } else {
emit('device-change', null); emit('device-change', null);
console.log('DeviceSelect 清空设备信息');
} }
}; };
// //
fetchData(); fetchData();
//
defineExpose({
getSelectedDevice: () => {
if (props.value) {
return options.value.find(option => option.value === props.value);
}
return null;
},
});
</script> </script>
<template> <template>

View File

@ -26,6 +26,7 @@ const selectedDeviceInfo = ref<any>(null);
// ID // ID
const fetchDeviceOptions = async () => { const fetchDeviceOptions = async () => {
try { try {
console.log('开始获取设备信息...');
const { data } = await postMetersPage({ const { data } = await postMetersPage({
body: { body: {
pageIndex: 1, pageIndex: 1,
@ -35,7 +36,10 @@ const fetchDeviceOptions = async () => {
if (data?.items) { if (data?.items) {
deviceOptions.value = data.items; deviceOptions.value = data.items;
console.log('设备信息选项:', deviceOptions.value); console.log('设备信息获取成功,总数:', data.items.length);
console.log('设备信息选项前3项:', deviceOptions.value.slice(0, 3));
} else {
console.log('设备信息为空');
} }
} catch (error) { } catch (error) {
console.error('获取设备信息失败:', error); console.error('获取设备信息失败:', error);
@ -47,11 +51,7 @@ const getDeviceInfoById = (deviceId: string) => {
return deviceOptions.value.find((device) => device.id === deviceId); return deviceOptions.value.find((device) => device.id === deviceId);
}; };
//
const handleDeviceChange = (deviceInfo: any) => {
selectedDeviceInfo.value = deviceInfo;
console.log('设备选择变化:', deviceInfo);
};
const route = useRoute(); const route = useRoute();
const { DeviceType, DeviceId, FocusAddress, SystemName } = route.query; const { DeviceType, DeviceId, FocusAddress, SystemName } = route.query;
@ -115,6 +115,57 @@ const formOptions: VbenFormProps = {
relevantFields.has(field), relevantFields.has(field),
); );
// DeviceIdselectedDeviceInfo
if (changedFields.includes('DeviceId')) {
const deviceId = values.DeviceId;
console.log('DeviceId变化:', deviceId);
if (deviceId) {
// deviceOptions
let device = deviceOptions.value.find(d => d.id === deviceId);
// DeviceSelect
if (!device && gridApi?.formApi) {
try {
// DeviceSelect
const deviceSelectRef = gridApi.formApi.getFieldComponentRef('DeviceId');
console.log('DeviceSelect组件实例:', deviceSelectRef);
if (deviceSelectRef && deviceSelectRef.getSelectedDevice) {
device = deviceSelectRef.getSelectedDevice();
console.log('从DeviceSelect组件获取设备信息:', device);
}
} catch (error) {
console.log('无法从DeviceSelect组件获取设备信息:', error);
}
}
if (device) {
selectedDeviceInfo.value = device;
console.log('同步设备信息成功:', device);
} else {
console.log('未找到匹配的设备deviceId:', deviceId);
//
setTimeout(() => {
try {
const deviceSelectRef = gridApi.formApi.getFieldComponentRef('DeviceId');
if (deviceSelectRef && deviceSelectRef.getSelectedDevice) {
const delayedDevice = deviceSelectRef.getSelectedDevice();
if (delayedDevice) {
selectedDeviceInfo.value = delayedDevice;
console.log('延迟获取设备信息成功:', delayedDevice);
}
}
} catch (error) {
console.log('延迟获取设备信息失败:', error);
}
}, 100);
}
} else {
selectedDeviceInfo.value = null;
console.log('DeviceId为空清空selectedDeviceInfo');
}
}
if (hasRelevantChange) { if (hasRelevantChange) {
console.log('表单字段变化:', { values, changedFields }); console.log('表单字段变化:', { values, changedFields });
console.log( console.log(
@ -130,10 +181,6 @@ const formOptions: VbenFormProps = {
}, 0); }, 0);
} }
}, },
//
events: {
handleDeviceChange,
},
}; };
const gridOptions: VxeGridProps<any> = { const gridOptions: VxeGridProps<any> = {
@ -187,20 +234,31 @@ const gridOptions: VxeGridProps<any> = {
let finalFocusAddress = formValues.FocusAddress; let finalFocusAddress = formValues.FocusAddress;
// 使 // 使
console.log('API调用时的selectedDeviceInfo:', selectedDeviceInfo.value);
console.log('API调用时的formValues.DeviceId:', formValues.DeviceId);
const deviceInfo = selectedDeviceInfo.value || (formValues.DeviceId ? getDeviceInfoById(formValues.DeviceId) : null); const deviceInfo = selectedDeviceInfo.value || (formValues.DeviceId ? getDeviceInfoById(formValues.DeviceId) : null);
console.log('最终使用的deviceInfo:', deviceInfo);
if (deviceInfo) { if (deviceInfo) {
finalFocusAddress = deviceInfo.focusAddress; finalFocusAddress = deviceInfo.focusAddress;
console.log('使用focusAddress:', finalFocusAddress);
if (deviceTypeNumber === 10) { if (deviceTypeNumber === 10) {
// 使focusId // 使focusId
if (deviceInfo.focusId) { if (deviceInfo.focusId) {
finalDeviceId = deviceInfo.focusId; finalDeviceId = deviceInfo.focusId;
console.log('集中器类型使用focusId:', finalDeviceId);
} }
} else { } else {
// 使meterId // 使meterId
if (deviceInfo.meterId) { if (deviceInfo.meterId) {
finalDeviceId = deviceInfo.meterId; finalDeviceId = deviceInfo.meterId;
console.log('其他类型使用meterId:', finalDeviceId);
} }
} }
} else {
console.log('没有找到设备信息使用原始DeviceId:', finalDeviceId);
} }
try { try {
const { data } = await postTreeModelDeviceDataInfoPage({ const { data } = await postTreeModelDeviceDataInfoPage({

View File

@ -112,7 +112,6 @@ export const querySchema = computed(() => [
componentProps: { componentProps: {
placeholder: $t('common.pleaseSelect') + $t('abp.log.deviceInfo'), placeholder: $t('common.pleaseSelect') + $t('abp.log.deviceInfo'),
allowClear: true, allowClear: true,
onDeviceChange: 'handleDeviceChange',
}, },
}, },
]); ]);