| | |
| | | } |
| | | console.log('使用的userId:', userId); |
| | | |
| | | // 构建表单数据,与物品信息导入功能保持一致 |
| | | // 构建表单数据,与房产导入功能保持一致 |
| | | var param = new FormData(); |
| | | param.append("uploadFile", vc.component.importPurchaseApplyInfo.excelTemplate); // 添加Excel文件 |
| | | param.append('communityId', vc.component.importPurchaseApplyInfo.communityId); // 添加小区ID |
| | | |
| | | // 获取用户ID,添加异常处理 |
| | | let userId = '-1'; |
| | | try { |
| | | let userInfo = vc.getData('/nav/getUserInfo'); |
| | | 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); |
| | | } |
| | | param.append('importAdapt', "importPurchaseApply"); // 指定使用的导入适配器 |
| | | param.append('userId', userId); // 添加userId参数 |
| | | param.append('USER-ID', userId); // 添加USER-ID参数(大写形式) |
| | | param.append('USER-ID', userId); // 添加USER-ID参数(大写形式),与房产导入保持一致 |
| | | |
| | | console.log('准备发送导入请求'); |
| | | // 上传文件并处理响应,与物品信息导入功能保持一致 |
| | | // 上传文件并处理响应,与房产导入功能保持一致 |
| | | vc.http.upload( |
| | | 'importPurchaseApply', // 模块名 |
| | | 'assetImport', // 模块名 |
| | | 'importData', // 方法名 |
| | | param, // 参数 |
| | | { |
| | | emulateJSON: true, |
| | | // 移除emulateJSON,避免与Content-Type冲突 |
| | | headers: { |
| | | "Content-Type": "multipart/form-data" |
| | | } |
| | | }, |
| | | function (json, res) { |
| | | try { |
| | | console.log('导入请求成功响应:', json); |
| | | let _json = JSON.parse(json); |
| | | console.log('解析后的响应数据:', _json); |
| | | if (_json.code == 0) { |
| | | console.log('导入成功,data:', _json.data); |
| | | vc.toast(_json.data); |
| | | $('#importPurchaseApplyModel').modal('hide'); |
| | | // 触发列表刷新 |
| | | vc.emit('purchaseApplyManage', 'listPurchaseApplys', {}); |
| | | return; |
| | | } |
| | | // 处理错误信息 |
| | | let errorMsg = _json.msg; |
| | | if (!errorMsg || errorMsg === null || errorMsg === undefined || errorMsg.includes('null') || errorMsg.includes('undefined')) { |
| | | errorMsg = '导入失败,请检查数据格式是否正确,或联系管理员查看详细日志'; |
| | | } |
| | | console.log('导入失败,错误信息:', errorMsg); |
| | | vc.toast(errorMsg, 10000); |
| | | } catch (e) { |
| | | console.error('导入响应解析失败:', e); |
| | | vc.toast('导入响应解析失败,请重试', 10000); |
| | | 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.error('导入请求失败:', errInfo, error); |
| | | // 处理网络错误 |
| | | 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.log('最终显示的错误信息:', errorMsg); // 打印最终显示的错误信息到console |
| | | vc.toast(errorMsg, 10000); |
| | | // 关闭模态框 |
| | | $('#importPurchaseApplyModel').modal('hide'); |
| | | }); |
| | | }, |
| | | /** |