| New file |
| | |
| | | (function (vc) { |
| | | var $that = this; |
| | | vc.extends({ |
| | | data: { |
| | | importPurchaseApplyInfo: { |
| | | communityId: vc.getCurrentCommunity().communityId, |
| | | excelTemplate: '' |
| | | } |
| | | }, |
| | | _initEvent: function () { |
| | | vc.on('importPurchaseApply', 'openImportPurchaseApplyModal', function () { |
| | | $('#importPurchaseApplyModel').modal('show'); |
| | | $that.resetImportForm(); |
| | | }); |
| | | // 监听模态框关闭事件,重置表单 |
| | | $('#importPurchaseApplyModel').on('hidden.bs.modal', function () { |
| | | $that.resetImportForm(); |
| | | }); |
| | | }, |
| | | methods: { |
| | | importPurchaseApplyValidate() { |
| | | return vc.validate.validate({ |
| | | importPurchaseApplyInfo: $that.importPurchaseApplyInfo |
| | | }, { |
| | | 'importPurchaseApplyInfo.communityId': [{ |
| | | limit: "required", |
| | | param: "", |
| | | errInfo: "数据异常还没有入驻小区" |
| | | }], |
| | | 'importPurchaseApplyInfo.excelTemplate': [{ |
| | | limit: "required", |
| | | param: "", |
| | | errInfo: "文件不能为空" |
| | | }] |
| | | }); |
| | | }, |
| | | // 重置导入表单 |
| | | resetImportForm() { |
| | | $that.importPurchaseApplyInfo = { |
| | | communityId: vc.getCurrentCommunity().communityId, |
| | | excelTemplate: '' |
| | | }; |
| | | // 重置文件输入框 |
| | | var fileInput = document.getElementById('excelTemplate'); |
| | | if (fileInput) { |
| | | fileInput.value = ''; |
| | | } |
| | | console.log('导入表单已重置'); |
| | | }, |
| | | getExcelTemplate: function (e) { |
| | | if (e.target.files && e.target.files[0]) { |
| | | $that.importPurchaseApplyInfo.excelTemplate = e.target.files[0]; |
| | | console.log('选择的文件:', $that.importPurchaseApplyInfo.excelTemplate); |
| | | } else { |
| | | $that.importPurchaseApplyInfo.excelTemplate = ''; |
| | | console.log('未选择文件'); |
| | | } |
| | | }, |
| | | _importData: function () { |
| | | if (!$that.importPurchaseApplyValidate()) { |
| | | vc.toast(vc.validate.errInfo); |
| | | return; |
| | | } |
| | | // 导入数据 |
| | | if (!$that.checkFileType($that.importPurchaseApplyInfo.excelTemplate.name.split('.')[1])) { |
| | | vc.toast('不是有效的Excel格式'); |
| | | return; |
| | | } |
| | | // 使用实际的userId,如果没有则使用默认值 |
| | | let userId = '-1'; |
| | | try { |
| | | // 尝试从不同来源获取userId |
| | | let userInfo = vc.getData('userInfo'); |
| | | if (userInfo && userInfo.userId) { |
| | | userId = userInfo.userId; |
| | | } else { |
| | | // 尝试从localStorage直接获取 |
| | | let localUserInfo = JSON.parse(window.localStorage.getItem('userInfo')); |
| | | if (localUserInfo && localUserInfo.userId) { |
| | | userId = localUserInfo.userId; |
| | | } |
| | | } |
| | | } catch (e) { |
| | | console.error('获取userId失败:', e); |
| | | } |
| | | console.log('使用的userId:', userId); |
| | | |
| | | // 构建表单数据,与房产导入保持一致 |
| | | var param = new FormData(); |
| | | param.append("uploadFile", $that.importPurchaseApplyInfo.excelTemplate); // 添加Excel文件 |
| | | param.append('communityId', $that.importPurchaseApplyInfo.communityId); // 添加小区ID |
| | | param.append('importAdapt', "importPurchaseApply"); // 指定使用的导入适配器 |
| | | 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 |
| | | $('#importPurchaseApplyModel').modal('hide'); // 关闭模态框 |
| | | // 跳转到导入日志详情页面 |
| | | vc.jumpToPage('/#/pages/property/assetImportLogDetail?logId=' + _json.data.logId + '&logType=importPurchaseApply'); |
| | | 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); |
| | | // 关闭模态框 |
| | | $('#importPurchaseApplyModel').modal('hide'); |
| | | }); |
| | | }, |
| | | checkFileType: function(fileType) { |
| | | const acceptTypes = ['xlsx', 'xls']; |
| | | for (var i = 0; i < acceptTypes.length; i++) { |
| | | if (fileType === acceptTypes[i]) { |
| | | return true; |
| | | } |
| | | } |
| | | return false; |
| | | } |
| | | } |
| | | }); |
| | | })(window.vc); |