diff --git a/src/views/index.vue b/src/views/index.vue index f8a9287..92a6409 100644 --- a/src/views/index.vue +++ b/src/views/index.vue @@ -303,6 +303,7 @@ export default { selectedNodeName: null, // 当前选中的节点名称 selectedNodeType: null, // 当前选中的节点类型 selectedNodeCode: null, // 当前选中的节点Code + isParentNode: false, // 是否为父节点(有子节点) deviceTreeProps: { children: 'children', label: 'label' @@ -362,68 +363,54 @@ export default { this.loading = true try { console.log('=== 开始加载节点数据 ===') - console.log('节点ID:', nodeId) + console.log('节点ID:', nodeId, '是否为父节点:', this.isParentNode) - // 同时获取当前节点和子节点的数据 - const [currentNodeResponse, childNodesResponse] = await Promise.all([ - // 获取当前节点自身的数据(通过获取所有数据然后筛选) - getLatestRecords(), - // 获取子节点的数据 - getLatestRecordsByParentId(nodeId) - ]) + // 判断是否为父节点(有子节点的节点) + const isParentNode = this.isParentNode let allDeviceData = [] - // 处理当前节点自身的数据 - if (currentNodeResponse.code === 200) { - const currentNodeData = currentNodeResponse.data || [] - console.log('所有设备数据总数:', currentNodeData.length) - console.log('当前节点信息 - ID:', this.selectedNodeId, '名称:', this.selectedNodeName, 'Code:', this.selectedNodeCode) + if (isParentNode) { + // 父节点:只获取子节点的数据,不显示父节点自身(父节点通常是虚拟设备) + const childNodesResponse = await getLatestRecordsByParentId(nodeId) + if (childNodesResponse.code === 200) { + const childNodesData = childNodesResponse.data || [] + console.log('子节点原始数据数量:', childNodesData.length) - // 筛选出当前节点对应的设备数据 - // 优先通过节点code匹配设备monitorId(叶子节点的正确匹配方式) - const currentDevices = currentNodeData.filter(device => { - const matchByCode = this.selectedNodeCode && device.monitorId === this.selectedNodeCode - const matchById = device.monitorId === nodeId - const matchByName = device.monitorName === this.selectedNodeName - const isValid = device.monitorName !== '胶东机场' && - device.monitorId && - device.monitorName + const childDevices = childNodesData.filter(device => { + const matches = device.monitorName !== '胶东机场' && + device.monitorId && + device.monitorName + return matches + }) + console.log('子节点有效设备数量:', childDevices.length) + allDeviceData = childDevices + } + } else { + // 叶子节点:获取当前节点自身的数据 + const currentNodeResponse = await getLatestRecords() + if (currentNodeResponse.code === 200) { + const currentNodeData = currentNodeResponse.data || [] + console.log('所有设备数据总数:', currentNodeData.length) + console.log('当前节点信息 - ID:', this.selectedNodeId, '名称:', this.selectedNodeName, 'Code:', this.selectedNodeCode) - const matches = (matchByCode || matchById || matchByName) && isValid + // 筛选出当前节点对应的设备数据 + const currentDevices = currentNodeData.filter(device => { + const matchByCode = this.selectedNodeCode && device.monitorId === this.selectedNodeCode + const matchById = device.monitorId === nodeId + const matchByName = device.monitorName === this.selectedNodeName + const isValid = device.monitorName !== '胶东机场' && + device.monitorId && + device.monitorName - if (matches) { - console.log('找到当前节点设备:', device.monitorName, device.monitorId, - matchByCode ? '(通过Code匹配)' : - matchById ? '(通过ID匹配)' : '(通过名称匹配)') - } - return matches - }) - console.log('当前节点设备数量:', currentDevices.length) - allDeviceData = [...allDeviceData, ...currentDevices] + return (matchByCode || matchById || matchByName) && isValid + }) + console.log('当前节点设备数量:', currentDevices.length) + allDeviceData = currentDevices + } } - // 处理子节点数据 - if (childNodesResponse.code === 200) { - const childNodesData = childNodesResponse.data || [] - console.log('子节点原始数据数量:', childNodesData.length) - - const childDevices = childNodesData.filter(device => { - const matches = device.monitorName !== '胶东机场' && - device.monitorId && - device.monitorName - if (matches) { - console.log('找到子节点设备:', device.monitorName, device.monitorId) - } - return matches - }) - console.log('子节点有效设备数量:', childDevices.length) - allDeviceData = [...allDeviceData, ...childDevices] - } - - console.log('合并后设备数据总数:', allDeviceData.length) - - + console.log('设备数据总数:', allDeviceData.length) // 去重处理(基于monitorId) const uniqueDevices = [] @@ -438,18 +425,6 @@ export default { this.deviceList = uniqueDevices console.log('最终设备列表数量:', this.deviceList.length) - - // 统计当前节点和子节点的设备数量 - const currentNodeDeviceCount = this.deviceList.filter(d => { - return (this.selectedNodeCode && d.monitorId === this.selectedNodeCode) || - d.monitorId === nodeId || - d.monitorName === this.selectedNodeName - }).length - - const childNodeDeviceCount = this.deviceList.length - currentNodeDeviceCount - - console.log('当前节点数据数量:', currentNodeDeviceCount) - console.log('子节点数据数量:', childNodeDeviceCount) console.log('=== 数据加载完成 ===') } catch (error) { @@ -491,8 +466,10 @@ export default { this.selectedNodeName = data.label this.selectedNodeType = data.type this.selectedNodeCode = data.code // 添加节点code字段 + // 判断是否为父节点:有children且children不为空 + this.isParentNode = !!(data.children && data.children.length > 0) - console.log('选中节点ID:', this.selectedNodeId, '节点名称:', this.selectedNodeName, '节点类型:', this.selectedNodeType, '节点Code:', this.selectedNodeCode) + console.log('选中节点ID:', this.selectedNodeId, '节点名称:', this.selectedNodeName, '节点类型:', this.selectedNodeType, '节点Code:', this.selectedNodeCode, '是否父节点:', this.isParentNode) // 根据选中的节点ID加载该节点下的设备数据 this.loadDeviceDataByNode(this.selectedNodeId)