zhangjiaqing
8 天以前 1cef3adee31c6934c0da4b4f0b8a6f5ac03b364f
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/**
 入驻小区
 **/
/*
 * 修复说明:
 * 1. 修复页面空白问题:
 *    - 原因:_initMethod 和 listMeterTypes 函数使用了未定义的 $that 变量
 *    - 解决方案:将 $that 替换为 vc.component,并添加了组件存在性检查
 *
 * 2. 修复滑动条无法滚动到最后一列问题:
 *    - 原因:_computeTableDivWidth 方法没有考虑 roomTreeDiv 的宽度
 *    - 解决方案:修改 _computeTableDivWidth 方法,考虑 roomTreeDiv 的宽度
 *
 * 3. 修复操作列位置问题:
 *    - 原因:操作列位于表格的第一列,用户希望将其移到最后一列
 *    - 解决方案:将操作列从第一列移动到最后一列,并修改其样式为粘在右侧
 *
 * 4. 粘项实现说明:
 *    - 使用 CSS 的 position: sticky 属性实现粘性定位
 *    - 当表格水平滚动时,操作列会固定在表格的右侧,不会随滚动而消失
 *    - 实现代码:
 *      <th class="text-center" style="position: sticky; right: 0; background-color: #fff; z-index: 10;">
 *          <vc:i18n name="操作" namespace="meterWaterManage"></vc:i18n>
 *      </th>
 *      <td class="text-center" style="position: sticky; right: 0; background-color: #fff; z-index: 10;">
 *          <!-- 操作按钮 -->
 *      </td>
 */
(function (vc) {
    var DEFAULT_PAGE = vc.paginationConfig ? vc.paginationConfig.defaultPage : 1;
    var DEFAULT_ROWS = 10;
    vc.extends({
        data: {
            meterWaterManageInfo: {
                meterWaters: [],
                total: 0,
                records: 1,
                moreCondition: false,
                waterId: '',
                meterTypes: [],
                roomName:'',
                conditions: {
                    waterId: '',
                    meterType: '',
                    roomNum: '',
                    objId:''
                },
                timer: {}
            }
        },
        
        // 框架要求的字典初始化方法,必须定义在组件对象顶层
        _initDicts: function() {
            console.log('水电抄表组件初始化字典数据');
            // 这里可以根据需要初始化字典数据,目前组件不需要字典数据,所以留空
        },
        
        _initMethod: function () {
            // 修改:改回使用 initRoomTreeDiv 方法,与 roomTreeDiv 组件对应
            // 原因:确保房屋树能够正常初始化,支持房间选择功能
            vc.emit('roomTreeDiv', 'initRoomTreeDiv', {
                callName: 'meterWaterManage'
            });
            if (vc.component && typeof vc.component.listMeterTypes === 'function') {
                vc.component.listMeterTypes(); // 加载抄表类型
            }
            if (vc.component && typeof vc.component._listMeterWaters === 'function') {
                vc.component._listMeterWaters(DEFAULT_PAGE, DEFAULT_ROWS);
            }
        },
        // 组件挂载完成后立即加载数据
        mounted: function() {
            console.log('水电抄表组件已挂载,立即加载数据');
            
            // 显式初始化footable表格
            if (window.$ && typeof window.$.fn.footable === 'function') {
                console.log('显式初始化footable表格');
                $('.footable').footable();
            }
            
            // 加载表类型数据
            if (vc.component && typeof vc.component.listMeterTypes === 'function') {
                vc.component.listMeterTypes();
            }
            
            // 立即加载抄表数据,不依赖房间树
            if (vc.component && typeof vc.component._listMeterWaters === 'function') {
                vc.component._listMeterWaters(DEFAULT_PAGE, DEFAULT_ROWS);
            }
            
            // 添加页面可见性变化监听
            document.addEventListener('visibilitychange', () => {
                if (document.visibilityState === 'visible' && vc.component) {
                    console.log('页面变为可见,重新加载抄表数据');
                    
                    // 加载表类型数据
                    if (typeof vc.component.listMeterTypes === 'function') {
                        vc.component.listMeterTypes();
                    }
                    
                    // 加载抄表数据
                    if (typeof vc.component._listMeterWaters === 'function') {
                        vc.component._listMeterWaters(DEFAULT_PAGE, DEFAULT_ROWS);
                    }
                }
            });
            
            // 添加组件激活监听
            window.addEventListener('hashchange', () => {
                if (window.location.hash.includes('meterWaterManage') && vc.component) {
                    console.log('组件激活,重新加载抄表数据');
                    
                    // 加载表类型数据
                    if (typeof vc.component.listMeterTypes === 'function') {
                        vc.component.listMeterTypes();
                    }
                    
                    // 加载抄表数据
                    if (typeof vc.component._listMeterWaters === 'function') {
                        vc.component._listMeterWaters(DEFAULT_PAGE, DEFAULT_ROWS);
                    }
                }
            });
            
            // 添加全局刷新事件监听
            vc.on('globalRefresh', 'refreshData', function () {
                if (vc.component) {
                    console.log('收到全局刷新事件,重新加载抄表数据');
                    
                    // 加载表类型数据
                    if (typeof vc.component.listMeterTypes === 'function') {
                        vc.component.listMeterTypes();
                    }
                    
                    // 加载抄表数据
                    if (typeof vc.component._listMeterWaters === 'function') {
                        vc.component._listMeterWaters(DEFAULT_PAGE, DEFAULT_ROWS);
                    }
                }
            });
        },
        _initEvent: function () {
            // 房间选择事件
            vc.on('meterWaterManage', 'selectRoom', function (_param) {
                console.log('选择房间,加载对应抄表数据:', _param);
                if (!vc.component || !vc.component.meterWaterManageInfo) {
                    console.error('组件实例不存在');
                    return;
                }
                vc.component.meterWaterManageInfo.conditions.objId = _param.roomId;
                vc.component.meterWaterManageInfo.conditions.roomNum = '';
                vc.component.meterWaterManageInfo.roomName = _param.roomName;
                vc.component._listMeterWaters(DEFAULT_PAGE, DEFAULT_ROWS);
            });
            
            // 列表刷新事件
            vc.on('meterWaterManage', 'listMeterWater', function () {
                console.log('收到列表刷新事件,重新加载抄表数据');
                if (vc.component && typeof vc.component._listMeterWaters === 'function') {
                    vc.component._listMeterWaters(DEFAULT_PAGE, DEFAULT_ROWS);
                }
            });
            
            // 分页事件
            vc.on('pagination', 'page_event', function (_currentPage) {
                console.log('分页事件触发,页码:', _currentPage);
                if (vc.component && typeof vc.component._listMeterWaters === 'function') {
                    vc.component._listMeterWaters(_currentPage, DEFAULT_ROWS);
                }
            });
            
            // 房间通知事件
            vc.on('meterWaterManage', 'notifyRoom', function (_room) {
                console.log('收到房间通知事件:', _room);
                if (!vc.component || !vc.component.meterWaterManageInfo) {
                    console.error('组件实例不存在');
                    return;
                }
                vc.component.meterWaterManageInfo.conditions.roomNum = _room.floorNum + "-" + _room.unitNum + "-" + _room.roomNum;
                vc.component._listMeterWaters(DEFAULT_PAGE, DEFAULT_ROWS);
            });
            
            // 查询按钮点击事件
            document.addEventListener('click', (e) => {
                if (e.target.textContent === '查询' || e.target.textContent === '重置') {
                    console.log('查询/重置按钮点击,重新加载抄表数据');
                    if (vc.component && vc.component.meterWaterManageInfo && typeof vc.component._listMeterWaters === 'function') {
                        vc.component._listMeterWaters(DEFAULT_PAGE, DEFAULT_ROWS);
                    }
                }
            });
            
            // 监听社区切换事件
            vc.on('communitySwitch', 'communitySwitched', function () {
                console.log('社区切换,重新加载抄表数据');
                if (vc.component && vc.component.meterWaterManageInfo && typeof vc.component._listMeterWaters === 'function') {
                    vc.component._listMeterWaters(DEFAULT_PAGE, DEFAULT_ROWS);
                }
            });
        },
        methods: {
            _listMeterWaters: function (_page, _rows) {
                // 验证vc.component是否存在
                if (!vc.component || !vc.component.meterWaterManageInfo) {
                    console.error('组件实例不存在');
                    return;
                }
                
                console.log('===== 开始加载抄表数据 =====');
                console.log('发起抄表数据请求,页码:', _page, '行数:', _rows);
                
                // 获取社区信息
                let currentCommunity = vc.getCurrentCommunity();
                console.log('当前社区信息:', currentCommunity);
                
                if (!currentCommunity) {
                    console.error('当前社区信息为空');
                    // 尝试直接从全局获取社区信息
                    currentCommunity = window._currentCommunity;
                    console.log('尝试从全局获取社区信息:', currentCommunity);
                }
                
                if (!currentCommunity || !currentCommunity.communityId) {
                    console.error('社区信息不存在或缺少communityId,1秒后重试');
                    // 延迟重试
                    setTimeout(() => {
                        if (vc.component && vc.component._listMeterWaters) {
                            vc.component._listMeterWaters(_page, _rows);
                        }
                    }, 1000);
                    return;
                }
                
                // 输出当前条件值
                console.log('当前条件值:', vc.component.meterWaterManageInfo.conditions);
                
                // 构建请求参数
                let params = {
                    page: _page,
                    row: _rows,
                    communityId: currentCommunity.communityId,
                    waterId: vc.component.meterWaterManageInfo.conditions.waterId ? vc.component.meterWaterManageInfo.conditions.waterId.trim() : '',
                    meterType: vc.component.meterWaterManageInfo.conditions.meterType || '',
                    roomNum: vc.component.meterWaterManageInfo.conditions.roomNum ? vc.component.meterWaterManageInfo.conditions.roomNum.trim() : ''
                };
                
                // 只有当objId有值时才传递,否则不传递该参数
                // 当选择房间时,objId会被设置为房间ID,否则为空
                let objId = vc.component.meterWaterManageInfo.conditions.objId;
                console.log('当前objId:', objId);
                if (objId && objId.trim() !== '') {
                    params.objId = objId;
                    console.log('传递objId参数:', objId);
                } else {
                    console.log('不传递objId参数');
                }
                
                let param = {
                    params: params
                };
                
                console.log('请求参数:', JSON.stringify(param));
                
                // 发送请求
                vc.http.apiGet('/meterWater.listMeterWaters',
                    param,
                    function (jsonStr, res) {
                        console.log('收到API响应字符串:', jsonStr);
                        
                        // 验证vc.component是否存在
                        if (!vc.component || !vc.component.meterWaterManageInfo) {
                            console.error('组件实例不存在');
                            return;
                        }
                        
                        try {
                            // 检查jsonStr是否已经是对象
                            let _json;
                            if (typeof jsonStr === 'string') {
                                // 如果是字符串,解析为JSON对象
                                _json = JSON.parse(jsonStr);
                                console.log('解析后的API响应:', _json);
                            } else {
                                // 如果已经是对象,直接使用
                                _json = jsonStr;
                                console.log('API响应已为对象:', _json);
                            }
                            
                            if (_json && _json.code == 0) {
                                // 确保 data 是数组
                                let meterWatersData = _json.data || [];
                                if (!Array.isArray(meterWatersData)) {
                                    console.error('API返回的data不是数组,转换为空数组');
                                    meterWatersData = [];
                                }
                                
                                console.log('准备显示的抄表数据:', meterWatersData);
                                console.log('抄表数据数量:', meterWatersData.length);
                                
                                // 处理API返回的total和records为0但实际有数据的情况
                                vc.component.meterWaterManageInfo.total = _json.total > 0 ? _json.total : meterWatersData.length;
                                vc.component.meterWaterManageInfo.records = _json.records > 0 ? _json.records : meterWatersData.length;
                                
                                // 强制Vue更新 - 解决响应式问题
                                vc.component.meterWaterManageInfo.meterWaters = [];
                                
                                setTimeout(() => {
                                    // 深拷贝数据,确保响应式
                                    var deepCopiedData = JSON.parse(JSON.stringify(meterWatersData));
                                    vc.component.meterWaterManageInfo.meterWaters = deepCopiedData;
                                    
                                    console.log('数据更新后,meterWaters数量:', vc.component.meterWaterManageInfo.meterWaters.length);
                                    console.log('数据更新后,total:', vc.component.meterWaterManageInfo.total);
                                    console.log('数据更新后,records:', vc.component.meterWaterManageInfo.records);
                                    
                                    // 计算总页数
                                    var totalPages = Math.ceil(vc.component.meterWaterManageInfo.total / _rows) || 1;
                                    
                                    // 初始化分页
                                    vc.emit('pagination', 'init', {
                                        total: totalPages,
                                        dataCount: vc.component.meterWaterManageInfo.total,
                                        currentPage: _page
                                    });
                                    
                                    // 强制DOM更新,确保数据显示
                                    // 通过重新赋值数组引用,触发Vue的响应式更新
                                    vc.component.meterWaterManageInfo.meterWaters = [...vc.component.meterWaterManageInfo.meterWaters];
                                    console.log('强制DOM更新后,meterWaters数量:', vc.component.meterWaterManageInfo.meterWaters.length);
                                    
                                    // 手动刷新footable表格,确保数据正确显示
                                    setTimeout(() => {
                                        if (window.$ && typeof window.$.fn.footable === 'function') {
                                            console.log('手动刷新footable表格');
                                            $('.footable').footable('refresh');
                                        }
                                    }, 100);
                                }, 0);
                            } else {
                                console.error('API请求失败:', _json.msg || '未知错误');
                                vc.component.meterWaterManageInfo.meterWaters = [];
                                vc.component.meterWaterManageInfo.total = 0;
                                vc.component.meterWaterManageInfo.records = 0;
                            }
                        } catch (e) {
                            console.error('处理API响应失败:', e, '原始数据:', jsonStr);
                            vc.component.meterWaterManageInfo.meterWaters = [];
                            vc.component.meterWaterManageInfo.total = 0;
                            vc.component.meterWaterManageInfo.records = 0;
                        }
                        
                        console.log('===== 抄表数据加载完成 =====');
                    },
                    function (errInfo) {
                        console.error('API请求异常:', errInfo);
                        
                        // 验证vc.component是否存在
                        if (!vc.component || !vc.component.meterWaterManageInfo) {
                            console.error('组件实例不存在');
                            return;
                        }
                        
                        vc.component.meterWaterManageInfo.meterWaters = [];
                        vc.component.meterWaterManageInfo.total = 0;
                        vc.component.meterWaterManageInfo.records = 0;
                        
                        console.log('===== 抄表数据加载异常 =====');
                    }
                );
            },
            _openAddMeterWaterModal: function () {
                // 验证vc.component是否存在
                if (!vc.component || !vc.component.meterWaterManageInfo) {
                    console.error('组件实例不存在');
                    return;
                }
                vc.emit('addMeterWater', 'openAddMeterWaterModal', {
                    roomId: vc.component.meterWaterManageInfo.conditions.objId,
                    roomName: vc.component.meterWaterManageInfo.roomName,
                    ownerName:''
                });
            },
            _openEditMeterWaterModel: function (_meterWater) {
                vc.emit('editMeterWater', 'openEditMeterWaterModal', _meterWater);
            },
            _openDeleteMeterWaterModel: function (_meterWater) {
                vc.emit('deleteMeterWater', 'openDeleteMeterWaterModal', _meterWater);
            },
            //查询
            _queryMeterWaterMethod: function () {
                // 验证vc.component是否存在
                if (!vc.component || !vc.component.meterWaterManageInfo) {
                    console.error('组件实例不存在');
                    return;
                }
                vc.component._listMeterWaters(DEFAULT_PAGE, DEFAULT_ROWS);
            },
            //重置
            _resetMeterWaterMethod: function () {
                // 验证vc.component是否存在
                if (!vc.component || !vc.component.meterWaterManageInfo) {
                    console.error('组件实例不存在');
                    return;
                }
                vc.component.meterWaterManageInfo.conditions.roomNum = "";
                vc.component.meterWaterManageInfo.conditions.meterType = "";
                vc.component.meterWaterManageInfo.conditions.waterId = "";
                vc.component._listMeterWaters(DEFAULT_PAGE, DEFAULT_ROWS);
            },
            _moreCondition: function () {
                // 验证vc.component是否存在
                if (!vc.component || !vc.component.meterWaterManageInfo) {
                    console.error('组件实例不存在');
                    return;
                }
                if (vc.component.meterWaterManageInfo.moreCondition) {
                    vc.component.meterWaterManageInfo.moreCondition = false;
                } else {
                    vc.component.meterWaterManageInfo.moreCondition = true;
                }
            },
            _openMeterWaterImport: function (arg1, arg2) {
                vc.emit('importMeterWaterFee', 'openImportMeterWaterFeeModal', {});
            },
            _openMeterQrCode:function(){
                // 验证vc.component是否存在
                if (!vc.component || !vc.component.meterWaterManageInfo) {
                    console.error('组件实例不存在');
                    return;
                }
                vc.emit('roomMeterQrcode', 'openRoomMeterQrcodeModal', {
                    roomId: vc.component.meterWaterManageInfo.conditions.objId,
                    roomName: vc.component.meterWaterManageInfo.roomName,
                    ownerName:''
                });
            },
            
            _getMeteTypeName: function (_meterType) {
                if (_meterType == '1010') {
                    return "电表";
                } else if (_meterType == '2020') {
                    return "水表";
                }
                return "煤气费";
            },
            
            listMeterTypes: function () {
                // 验证vc.component是否存在
                if (!vc.component || !vc.component.meterWaterManageInfo) {
                    console.error('组件实例不存在');
                    return;
                }
                
                // 验证社区信息是否存在
                let currentCommunity = vc.getCurrentCommunity();
                if (!currentCommunity || !currentCommunity.communityId) {
                    console.error('社区信息不存在');
                    return;
                }
                
                let param = {
                    params: {
                        page: 1,
                        row: 100,
                        communityId: currentCommunity.communityId
                    }
                };
                //发送get请求
                vc.http.apiGet('/meterType.listMeterType',
                    param,
                    function (json, res) {
                        // 验证vc.component是否存在
                        if (!vc.component || !vc.component.meterWaterManageInfo) {
                            console.error('组件实例不存在');
                            return;
                        }
                        
                        try {
                            let _json = JSON.parse(json);
                            // 确保 meterTypes 是一个数组
                            vc.component.meterWaterManageInfo.meterTypes = _json.data && Array.isArray(_json.data) ? _json.data : [];
                        } catch (e) {
                            console.error('处理API响应失败:', e);
                            if (vc.component && vc.component.meterWaterManageInfo) {
                                vc.component.meterWaterManageInfo.meterTypes = [];
                            }
                        }
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                        // 验证vc.component是否存在
                        if (vc.component && vc.component.meterWaterManageInfo) {
                            vc.component.meterWaterManageInfo.meterTypes = [];
                        }
                    }
                );
            },
               
            _meterInputRoom: function () {
                // 验证vc.component是否存在
                if (!vc.component || !vc.component.meterWaterManageInfo) {
                    console.error('组件实例不存在');
                    return;
                }
                if (vc.component.meterWaterManageInfo.timer) {
                    clearTimeout(vc.component.meterWaterManageInfo.timer)
                }
                vc.component.meterWaterManageInfo.timer = setTimeout(() => {
                    vc.emit('inputSearchRoomInfo', 'searchRoom', {
                        callComponent: 'meterWaterManage',
                        roomName: vc.component.meterWaterManageInfo.conditions.roomNum
                    });
                }, 1500)
            },
            _openMeterType:function(){
                vc.jumpToPage('/#/pages/property/meterTypeManage?tab=抄表类型');
            },
            _computeTableDivWidth: function () {
                let mainWidth = document.getElementsByTagName('body')[0].clientWidth - (document.getElementById('menu-nav') ? document.getElementById('menu-nav').offsetWidth : 200);
                // 保持使用 .room-floor-unit-tree 选择器,确保表格宽度计算正确
                let treeColumn = document.querySelector('.room-floor-unit-tree');
                if (treeColumn) {
                    mainWidth -= treeColumn.offsetWidth;
                }
                mainWidth = mainWidth - 20 - 15 - 20;
                return mainWidth + 'px';
            }
        }
    });
})(window.vc);