123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- 'use strict';
- const importXML = (() => {
- // 通用设置和工具
- const config = importXMLBase.CONFIG;
- const util = importXMLBase.UTIL;
- const {
- fixedFlag,
- billType,
- rationType,
- projectType
- } = commonConstants;
- const { AdjustType } = config;
- const {
- getValue,
- arrayValue,
- getFee,
- getFlag,
- getItemsRecur,
- } = util;
- //导入的文件类型,界面选的文件类型是生成项目的文件类型,这里的文件类型指的是,要导入文件的类型,
- //导入文件类型不同,导入数据不同
- let importFileKind = '';
- const countData = {
- projectCount: 0, //项目数量
- projectGLJCount: 0, //项目人材机数量
- ratioCount: 0, //组成物数量
- unitPriceCount: 0, //单价数量
- unitPriceFileCount: 0, //单价文件数量
- };
- // 建设项目
- function loadProject(xmlObj) {
- }
- /**
- * 解压cos、zip文件
- * @param {File} file - 上传的文件
- * @return {Object} 解压出来的xml文件名称与xml文件文本内容映射
- */
- async function unzipFile(file) {
- const jsZip = new JSZip();
- const zip = await jsZip.loadAsync(file);
- const map = {};
- for (const fileName in zip.files) {
- // 将二进制数据转换成字符串
- map[fileName] = await jsZip.file(fileName).async('string');
- }
- return map;
- }
- //从xml文件中提取数据
- async function extractData(file, escape = false) {
- const fileMap = await unzipFile(file);
- const projectXML = fileMap['Project.xml'];
- if (!projectXML) {
- throw '无有效数据';
- }
- const xmlObjMap = {};
- for (const fileName in fileMap) {
- const xmlStr = escape ? util.escapeXMLEntity(fileMap[fileName]) : fileMap[fileName];
- //将xml格式良好的字符串转换成对象
- const x2js = new X2JS();
- let xmlObj = x2js.xml_str2json(xmlStr);
- xmlObj = JSON.parse(util.restoreXMLEntity(JSON.stringify(xmlObj)));
- if (!xmlObj) {
- throw '无有效数据。';
- }
- xmlObjMap[fileName] = xmlObj;
- }
- if (escape) {
- xmlStr = util.escapeXMLEntity(xmlStr);
- }
- let xmlObj = x2js.xml_str2json(xmlStr);
- xmlObj = JSON.parse(util.restoreXMLEntity(JSON.stringify(xmlObj)));
- if (!xmlObj) {
- throw '无有效数据。';
- }
- //console.log(xmlObj);
- //提取数据
- return loadProject(xmlObj);
- };
- // 接受上传的文件类型(不同的省份可以上传的文件不同)
- const accept = ['.zip', '.cos'];
- return {
- accept,
- }
- })();
|