guangdong_2018_import.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. 'use strict';
  2. const importXML = (() => {
  3. // 通用设置和工具
  4. const config = importXMLBase.CONFIG;
  5. const util = importXMLBase.UTIL;
  6. const {
  7. fixedFlag,
  8. billType,
  9. rationType,
  10. projectType
  11. } = commonConstants;
  12. const { AdjustType } = config;
  13. const {
  14. getValue,
  15. arrayValue,
  16. getFee,
  17. getFlag,
  18. getItemsRecur,
  19. } = util;
  20. //导入的文件类型,界面选的文件类型是生成项目的文件类型,这里的文件类型指的是,要导入文件的类型,
  21. //导入文件类型不同,导入数据不同
  22. let importFileKind = '';
  23. const countData = {
  24. projectCount: 0, //项目数量
  25. projectGLJCount: 0, //项目人材机数量
  26. ratioCount: 0, //组成物数量
  27. unitPriceCount: 0, //单价数量
  28. unitPriceFileCount: 0, //单价文件数量
  29. };
  30. // 建设项目
  31. function loadProject(xmlObj) {
  32. }
  33. /**
  34. * 解压cos、zip文件
  35. * @param {File} file - 上传的文件
  36. * @return {Object} 解压出来的xml文件名称与xml文件文本内容映射
  37. */
  38. async function unzipFile(file) {
  39. const jsZip = new JSZip();
  40. const zip = await jsZip.loadAsync(file);
  41. const map = {};
  42. for (const fileName in zip.files) {
  43. // 将二进制数据转换成字符串
  44. map[fileName] = await jsZip.file(fileName).async('string');
  45. }
  46. return map;
  47. }
  48. //从xml文件中提取数据
  49. async function extractData(file, escape = false) {
  50. const fileMap = await unzipFile(file);
  51. const projectXML = fileMap['Project.xml'];
  52. if (!projectXML) {
  53. throw '无有效数据';
  54. }
  55. const xmlObjMap = {};
  56. for (const fileName in fileMap) {
  57. const xmlStr = escape ? util.escapeXMLEntity(fileMap[fileName]) : fileMap[fileName];
  58. //将xml格式良好的字符串转换成对象
  59. const x2js = new X2JS();
  60. let xmlObj = x2js.xml_str2json(xmlStr);
  61. xmlObj = JSON.parse(util.restoreXMLEntity(JSON.stringify(xmlObj)));
  62. if (!xmlObj) {
  63. throw '无有效数据。';
  64. }
  65. xmlObjMap[fileName] = xmlObj;
  66. }
  67. if (escape) {
  68. xmlStr = util.escapeXMLEntity(xmlStr);
  69. }
  70. let xmlObj = x2js.xml_str2json(xmlStr);
  71. xmlObj = JSON.parse(util.restoreXMLEntity(JSON.stringify(xmlObj)));
  72. if (!xmlObj) {
  73. throw '无有效数据。';
  74. }
  75. //console.log(xmlObj);
  76. //提取数据
  77. return loadProject(xmlObj);
  78. };
  79. // 接受上传的文件类型(不同的省份可以上传的文件不同)
  80. const accept = ['.zip', '.cos'];
  81. return {
  82. accept,
  83. }
  84. })();