diff --git a/src/components/room/roomTreeDiv.vue b/src/components/room/roomTreeDiv.vue index c5129b6..48c3314 100644 --- a/src/components/room/roomTreeDiv.vue +++ b/src/components/room/roomTreeDiv.vue @@ -25,7 +25,9 @@ export default { children: 'children', label: 'text' }, - communityId: '' + communityId: '', + lastSelected: {}, + isInitialized: false // 添加初始化标志 } }, created() { @@ -34,6 +36,17 @@ export default { }, methods: { async loadTreeData() { + // 加载上次选择的房屋信息 + let lastSelected = localStorage.getItem('lastSelectedRoom'); + if (lastSelected) { + try { + this.lastSelected = JSON.parse(lastSelected); + } catch (error) { + console.error('解析lastSelected失败:', error); + localStorage.removeItem('lastSelectedRoom'); + } + } + try { const units = await queryUnits({ communityId: this.communityId @@ -73,11 +86,94 @@ export default { this.treeData = Object.values(floorMap) this.$nextTick(() => { - this.expandAndSelectFirstRoom() + // 如果有上次选择的房屋,则展开并选择;否则选择第一个房屋 + if (this.lastSelected && this.lastSelected.floorId && this.lastSelected.unitId) { + this.expandAndSelectLastRoom() + } else { + this.expandAndSelectFirstRoom() + } }) }, + // 新增:展开并选择上次选择的房屋 + async expandAndSelectLastRoom() { + if (!this.lastSelected || !this.lastSelected.floorId || !this.lastSelected.unitId) { + this.expandAndSelectFirstRoom() + return + } + + const treeRef = this.$refs.tree + if (!treeRef || !treeRef.store) { + // 如果树还没有完全初始化,延迟执行 + setTimeout(() => this.expandAndSelectLastRoom(), 100) + return + } + + try { + // 查找对应的楼栋和单元 + const floorNode = this.treeData.find(floor => floor.floorId === this.lastSelected.floorId) + if (!floorNode) { + console.warn('未找到对应的楼栋:', this.lastSelected.floorId) + this.expandAndSelectFirstRoom() + return + } + + const unitNode = floorNode.children.find(unit => unit.unitId === this.lastSelected.unitId) + if (!unitNode) { + console.warn('未找到对应的单元:', this.lastSelected.unitId) + this.expandAndSelectFirstRoom() + return + } + + // 展开楼栋和单元 + const floorTreeNode = treeRef.store.nodesMap[floorNode.id] + const unitTreeNode = treeRef.store.nodesMap[unitNode.id] + + if (floorTreeNode) { + floorTreeNode.expanded = true + } + if (unitTreeNode) { + unitTreeNode.expanded = true + } + + // 加载房屋数据 + await this.loadRooms(unitNode, { data: unitNode }) + + // 选择上次选择的房屋 + if (this.lastSelected.roomId) { + this.selectSpecificRoom(this.lastSelected.roomId) + } + + this.isInitialized = true + } catch (error) { + console.error('展开上次选择的房屋失败:', error) + this.expandAndSelectFirstRoom() + } + }, + // 新增:选择特定的房屋 + selectSpecificRoom(roomId) { + const treeRef = this.$refs.tree + if (!treeRef) return + + const roomNodeId = `r_${roomId}` + const roomTreeNode = treeRef.store.nodesMap[roomNodeId] + + if (roomTreeNode) { + // 设置当前选中节点 + treeRef.setCurrentKey(roomNodeId) + + // 触发选择事件 + this.$emit('selectRoom', { + roomId: roomId, + roomName: this.lastSelected.roomName || '' + }) + } else { + // 如果房屋节点还没有加载,等待加载完成后再选择 + setTimeout(() => this.selectSpecificRoom(roomId), 200) + } + }, async expandAndSelectFirstRoom() { if (!this.treeData.length) return + const firstFloor = this.treeData[0] if (!firstFloor.children || !firstFloor.children.length) return const firstUnit = firstFloor.children[0] @@ -87,6 +183,7 @@ export default { treeRef.store.nodesMap[firstUnit.id] && (treeRef.store.nodesMap[firstUnit.id].expanded = true) } await this.loadRooms(firstUnit, { data: firstUnit }) + this.isInitialized = true }, async handleNodeClick(data, node) { if (data.id.startsWith('u_')) { @@ -95,12 +192,41 @@ export default { node.expanded = true } } else if (data.id.startsWith('r_')) { + // 获取父节点信息 + const parentNodes = this.getParentNodes(data) + let selectedData = { + floorId: parentNodes.floorId, + unitId: parentNodes.unitId, + roomId: data.roomId, + roomName: data.roomName + }; + + // 保存到localStorage + localStorage.setItem('lastSelectedRoom', JSON.stringify(selectedData)); + this.lastSelected = selectedData; + this.$emit('selectRoom', { roomId: data.roomId, roomName: data.roomName }) } }, + // 新增:获取父节点信息 + getParentNodes(data) { + const treeRef = this.$refs.tree + if (!treeRef || !treeRef.store) return { floorId: '', unitId: '' } + + const node = treeRef.store.nodesMap[data.id] + if (!node || !node.parent) return { floorId: '', unitId: '' } + + const unitNode = node.parent + const floorNode = unitNode.parent + + return { + floorId: floorNode ? floorNode.data.floorId : '', + unitId: unitNode ? unitNode.data.unitId : '' + } + }, async loadRooms(unitData, node) { try { const { rooms } = await queryRoomsTree({ @@ -123,15 +249,24 @@ export default { // Update the node's children this.$set(node.data, 'children', roomNodes) - this.$emit('selectRoom', { - roomId: roomNodes[0].roomId, - roomName: roomNodes[0].roomName - }) + + // 只有在初始化时才自动选择第一个房屋 + if (!this.isInitialized) { + this.$emit('selectRoom', { + roomId: roomNodes[0].roomId, + roomName: roomNodes[0].roomName + }) + } } } catch (error) { console.error('Failed to load rooms:', error) this.$message.error(this.$t('roomTree.loadRoomError')) } + }, + // 新增:清除保存的选择记录 + clearLastSelected() { + localStorage.removeItem('lastSelectedRoom') + this.lastSelected = {} } } } diff --git a/src/views/fee/roomCreateFeeList.vue b/src/views/fee/roomCreateFeeList.vue index ae1372b..5982fc4 100644 --- a/src/views/fee/roomCreateFeeList.vue +++ b/src/views/fee/roomCreateFeeList.vue @@ -1,10 +1,18 @@