Blame view

src/components/room/roomTreeDiv.vue 8.69 KB
cd8d442f   wuxw   开始处理水电抄表功能
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  <template>
    <div class="room-tree-container">
      <el-tree ref="tree" :data="treeData" node-key="id" :props="defaultProps" :highlight-current="true"
        :expand-on-click-node="false" @node-click="handleNodeClick">
        <span slot-scope="{ node, data }" class="custom-tree-node">
          <span>
            <i :class="data.icon" style="margin-right: 5px"></i>
            {{ node.label }}
          </span>
        </span>
      </el-tree>
    </div>
  </template>
  
  <script>
  import { queryUnits, queryRoomsTree } from '@/api/fee/meterWaterManageApi'
  import { getCommunityId } from '@/api/community/communityApi'
  
  export default {
    name: 'RoomTreeDiv',
    data() {
      return {
        treeData: [],
        defaultProps: {
          children: 'children',
          label: 'text'
        },
5480f93a   wuxw   支持 房屋收费页面点击缴费后返回还...
28
29
30
        communityId: '',
        lastSelected: {},
        isInitialized: false // 添加初始化标志
cd8d442f   wuxw   开始处理水电抄表功能
31
32
33
34
35
36
37
38
      }
    },
    created() {
      this.communityId = getCommunityId()
      this.loadTreeData()
    },
    methods: {
      async loadTreeData() {
5480f93a   wuxw   支持 房屋收费页面点击缴费后返回还...
39
40
41
42
43
44
45
46
47
48
49
        // 加载上次选择的房屋信息
        let lastSelected = localStorage.getItem('lastSelectedRoom');
        if (lastSelected) {
          try {
            this.lastSelected = JSON.parse(lastSelected);
          } catch (error) {
            console.error('解析lastSelected失败:', error);
            localStorage.removeItem('lastSelectedRoom');
          }
        }
        
cd8d442f   wuxw   开始处理水电抄表功能
50
51
52
53
54
55
56
        try {
          const units = await queryUnits({
            communityId: this.communityId
          })
          this.buildTreeData(units)
        } catch (error) {
          console.error('Failed to load tree data:', error)
9d8dc2e6   wuxw   开发完成水电抄表
57
          this.$message.error(this.$t('roomTree.loadError'))
cd8d442f   wuxw   开始处理水电抄表功能
58
59
60
        }
      },
      buildTreeData(units) {
9d8dc2e6   wuxw   开发完成水电抄表
61
        const floorMap = {}
cd8d442f   wuxw   开始处理水电抄表功能
62
  
9d8dc2e6   wuxw   开发完成水电抄表
63
        // Build floor nodes and unit nodes
cd8d442f   wuxw   开始处理水电抄表功能
64
        units.forEach(unit => {
9d8dc2e6   wuxw   开发完成水电抄表
65
66
          if (!floorMap[unit.floorId]) {
            floorMap[unit.floorId] = {
cd8d442f   wuxw   开始处理水电抄表功能
67
68
69
              id: `f_${unit.floorId}`,
              floorId: unit.floorId,
              floorNum: unit.floorNum,
9d8dc2e6   wuxw   开发完成水电抄表
70
71
              icon: "/img/floor.png",
              text: `${unit.floorNum}${this.$t('room.floorUnitTree.building')}`,
cd8d442f   wuxw   开始处理水电抄表功能
72
              children: []
9d8dc2e6   wuxw   开发完成水电抄表
73
            }
cd8d442f   wuxw   开始处理水电抄表功能
74
          }
cd8d442f   wuxw   开始处理水电抄表功能
75
  
9d8dc2e6   wuxw   开发完成水电抄表
76
77
78
79
80
81
82
83
84
          floorMap[unit.floorId].children.push({
            id: `u_${unit.unitId}`,
            unitId: unit.unitId,
            unitNum: unit.unitNum,
            floorId: unit.floorId, // Add floorId reference
            icon: "/img/unit.png",
            text: `${unit.unitNum}${this.$t('room.floorUnitTree.unit')}`,
            children: []
          })
cd8d442f   wuxw   开始处理水电抄表功能
85
86
        })
  
9d8dc2e6   wuxw   开发完成水电抄表
87
        this.treeData = Object.values(floorMap)
24d3590f   wuxw   房屋收费页面开发完成
88
        this.$nextTick(() => {
5480f93a   wuxw   支持 房屋收费页面点击缴费后返回还...
89
90
91
92
93
94
          // 如果有上次选择的房屋,则展开并选择;否则选择第一个房屋
          if (this.lastSelected && this.lastSelected.floorId && this.lastSelected.unitId) {
            this.expandAndSelectLastRoom()
          } else {
            this.expandAndSelectFirstRoom()
          }
24d3590f   wuxw   房屋收费页面开发完成
95
96
        })
      },
5480f93a   wuxw   支持 房屋收费页面点击缴费后返回还...
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
      // 新增:展开并选择上次选择的房屋
      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)
        }
      },
24d3590f   wuxw   房屋收费页面开发完成
174
175
      async expandAndSelectFirstRoom() {
        if (!this.treeData.length) return
5480f93a   wuxw   支持 房屋收费页面点击缴费后返回还...
176
        
24d3590f   wuxw   房屋收费页面开发完成
177
178
179
180
181
182
183
184
185
        const firstFloor = this.treeData[0]
        if (!firstFloor.children || !firstFloor.children.length) return
        const firstUnit = firstFloor.children[0]
        const treeRef = this.$refs.tree
        if (treeRef && treeRef.store) {
          treeRef.store.nodesMap[firstFloor.id] && (treeRef.store.nodesMap[firstFloor.id].expanded = true)
          treeRef.store.nodesMap[firstUnit.id] && (treeRef.store.nodesMap[firstUnit.id].expanded = true)
        }
        await this.loadRooms(firstUnit, { data: firstUnit })
5480f93a   wuxw   支持 房屋收费页面点击缴费后返回还...
186
        this.isInitialized = true
cd8d442f   wuxw   开始处理水电抄表功能
187
      },
9d8dc2e6   wuxw   开发完成水电抄表
188
      async handleNodeClick(data, node) {
cd8d442f   wuxw   开始处理水电抄表功能
189
        if (data.id.startsWith('u_')) {
9d8dc2e6   wuxw   开发完成水电抄表
190
191
192
193
          if (!node.expanded) {
            await this.loadRooms(data, node)
            node.expanded = true
          }
cd8d442f   wuxw   开始处理水电抄表功能
194
        } else if (data.id.startsWith('r_')) {
5480f93a   wuxw   支持 房屋收费页面点击缴费后返回还...
195
196
197
198
199
200
201
202
203
204
205
206
207
          // 获取父节点信息
          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;
  
cd8d442f   wuxw   开始处理水电抄表功能
208
209
210
211
212
213
          this.$emit('selectRoom', {
            roomId: data.roomId,
            roomName: data.roomName
          })
        }
      },
5480f93a   wuxw   支持 房屋收费页面点击缴费后返回还...
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
      // 新增:获取父节点信息
      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 : ''
        }
      },
9d8dc2e6   wuxw   开发完成水电抄表
230
      async loadRooms(unitData, node) {
cd8d442f   wuxw   开始处理水电抄表功能
231
        try {
9d8dc2e6   wuxw   开发完成水电抄表
232
233
          const { rooms } = await queryRoomsTree({
            unitId: unitData.unitId,
cd8d442f   wuxw   开始处理水电抄表功能
234
235
236
237
238
            communityId: this.communityId,
            page: 1,
            row: 1000
          })
  
9d8dc2e6   wuxw   开发完成水电抄表
239
240
241
242
243
244
245
          if (rooms && rooms.length > 0) {
            const roomNodes = rooms.map(room => ({
              id: `r_${room.roomId}`,
              roomId: room.roomId,
              roomName: `${room.floorNum}-${room.unitNum}-${room.roomNum}`,
              icon: "/img/room.png",
              text: room.ownerName
5f798b88   wuxw   费用功能继续完善
246
                ? `${room.roomNum}(${room.ownerName})`
9d8dc2e6   wuxw   开发完成水电抄表
247
248
                : `${room.roomNum}`
            }))
cd8d442f   wuxw   开始处理水电抄表功能
249
  
9d8dc2e6   wuxw   开发完成水电抄表
250
251
            // Update the node's children
            this.$set(node.data, 'children', roomNodes)
5480f93a   wuxw   支持 房屋收费页面点击缴费后返回还...
252
253
254
255
256
257
258
259
            
            // 只有在初始化时才自动选择第一个房屋
            if (!this.isInitialized) {
              this.$emit('selectRoom', {
                roomId: roomNodes[0].roomId,
                roomName: roomNodes[0].roomName
              })
            }
cd8d442f   wuxw   开始处理水电抄表功能
260
261
262
          }
        } catch (error) {
          console.error('Failed to load rooms:', error)
9d8dc2e6   wuxw   开发完成水电抄表
263
          this.$message.error(this.$t('roomTree.loadRoomError'))
cd8d442f   wuxw   开始处理水电抄表功能
264
        }
5480f93a   wuxw   支持 房屋收费页面点击缴费后返回还...
265
266
267
268
269
      },
      // 新增:清除保存的选择记录
      clearLastSelected() {
        localStorage.removeItem('lastSelectedRoom')
        this.lastSelected = {}
cd8d442f   wuxw   开始处理水电抄表功能
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
      }
    }
  }
  </script>
  
  <style lang="scss" scoped>
  .room-tree-container {
    height: 100%;
    overflow-y: auto;
    padding: 10px;
  
    .custom-tree-node {
      flex: 1;
      display: flex;
      align-items: center;
      font-size: 14px;
      padding: 5px 0;
    }
  }
  </style>