From 113f24cc9d511f7a2e957f7b495ad344174b96ca Mon Sep 17 00:00:00 2001 From: ChenYi <296215406@outlook.com> Date: Thu, 17 Jul 2025 11:47:00 +0800 Subject: [PATCH] =?UTF-8?q?onenet=E6=97=A5=E5=BF=97=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/views/dataManger/onenetLog/index.vue | 136 +++++++++++------- 1 file changed, 81 insertions(+), 55 deletions(-) diff --git a/apps/web-antd/src/views/dataManger/onenetLog/index.vue b/apps/web-antd/src/views/dataManger/onenetLog/index.vue index bd7eb54..84273ca 100644 --- a/apps/web-antd/src/views/dataManger/onenetLog/index.vue +++ b/apps/web-antd/src/views/dataManger/onenetLog/index.vue @@ -63,7 +63,8 @@ const getDeviceInfoById = (deviceId: string) => { meterId: device.meterId, focusId: device.focusId, systemName: device.systemName, - keys: Object.keys(device) + keys: Object.keys(device), + fullDevice: JSON.parse(JSON.stringify(device)) // 展开完整对象 }))); const device = deviceOptions.value.find((device) => device.id === deviceId); @@ -99,40 +100,65 @@ const formOptions: VbenFormProps = { // 添加表单值变化的处理函数 handleValuesChange: async (values, changedFields) => { console.log('handleValuesChange 被触发:', { values, changedFields }); - console.log('deviceSelectRef.value:', deviceSelectRef.value); - + console.log('当前所有表单值:', values); + console.log('DeviceType:', values.DeviceType); + console.log('IoTDataType:', values.IoTDataType); + // 当 DeviceId 发生变化时,更新 selectedDeviceInfo if (changedFields.includes('DeviceId')) { const deviceId = values.DeviceId; console.log('DeviceId 变化:', deviceId); - + if (deviceId) { - // 尝试从 DeviceSelect 组件中获取 - if ( - deviceSelectRef.value && - typeof deviceSelectRef.value.getSelectedDevice === 'function' - ) { - const device = deviceSelectRef.value.getSelectedDevice(); - if (device) { - selectedDeviceInfo.value = device; - console.log('handleValuesChange 更新设备信息:', device); + // 先尝试从 deviceOptions 中查找(备用方案) + let device = deviceOptions.value.length > 0 ? deviceOptions.value.find(d => d.id === deviceId) : null; + + // 如果没找到,尝试从 DeviceSelect 组件中获取 + if (!device && gridApi?.formApi) { + try { + // 获取 DeviceSelect 组件的实例 + const deviceSelectRef = gridApi.formApi.getFieldComponentRef('DeviceId'); + if (deviceSelectRef && deviceSelectRef.getSelectedDevice) { + device = deviceSelectRef.getSelectedDevice(); + } + } catch (error) { + console.log('获取组件实例失败:', error); } + } + + if (device) { + selectedDeviceInfo.value = device; + console.log('handleValuesChange 更新设备信息:', device); } else { - console.log('deviceSelectRef 不可用或 getSelectedDevice 方法不存在'); + // 如果还是没找到,尝试延迟获取(组件可能还没完全更新) + 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; } } - + // 当任何相关字段发生变化时,刷新表格数据 const relevantFields = new Set([ 'DeviceId', 'DeviceType', - 'EndCreationTime', 'IoTDataType', - 'StartCreationTime', 'SystemName', + 'StartCreationTime', + 'EndCreationTime', ]); const hasRelevantChange = changedFields.some((field) => relevantFields.has(field), @@ -141,9 +167,8 @@ const formOptions: VbenFormProps = { if (hasRelevantChange) { // 使用 setTimeout 确保表单值已经完全更新 setTimeout(async () => { - if (gridApi) { - gridApi.reload(); - } + const latestValues = await gridApi.formApi.getValues(); + gridApi.reload(latestValues); }, 0); } }, @@ -179,54 +204,55 @@ const gridOptions: VxeGridProps = { proxyConfig: { ajax: { query: async ({ page }, formValues) => { + // 获取当前表单值,如果formValues为空则从表单API获取 + const currentFormValues = formValues || (gridApi?.formApi ? await gridApi.formApi.getValues() : {}) || {}; + + console.log('查询时的 formValues:', formValues); + console.log('查询时的 currentFormValues:', currentFormValues); + // 获取选中的设备信息 - let deviceId = formValues?.DeviceId || ''; + let deviceId = currentFormValues.DeviceId || ''; let systemName = ''; - const deviceType = formValues?.DeviceType || ''; - - console.log('查询参数:', { deviceId, deviceType, formValues }); - - // 获取设备详细信息,根据设备类型获取正确的 id - console.log('deviceOptions 状态:', { - hasDeviceOptions: !!deviceOptions.value, - deviceOptionsLength: deviceOptions.value?.length || 0, - }); - - if (deviceId && deviceOptions.value && deviceOptions.value.length > 0) { - const deviceInfo = getDeviceInfoById(deviceId); - console.log('getDeviceInfoById 结果:', deviceInfo); - if (deviceInfo) { - console.log('找到设备信息:', deviceInfo); - systemName = deviceInfo.systemName || ''; - // 根据设备类型获取正确的 id - if (Number(deviceType) === 10) { - // 集中器类型使用 focusId - deviceId = deviceInfo.focusId || deviceId; - } else { - // 其他设备类型使用 meterId - deviceId = deviceInfo.meterId || deviceId; - } + const deviceType = currentFormValues.DeviceType || ''; + + console.log('查询参数:', { deviceId, deviceType, currentFormValues }); + console.log('currentFormValues.DeviceId:', currentFormValues.DeviceId); + console.log('currentFormValues.DeviceType:', currentFormValues.DeviceType); + console.log('currentFormValues.IoTDataType:', currentFormValues.IoTDataType); + + // 优先使用选中的设备信息 + const deviceInfo = selectedDeviceInfo.value || (currentFormValues.DeviceId && deviceOptions.value.length > 0 ? getDeviceInfoById(currentFormValues.DeviceId) : null); + + if (deviceInfo) { + console.log('使用设备信息:', deviceInfo); + systemName = deviceInfo.systemName || ''; + // 根据设备类型获取正确的 id + if (Number(deviceType) === 10) { + // 集中器类型使用 focusId + deviceId = deviceInfo.focusId || deviceId; } else { - console.log('未找到设备信息,使用原始 deviceId:', deviceId); + // 其他设备类型使用 meterId + deviceId = deviceInfo.meterId || deviceId; } } else { - console.log( - 'deviceOptions 未加载或为空,使用原始 deviceId:', - deviceId, - ); + console.log('没有找到设备信息,使用原始 deviceId:', deviceId); } + // 构建查询参数 const queryParams = { pageIndex: page.currentPage, pageSize: page.pageSize, - SearchKeyword: formValues?.SearchKeyword || '', + SearchKeyword: currentFormValues.SearchKeyword || '', DeviceType: deviceType, - IoTDataType: formValues?.IoTDataType || '', + IoTDataType: currentFormValues.IoTDataType || '', DeviceId: deviceId, - SystemName: systemName || formValues?.SystemName || '', - StartCreationTime: formatDate(formValues?.StartCreationTime), - EndCreationTime: formatDate(formValues?.EndCreationTime), + SystemName: systemName || currentFormValues.SystemName || '', + StartCreationTime: formatDate(currentFormValues.StartCreationTime), + EndCreationTime: formatDate(currentFormValues.EndCreationTime), }; + + console.log('最终查询参数:', queryParams); + if (DeviceType) queryParams.DeviceType = DeviceType; if (DeviceId) queryParams.DeviceId = DeviceId; const { data } = await postOneNETLogInfoPage({