zhangjq
2026-01-26 da84a8d3418433c7de77068c933349c874ec70bc
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/**
 * 房间树组件
 * 用于展示房屋的楼层、单元和房间结构
 * @param {Object} vc - Vue 组件实例
 */
(function (vc) {
    /**
     * 默认页码
     * @type {number}
     */
    let DEFAULT_PAGE = 1;
    /**
     * 默认每页行数
     * @type {number}
     */
    let DEFAULT_ROW = 10;
    /**
     * 扩展 Vue 组件
     */
    vc.extends({
        /**
         * 组件数据
         */
        data: {
            /**
             * 房间树组件信息
             * @type {Object}
             */
            roomTreeDivInfo: {
                /**
                 * 单元列表
                 * @type {Array}
                 */
                units: [],
                /**
                 * 调用名称
                 * @type {string}
                 */
                callName: ''
            }
        },
        /**
         * 初始化方法
         */
        _initMethod: function () {
        },
        /**
         * 初始化事件
         */
        _initEvent: function () {
            /**
             * 监听初始化房间树事件
             * @param {Object} _param - 初始化参数
             */
            vc.on('roomTreeDiv', 'initRoomTreeDiv', function (_param) {
                // 设置调用名称
                $that.roomTreeDivInfo.callName = _param.callName;
                // 加载楼层和单元
                $that._loadRoomTreeDivFloorAndUnits();
            });
        },
        /**
         * 组件方法
         */
        methods: {
            /**
             * 加载楼层和单元数据
             */
            _loadRoomTreeDivFloorAndUnits: function () {
                /**
                 * 请求参数
                 * @type {Object}
                 */
                let param = {
                    params: {
                        // 获取当前社区ID
                        communityId: vc.getCurrentCommunity().communityId
                    }
                };
                
                // 验证社区ID是否存在
                let communityId = vc.getCurrentCommunity().communityId;
                console.log('当前社区ID:', communityId);
                if (!communityId) {
                    console.error('未获取到有效的社区ID');
                    $that.roomTreeDivInfo.units = [];
                    $that._initJsTreeRoomTreeDivFloorUnit();
                    return;
                }
                
                // 发送 GET 请求获取单元数据
                vc.http.apiGet('/unit.queryUnits',
                    param,
                    /**
                     * 请求成功回调
                     * @param {string} json - 返回的 JSON 字符串
                     */
                    function (json) {
                        console.log('单元API返回原始数据:', json);
                        let unitsData = [];
                        
                        if (json && json.trim() !== '') {
                            try {
                                // 解析 JSON 数据
                                let _unitInfo = JSON.parse(json);
                                console.log('解析后的单元数据:', _unitInfo);
                                
                                // 增强数据处理,支持多种响应格式
                                if (_unitInfo && _unitInfo.code == 0) {
                                    // 标准组件API返回格式
                                    if (_unitInfo.data && Array.isArray(_unitInfo.data)) {
                                        unitsData = _unitInfo.data;
                                        console.log('使用标准格式.data字段数组,共', unitsData.length, '条单元数据');
                                    } else if (_unitInfo.units && Array.isArray(_unitInfo.units)) {
                                        unitsData = _unitInfo.units;
                                        console.log('使用标准格式.units字段数组,共', unitsData.length, '条单元数据');
                                    }
                                } else if (_unitInfo && Array.isArray(_unitInfo)) {
                                    // 直接数组格式
                                    unitsData = _unitInfo;
                                    console.log('使用直接数组格式,共', unitsData.length, '条单元数据');
                                } else if (_unitInfo && typeof _unitInfo === 'object') {
                                    // 单对象格式
                                    unitsData = [_unitInfo];
                                    console.log('使用单对象格式,共 1 条单元数据');
                                } else {
                                    console.warn('单元数据格式不符合预期,无法提取数组:', _unitInfo);
                                }
                            } catch (e) {
                                console.error('解析单元数据时发生错误:', e);
                                console.error('原始响应数据:', json);
                            }
                        } else {
                            console.warn('单元API返回空数据');
                        }
                        
                        console.log('最终处理后的单元数据:', unitsData);
                        $that.roomTreeDivInfo.units = unitsData;
                        
                        // 初始化树结构
                        $that._initJsTreeRoomTreeDivFloorUnit();
                    },
                    /**
                     * 请求失败回调
                     */
                    function (errInfo, error) {
                        console.error('获取单元数据失败:', errInfo, error);
                        // 重置 units 为空数组
                        $that.roomTreeDivInfo.units = [];
                        // 初始化树结构
                        $that._initJsTreeRoomTreeDivFloorUnit();
                    });
            },
            /**
             * 初始化 JSTree 树结构
             */
            _initJsTreeRoomTreeDivFloorUnit: function () {
                // 构建树数据
                let _data = $that._doJsTreeRoomTreeDivData();
                // 销毁旧的树实例
                $.jstree.destroy()
                // 初始化新的树实例
                $("#jstree_floorUnitRoomDiv").jstree({
                    "checkbox": {
                        "keep_selected_style": false
                    },
                    'state': { // 一些初始化状态
                        "opened": true,
                    },
                    'core': {
                        "check_callback": true,
                        'data': _data
                    }
                });
                /**
                 * 树初始化完成事件
                 * @param {Event} e - 事件对象
                 * @param {Object} data - 树数据对象
                 */
                $("#jstree_floorUnitRoomDiv").on("ready.jstree", function (e, data) {
                    // 获取调用名称
                    let _callName = $that.roomTreeDivInfo.callName;
                    // 如果是欠费可调用,直接返回
                    if (_callName == 'oweFeeCallable') {
                        return;
                    }
                    // 默认选中第一个单元,添加安全检查避免出错
                    if (_data && _data.length > 0 && _data[0].children && _data[0].children.length > 0) {
                        $('#jstree_floorUnitRoomDiv').jstree('select_node', _data[0].children[0].id /* , true */);
                    } else {
                        console.log('没有可用的单元数据,无法默认选中');
                    }
                });
                /**
                 * 监听树节点变化事件
                 * @param {Event} e - 事件对象
                 * @param {Object} data - 节点数据对象
                 */
                $('#jstree_floorUnitRoomDiv').on("changed.jstree", function (e, data) {
                    // 如果是模型初始化或就绪事件,直接返回
                    if (data.action == 'model' || data.action == 'ready') {
                        // 默认合并
                        //$("#jstree_floorUnit").jstree("close_all");
                        return;
                    }
                    
                    // 获取选中的节点ID
                    let _selected = data.selected[0];
                    
                    // 如果是楼层节点,直接返回
                    if (_selected.startsWith('f_')) {
                        return;
                    }
                    
                    // 打印节点结构,用于调试
                    console.log('Node structure:', data.node);
                    
                    /**
                     * 处理单元节点点击事件
                     * 当点击单元节点时,加载该单元下的房间列表
                     */
                    if (_selected.startsWith('u_')) {
                        // 从节点ID中提取unitId
                        // 节点ID格式:u_{unitId},例如:u_123
                        let unitId = _selected.replace('u_', '');
                        console.log('Unit ID:', unitId);
                        
                        // 如果提取到unitId,调用加载房间列表方法
                        if (unitId) {
                            $that._roomTreeDivLoadRoom(unitId, data);
                        }
                    }
                    
                    /**
                     * 处理房间节点点击事件
                     * 当点击房间节点时,触发selectRoom事件,传递房间信息给父组件
                     */
                    if (_selected.startsWith('r_')) {
                        // 从节点ID中提取roomId
                        // 节点ID格式:r_{roomId},例如:r_456
                        let roomId = _selected.replace('r_', '');
                        
                        // 从节点text属性获取房间名称
                        // 房间名称格式:房间号(业主姓名),例如:101(张三)
                        let roomName = data.node.text;
                        
                        // 打印房间信息,用于调试
                        console.log('Room info:', {roomName, roomId});
                        
                        // 如果提取到roomId,触发selectRoom事件
                        if (roomId) {
                            /**
                             * 触发selectRoom事件,传递房间信息给父组件
                             * 父组件(meterWaterManage)会:
                             * 1. 设置 conditions.objId = roomId
                             * 2. 当objId存在时,抄表按钮(v-if="meterWaterManageInfo.conditions.objId")会显示
                             */
                            vc.emit($that.roomTreeDivInfo.callName, 'selectRoom', {
                                roomName: roomName,  // 房间名称
                                roomId: roomId      // 房间ID
                            });
                        }
                    }
                });
                /**
                 * 监听树节点点击事件
                 * @param {Event} e - 事件对象
                 */
                $('#jstree_floorUnitRoomDiv')
                    .on('click', '.jstree-anchor', function (e) {
                        // 切换节点展开/折叠状态
                        $(this).jstree(true).toggle_node(e.target);
                    })
            },
            /**
             * 加载单元下的房间数据
             * @param {string} _unitId - 单元ID
             * @param {Object} data - 树节点数据
             */
            _roomTreeDivLoadRoom: function (_unitId, data) {
                // 获取选中的节点
                let node = data.instance.get_node(data.selected[0]);
                // 遍历选中节点的子节点
                let childNodes = node.children;
                // 如果已有子节点,直接展开
                if (childNodes && childNodes.length > 0) {
                    $('#jstree_floorUnitRoomDiv').jstree('open_node', '#u_' + _unitId);
                    return;
                }
                /**
                 * 请求参数
                 * @type {Object}
                 */
                let param = {
                    params: {
                        page: 1,
                        row: 1000,
                        unitId: _unitId,
                        communityId: vc.getCurrentCommunity().communityId
                    }
                };
                /**
                 * 房间数据数组
                 * @type {Array}
                 */
                let _datas = [];
                // 发送 GET 请求获取房间数据
                vc.http.apiGet('/room.queryRoomsTree',
                    param,
                    /**
                     * 请求成功回调
                     * @param {string} json - 返回的 JSON 字符串
                     * @param {Object} res - 响应对象
                     */
                    function (json, res) {
                        if (json) {
                            // 解析 JSON 数据
                            let listRoomData = JSON.parse(json);
                            // 如果有房间数据
                            if (listRoomData && listRoomData.rooms && Array.isArray(listRoomData.rooms)) {
                                // 遍历房间数据
                                listRoomData.rooms.forEach(_room => {
                                    if (!_room) return;
                                    // 构建房间文本
                                    let _text = _room.roomNum;
                                    // 如果有业主姓名,添加到文本中
                                    if (_room.ownerName) {
                                        _text += ('(' + _room.ownerName + ")")
                                    }
                                    // 构建房间节点数据
                                    let _data = {
                                        id: 'r_' + _room.roomId,
                                        text: _text,
                                        icon: "/img/room.png",
                                        data: {
                                            roomId: _room.roomId,
                                            roomName: _room.floorNum + "-" + _room.unitNum + "-" + _room.roomNum
                                        }
                                    };
                                    // 添加到房间数据数组
                                    _datas.push(_data);
                                });
                                // 使用 create_node 方法逐个添加节点,避免 _append_json_data 的问题
                                if (_datas && Array.isArray(_datas)) {
                                    _datas.forEach(function(_data) {
                                        $('#jstree_floorUnitRoomDiv').jstree('create_node', 'u_' + _unitId, _data, "last", false, false);
                                    });
                                }
                                // 延迟展开节点
                                setTimeout(function () {
                                    $('#jstree_floorUnitRoomDiv').jstree('open_node', '#u_' + _unitId);
                                }, 500);
                                // 如果有房间数据,默认选中第一个房间
                                if (listRoomData.rooms.length > 0) {
                                    let _firstRoom = listRoomData.rooms[0];
                                    vc.emit($that.roomTreeDivInfo.callName, 'selectRoom', {
                                        roomName: _firstRoom.floorNum + "-" + _firstRoom.unitNum + "-" + _firstRoom.roomNum,
                                        roomId: _firstRoom.roomId
                                    });
                                } else {
                                    // 如果没有房间数据,发送空房间信息
                                    vc.emit($that.roomTreeDivInfo.callName, 'selectRoom', {
                                        roomName: '',
                                        roomId: ''
                                    });
                                }
                            }
                        }
                    },
                    /**
                     * 请求失败回调
                     * @param {string} errInfo - 错误信息
                     * @param {Object} error - 错误对象
                     */
                    function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            /**
             * 构建树数据
             * @returns {Array} 树数据数组
             */
            _doJsTreeRoomTreeDivData: function () {
                /**
                 * 楼层树数据
                 * @type {Array}
                 */
                let _mFloorTree = [];
                /**
                 * 单元列表
                 * @type {Array}
                 */
                let _units = $that.roomTreeDivInfo.units || [];
                // 构建第一层菜单组
                _units.forEach(pItem => {
                    if (!pItem) return;
                    /**
                     * 是否包含楼层
                     * @type {boolean}
                     */
                    let _includeFloor = false;
                    // 检查是否已包含该楼层
                    for (let _mgIndex = 0; _mgIndex < _mFloorTree.length; _mgIndex++) {
                        if (pItem.floorId == _mFloorTree[_mgIndex].floorId) {
                            _includeFloor = true;
                        }
                    }
                    // 如果不包含该楼层,添加楼层节点
                    if (!_includeFloor) {
                        /**
                         * 楼层节点数据
                         * @type {Object}
                         */
                        let _floorItem = {
                            id: 'f_' + pItem.floorId,
                            text: pItem.floorNum,
                            icon: "/img/floor.png",
                            children: [],
                            state: {
                                opened: false
                            },
                            data: {
                                floorId: pItem.floorId,
                                floorNum: pItem.floorNum
                            }
                        };
                        // 构建楼层下的单元数据
                        $that._doJsTreeRoomTreeDivMenuData(_floorItem);
                        // 添加楼层节点到树数据
                        _mFloorTree.push(_floorItem);
                    }
                });
                // 返回树数据
                return _mFloorTree;
            },
            /**
             * 构建楼层下的单元数据
             * @param {Object} _floorItem - 楼层节点数据
             */
            _doJsTreeRoomTreeDivMenuData: function (_floorItem) {
                /**
                 * 单元列表
                 * @type {Array}
                 */
                let _units = $that.roomTreeDivInfo.units || [];
                /**
                 * 子节点列表
                 * @type {Array}
                 */
                let _children = _floorItem.children || [];
                // 构建菜单
                for (let _pIndex = 0; _pIndex < _units.length; _pIndex++) {
                    /**
                     * 当前单元
                     * @type {Object}
                     */
                    let _unit = _units[_pIndex];
                    if (!_unit) continue;
                    // 如果单元属于当前楼层
                    if (_floorItem.data && _unit.floorId == _floorItem.data.floorId) {
                        /**
                         * 是否包含单元
                         * @type {boolean}
                         */
                        let _includeMenu = false;
                        // 检查是否已包含该单元
                        for (let _mgIndex = 0; _mgIndex < _children.length; _mgIndex++) {
                            if (_children[_mgIndex].data && _unit.unitId == _children[_mgIndex].data.unitId) {
                                _includeMenu = true;
                            }
                        }
                        // 如果不包含该单元,添加单元节点
                        if (!_includeMenu) {
                            /**
                             * 单元节点数据
                             * @type {Object}
                             */
                            let _menuItem = {
                                id: 'u_' + _unit.unitId,
                                text: _unit.unitNum,
                                icon: "/img/unit.png",
                                children: [],
                                state: {
                                    opened: true
                                },
                                data: {
                                    unitId: _unit.unitId,
                                    unitNum: _unit.unitNum
                                }
                            };
                            // 添加单元节点到子节点列表
                            _children.push(_menuItem);
                        }
                    }
                }
            },
        }
    });
})(window.vc);