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
/**
 入驻小区
 **/
(function (vc) {
    var DEFAULT_PAGE = vc.paginationConfig ? vc.paginationConfig.defaultPage : 1;
    var DEFAULT_ROWS = 10;
    vc.extends({
        data: {
            assetImportLogInfo: {
                logs: [],
                total: 0,
                records: 1,
                moreCondition: false,
                carNum: ''
            }
        },
        _initMethod: function () {
            console.log('导入日志列表页面初始化,开始加载数据');
            // 直接调用数据加载方法,不等待mounted
            vc.component._listAssetImportLogs(DEFAULT_PAGE, DEFAULT_ROWS);
        },
        _initEvent: function () {
            // 监听全局的assetImportLog事件
            vc.on('assetImportLog', 'listAssetImportLog', function (_param) {
                vc.component._listAssetImportLogs(DEFAULT_PAGE, DEFAULT_ROWS);
            });
            
            // 监听分页事件
            vc.on('pagination', 'page_event', function (_currentPage) {
                vc.component._listAssetImportLogs(_currentPage, DEFAULT_ROWS);
            });
        },
        methods: {
            _listAssetImportLogs: function (_page, _rows) {
                console.log('调用_listAssetImportLogs方法,页码:', _page, '行数:', _rows);
                
                try {
                    // 构建请求参数
                    var param = {
                        params: {
                            page: _page,
                            row: _rows
                        }
                    };
                    
                    // 合同管理员账户查询导入日志时不应该传入communityId
                    // 检查用户账号是否为合同管理员账号
                    var contractAdminAccounts = ['18916013413'];
                    var userAccount = '';
                    
                    // 尝试从多个来源获取用户账号
                    // 1. 从vc.getData()获取
                    var data = vc.getData();
                    console.log('获取到的vc.getData():', data);
                    
                    if (data && typeof data === 'object') {
                        // 检查data是否直接包含用户信息
                        if (data.username || data.phone) {
                            userAccount = data.username || data.phone;
                            console.log('从vc.getData()直接获取到用户账号:', userAccount);
                        } else if (data.userId) {
                            userAccount = data.userId.toString();
                            console.log('从vc.getData().userId获取到用户账号:', userAccount);
                        } 
                        // 检查data是否包含userInfo属性
                        else if (data.userInfo && typeof data.userInfo === 'object') {
                            if (data.userInfo.username || data.userInfo.phone) {
                                userAccount = data.userInfo.username || data.userInfo.phone;
                                console.log('从vc.getData().userInfo获取到用户账号:', userAccount);
                            } else if (data.userInfo.userId) {
                                userAccount = data.userInfo.userId.toString();
                                console.log('从vc.getData().userInfo.userId获取到用户账号:', userAccount);
                            }
                        }
                        // 3. 尝试从nav.getNavUserInfo获取
                        else if (data.nav && data.nav.userInfo) {
                            if (data.nav.userInfo.username || data.nav.userInfo.phone) {
                                userAccount = data.nav.userInfo.username || data.nav.userInfo.phone;
                                console.log('从vc.getData().nav.userInfo获取到用户账号:', userAccount);
                            } else if (data.nav.userInfo.userId) {
                                userAccount = data.nav.userInfo.userId.toString();
                                console.log('从vc.getData().nav.userInfo.userId获取到用户账号:', userAccount);
                            }
                        }
                    }
                    
                    // 2. 尝试从localStorage获取
                    if (!userAccount) {
                        try {
                            let localUserInfo = JSON.parse(window.localStorage.getItem('userInfo'));
                            if (localUserInfo) {
                                console.log('localStorage中的用户信息:', localUserInfo);
                                if (localUserInfo.username || localUserInfo.phone) {
                                    userAccount = localUserInfo.username || localUserInfo.phone;
                                    console.log('从localStorage获取到用户账号:', userAccount);
                                } else if (localUserInfo.userId) {
                                    userAccount = localUserInfo.userId.toString();
                                    console.log('从localStorage.userId获取到用户账号:', userAccount);
                                }
                            }
                        } catch (e) {
                            console.error('解析localStorage中的userInfo失败:', e);
                        }
                    }
                    
                    // 3. 尝试从sessionStorage获取
                    if (!userAccount) {
                        try {
                            let sessionUserInfo = JSON.parse(window.sessionStorage.getItem('userInfo'));
                            if (sessionUserInfo) {
                                console.log('sessionStorage中的用户信息:', sessionUserInfo);
                                if (sessionUserInfo.username || sessionUserInfo.phone) {
                                    userAccount = sessionUserInfo.username || sessionUserInfo.phone;
                                    console.log('从sessionStorage获取到用户账号:', userAccount);
                                } else if (sessionUserInfo.userId) {
                                    userAccount = sessionUserInfo.userId.toString();
                                    console.log('从sessionStorage.userId获取到用户账号:', userAccount);
                                }
                            }
                        } catch (e) {
                            console.error('解析sessionStorage中的userInfo失败:', e);
                        }
                    }
                    
                    console.log('最终获取到的用户账号:', userAccount);
                    var isContractAdminAccount = contractAdminAccounts.includes(userAccount);
                    console.log('是否为合同管理员账号:', isContractAdminAccount);
                    
                    // 只有非合同管理员才需要传入communityId
                    if (!isContractAdminAccount) {
                        var currentCommunity = vc.getCurrentCommunity();
                        console.log('获取到的currentCommunity:', currentCommunity);
                        
                        // 尝试从localStorage直接获取小区信息
                        let localStorageCommunity = JSON.parse(window.localStorage.getItem('hc_currentCommunityInfo'));
                        console.log('从localStorage获取的小区信息:', localStorageCommunity);
                        
                        // 尝试从localStorage获取小区列表
                        let communityList = JSON.parse(window.localStorage.getItem('hc_communityInfos'));
                        console.log('从localStorage获取的小区列表:', communityList);
                        
                        if (currentCommunity && currentCommunity.communityId && currentCommunity.communityId !== '-1') {
                            param.params.communityId = currentCommunity.communityId;
                            console.log('传入的communityId:', currentCommunity.communityId);
                        } else if (localStorageCommunity && localStorageCommunity.communityId) {
                            param.params.communityId = localStorageCommunity.communityId;
                            console.log('从localStorage直接获取并传入的communityId:', localStorageCommunity.communityId);
                        }
                    } else {
                        // 合同管理员账号,强制不传入communityId
                        console.log('合同管理员账号,强制不传入communityId');
                        // 确保param.params中没有communityId
                        delete param.params.communityId;
                        console.log('确保param.params中没有communityId');
                    }
                    
                    console.log('发送请求的参数:', param);
                    
                    //发送get请求
                    vc.http.apiGet('/assetImportLog/queryAssetImportLog',
                        param,
                        function (json, res) {
                            console.log('收到请求响应:', json, res);
                            
                            try {
                                let _assetImportLogInfo = JSON.parse(json);
                                console.log('解析后的数据:', _assetImportLogInfo);
                                
                                // 验证数据格式,确保所有必要字段存在
                                if (_assetImportLogInfo && _assetImportLogInfo.code == 0) {
                                    var logData = _assetImportLogInfo.data || [];
                                    
                                    // 强制Vue更新 - 解决响应式问题
                                    vc.component.assetImportLogInfo.total = _assetImportLogInfo.total || 0;
                                    vc.component.assetImportLogInfo.records = _assetImportLogInfo.records || 0;
                                    vc.component.assetImportLogInfo.logs = [];
                                    
                                    setTimeout(() => {
                                        // 深拷贝数据,确保响应式
                                        var deepCopiedData = JSON.parse(JSON.stringify(logData));
                                        vc.component.assetImportLogInfo.logs = deepCopiedData;
                                        
                                        console.log('设置组件数据:', vc.component.assetImportLogInfo);
                                        
                                        // 手动触发Vue更新
                                        if (vc.component.$forceUpdate) {
                                            vc.component.$forceUpdate();
                                        }
                                        
                                        // 初始化分页组件
                                        var totalPages = Math.ceil(vc.component.assetImportLogInfo.total / _rows) || 1;
                                        vc.emit('pagination', 'init', {
                                            total: totalPages,
                                            dataCount: vc.component.assetImportLogInfo.total,
                                            currentPage: _page
                                        });
                                    }, 0);
                                } else {
                                    // 数据格式不符合预期,显示友好错误信息
                                    console.warn('导入日志数据格式不符合预期:', _assetImportLogInfo);
                                    vc.toast('查询导入日志失败: ' + (_assetImportLogInfo.msg || '未知错误'));
                                    // 重置数据,避免显示旧数据
                                    vc.component.assetImportLogInfo.total = 0;
                                    vc.component.assetImportLogInfo.records = 0;
                                    vc.component.assetImportLogInfo.logs = [];
                                    
                                    // 重置分页组件
                                    vc.emit('pagination', 'init', {
                                        total: 0,
                                        dataCount: 0,
                                        currentPage: _page
                                    });
                                }
                            } catch (e) {
                                // 解析失败,显示友好错误信息
                                console.error('解析导入日志失败:', e, json);
                                vc.toast('解析导入日志失败: ' + e.message);
                                // 重置数据,避免显示旧数据
                                vc.component.assetImportLogInfo.total = 0;
                                vc.component.assetImportLogInfo.records = 0;
                                vc.component.assetImportLogInfo.logs = [];
                                
                                // 重置分页组件
                                vc.emit('pagination', 'init', {
                                    total: 0,
                                    dataCount: 0,
                                    currentPage: _page
                                });
                            }
                        },
                        function (errInfo, error) {
                            // 详细记录错误信息,便于调试
                            console.error('请求导入日志失败:', errInfo, error);
                            
                            // 显示友好的错误信息
                            let errorMsg = '查询导入日志失败: ';
                            if (errInfo && errInfo.includes('Read timed out')) {
                                errorMsg += '请求超时,请稍后重试';
                            } else if (errInfo && errInfo.includes('I/O error')) {
                                errorMsg += '网络异常,请检查网络连接';
                            } else {
                                errorMsg += (errInfo || '未知错误');
                            }
                            
                            vc.toast(errorMsg);
                            // 重置数据,避免显示旧数据
                            vc.component.assetImportLogInfo.total = 0;
                            vc.component.assetImportLogInfo.records = 0;
                            vc.component.assetImportLogInfo.logs = [];
                            
                            // 重置分页组件
                            vc.emit('pagination', 'init', {
                                total: 0,
                                dataCount: 0,
                                currentPage: _page
                            });
                        }
                    );
                } catch (e) {
                    console.error('调用_listAssetImportLogs方法失败:', e);
                }
            },
            _queryData: function () {
                vc.component._listAssetImportLogs(DEFAULT_PAGE, DEFAULT_ROWS);
            },
            _openDetail: function (_log) {
                vc.jumpToPage('/#/pages/property/assetImportLogDetail?logId=' + _log.logId + "&logType=" + _log.logType);
            }
        }
    });
})(window.vc);