/** 权限组 **/ (function(vc) { vc.extends({ data: { assetImportInfo: { communityId: vc.getCurrentCommunity().communityId, excelTemplate: '', remark: "" } }, _initMethod: function() { }, _initEvent: function() { }, methods: { assetImportValidate: function() { return vc.validate.validate({ assetImportInfo: vc.component.assetImportInfo }, { 'assetImportInfo.excelTemplate': [{ limit: "required", param: "", errInfo: "文件不能为空" }], 'assetImportInfo.communityId': [{ limit: "required", param: "", errInfo: "还未入驻小区,请先入驻小区" }] }); }, _openDownloadHcExcelTemplate: function() { //下载 模板 vc.jumpToPage('/import/hc.xlsx') }, getExcelTemplate: function(e) { //console.log("getExcelTemplate 开始调用") vc.component.assetImportInfo.excelTemplate = e.target.files[0]; }, _importData: function() { if (!vc.component.assetImportValidate()) { vc.toast(vc.validate.errInfo); return; } // 导入数据 if (!vc.component.checkFileType(vc.component.assetImportInfo.excelTemplate.name.split('.')[1])) { vc.toast('不是有效的Excel格式'); return; } // 移除文件大小限制检查 // if (!vc.component.checkFileSize(vc.component.assetImportInfo.excelTemplate.size)) { // vc.toast('Excel文件大小不能超过20M'); // return; // } // 重新获取当前社区的ID,确保使用的是最新的社区ID let currentCommunity = vc.getCurrentCommunity(); if (!currentCommunity || !currentCommunity.communityId) { vc.toast('无法获取当前社区信息,请重新选择社区'); return; } let currentCommunityId = currentCommunity.communityId; console.log('当前社区ID:', currentCommunityId); console.log('当前社区信息:', currentCommunity); var param = new FormData(); param.append("uploadFile", vc.component.assetImportInfo.excelTemplate); param.append('communityId', currentCommunityId); param.append('importAdapt', "importRoomOwnerV2"); // 使用实际的userId,如果没有则使用默认值 let userId = vc.getData() && vc.getData().userInfo && vc.getData().userInfo.userId ? vc.getData().userInfo.userId : '-1'; param.append('userId', userId); vc.http.upload( 'assetImport', 'importData', param, { headers: { "Content-Type": "multipart/form-data" } }, function(json, res) { // 打印完整的响应数据,便于调试 console.log('导入响应完整数据:', json); try { let _json = JSON.parse(json); console.log('导入响应解析后数据:', _json); if (res.status == 200 && _json.code == 0) { //关闭model vc.toast("处理成功"); vc.jumpToPage('/#/pages/property/listOwner') return; } // 导入失败,显示错误信息 let errorMsg = _json.msg; // 确保错误信息有意义,避免显示"数据有误: null" if (!errorMsg || errorMsg.includes('null')) { errorMsg = '导入失败,请检查数据格式是否正确,或联系管理员查看详细日志'; } // 优化SocketException错误信息 if (errorMsg && errorMsg.includes('java.net.SocketException')) { errorMsg = '导入失败: 网络连接异常,请检查网络连接或稍后重试'; } vc.toast(errorMsg, 10000); } catch (e) { // 解析JSON失败,直接显示原始响应数据 console.error('解析JSON失败:', e, json); vc.toast(json, 10000); } }, function(errInfo, error) { console.log('请求失败处理:', errInfo, error); // 确保即使 errInfo 为空,也能显示错误信息 let errorMsg = errInfo; if (!errorMsg || errorMsg.trim() === '') { errorMsg = '导入失败,请检查文件格式或联系管理员'; } else { // 优化错误信息显示 if (errorMsg.includes('java.net.SocketException')) { errorMsg = '导入失败: 网络连接异常,请检查网络连接或稍后重试'; } else if (errorMsg.includes('timeout')) { errorMsg = '导入失败: 请求超时,请检查网络连接或稍后重试'; } else if (errorMsg.indexOf("") > 0) { errorMsg = '导入失败: 服务器内部错误,请联系管理员'; } } vc.toast(errorMsg, 10000); }); }, _exitCommunityData: function() { vc.jumpToPage('/callComponent/assetImport/exitCommunityData?communityId=' + vc.getCurrentCommunity().communityId); }, _openAssetImportLog: function() { vc.jumpToPage('/#/pages/property/assetImportLog') }, checkFileType: function(fileType) { const acceptTypes = ['xls', 'xlsx']; for (var i = 0; i < acceptTypes.length; i++) { if (fileType === acceptTypes[i]) { return true; } } return false; }, checkFileSize: function(fileSize) { //2M const MAX_SIZE = 20 * 1024 * 1024; if (fileSize > MAX_SIZE) { return false; } return true; } } }); })(window.vc);