zhangjq
2026-01-27 6f51f667ae7b13dca029045c221d0b1722cf98df
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
(function(vc) {
    vc.extends({
        data: {
            importOwnerRoomInfo: {
                communityId: vc.getCurrentCommunity().communityId,
                excelTemplate: ''
            }
        },
        _initMethod: function() {},
        _initEvent: function() {
            vc.on('importOwnerRoom', 'openImportOwnerRoomModal', function(_param) {
                $('#importOwnerRoomModel').modal('show');
            });
        },
        methods: {
            importOwnerRoomValidate() {
                return vc.validate.validate({
                    importOwnerRoomInfo: $that.importOwnerRoomInfo
                }, {
                    'importOwnerRoomInfo.communityId': [{
                        limit: "required",
                        param: "",
                        errInfo: "数据异常还没有入驻小区"
                    }],
                    'importOwnerRoomInfo.excelTemplate': [{
                        limit: "required",
                        param: "",
                        errInfo: "文件不能为空"
                    }]
                });
            },
            _importData: function() {
                if (!$that.importOwnerRoomValidate()) {
                    vc.toast(vc.validate.errInfo);
                    return;
                }
                // 导入数据
                if (!$that.checkOwnerFileType($that.importOwnerRoomInfo.excelTemplate.name.split('.')[1])) {
                    vc.toast('不是有效的Excel格式');
                    return;
                }
                // 移除文件大小限制检查
                // if (!$that.checkOwnerFileSize($that.importOwnerRoomInfo.excelTemplate.size)) {
                //     vc.toast('Excel文件大小不能超过20M');
                //     return;
                // }
                // 使用实际的userId,如果没有则使用默认值
                let userId = vc.getData() && vc.getData().userInfo && vc.getData().userInfo.userId ? vc.getData().userInfo.userId : '-1';
                
                // 构建表单数据,与车辆导入保持一致
                var param = new FormData();
                param.append("uploadFile", $that.importOwnerRoomInfo.excelTemplate); // 添加Excel文件
                param.append('communityId', $that.importOwnerRoomInfo.communityId); // 添加小区ID
                param.append('importAdapt', "importRoomOwnerV2"); // 指定使用的导入适配器
                param.append('userId', userId); // 添加userId参数
                param.append('USER-ID', userId); // 添加USER-ID参数(大写形式),与车辆导入保持一致
 
                // 上传文件并处理响应,与车辆导入保持一致
                vc.http.upload(
                    'assetImport', // 模块名
                    'importData', // 方法名
                    param, // 参数
                    {
                        // 移除emulateJSON,避免与Content-Type冲突
                        headers: {
                            "Content-Type": "multipart/form-data"
                        }
                    },
                    function(json, res) {
                        // 成功回调
                        console.log('导入请求成功响应:', json); // 打印完整的响应数据到console
                        let _json = JSON.parse(json);
                        console.log('解析后的响应数据:', _json); // 打印解析后的响应数据到console
                        if (_json.code == 0) {
                            // 导入成功
                            vc.toast("处理成功");
                            console.log('导入成功,logId:', _json.data.logId); // 打印logId到console
                            $('#importOwnerRoomModel').modal('hide'); // 关闭模态框
                            // 跳转到导入日志详情页面
                            vc.jumpToPage('/#/pages/property/assetImportLogDetail?logId=' + _json.data.logId + '&logType=importRoomOwnerV2');
                            return;
                        }
                        // 导入失败,显示错误信息
                        console.log('导入失败,错误信息:', _json.msg); // 打印失败错误信息到console
                        // 确保错误信息有意义,避免显示"数据有误: null"
                        let errorMsg = _json.msg;
                        if (!errorMsg || errorMsg === null || errorMsg === undefined || errorMsg.includes('null') || errorMsg.includes('undefined')) {
                            errorMsg = '导入失败,请检查数据格式是否正确,或联系管理员查看详细日志';
                        }
                        // 优化错误信息显示,替换不友好的后端错误
                        if (errorMsg.includes('sheet 未找到') || errorMsg.includes('excel sheet')) {
                            errorMsg = '导入失败:未找到正确的sheet,请确保Excel文件中包含名称为"住宅物业费导入新模板"或"商铺物业费导入新模板"的sheet,且名称正确无空格';
                        }
                        vc.toast(errorMsg, 10000);
                    },
                    function(errInfo, error) {
                        // 失败回调
                        console.log('请求失败处理,原始错误信息:', errInfo); // 打印原始错误信息到console
                        console.log('请求失败,错误对象:', error); // 打印完整的错误对象到console
                        // 确保错误信息有意义,避免显示"null"或空错误
                        let errorMsg = errInfo;
                        if (!errorMsg || errorMsg === null || errorMsg === undefined || errorMsg.trim() === '') {
                            errorMsg = '导入失败,请检查文件格式或网络连接,或联系管理员';
                        }
                        // 优化网络错误信息
                        if (errorMsg.includes('SocketException') || errorMsg.includes('timeout') || errorMsg.includes('Network Error')) {
                            errorMsg = '导入失败:网络连接异常,请检查网络连接或稍后重试';
                        }
                        console.log('最终显示的错误信息:', errorMsg); // 打印最终显示的错误信息到console
                        vc.toast(errorMsg, 10000);
                        // 关闭模态框
                        $('#importOwnerRoomModel').modal('hide');
                    });
            },
            clearAddFeeConfigInfo: function() {
                $that.importOwnerRoomInfo = {
                    communityId: vc.getCurrentCommunity().communityId,
                    excelTemplate: ''
                };
            },
            _changeFeeTypeCd: function(_feeTypeCd) {},
            getExcelTemplate: function(e) {
                $that.importOwnerRoomInfo.excelTemplate = e.target.files[0];
            },
            checkOwnerFileType: function(fileType) {
                const acceptTypes = ['xlsx', 'xls'];
                for (var i = 0; i < acceptTypes.length; i++) {
                    if (fileType === acceptTypes[i]) {
                        return true;
                    }
                }
                return false;
            },
            checkOwnerFileSize: function(fileSize) {
                //2M
                const MAX_SIZE = 20 * 1024 * 1024;
                if (fileSize > MAX_SIZE) {
                    return false;
                }
                return true;
            }
        }
    });
})(window.vc);