exportStdInterfaceBase.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Zhong
  6. * @date 2019/6/20
  7. * @version
  8. */
  9. const XML_EXPORT_BASE = (() => {
  10. // 属性类型
  11. const TYPE = {
  12. DATE: 1, //日期类型YYYY-MM-DD
  13. DATE_TIME: 2, //日期类型YYY-MM-DDTHH:mm:ss
  14. INT: 3, //整数类型
  15. DECIMAL: 4, //数值类型,不限制小数位数
  16. NUM2: 5, //数值类型2:最多两位小数
  17. BOOL: 6 //布尔型
  18. };
  19. // 空白字符处理
  20. const WHITE_SPACE = {
  21. COLLAPSE: 1 //移除所有空白字符(换行、回车、空格以及制表符会被替换为空格,开头和结尾的空格会被移除,而多个连续的空格会被缩减为一个单一的空格)
  22. };
  23. // 计税方法
  24. const TAX_TYPE = {
  25. '1': '一般计税法',
  26. '2': '简易计税法',
  27. };
  28. // 承包人材料调整类型
  29. const ADJUST_TYPE = {
  30. info: 'priceInfo', // 造价信息差额调整法
  31. coe: 'priceCoe' // 价格指数调整法
  32. };
  33. // 加载数据间隔,减少服务器压力
  34. const TIMEOUT_TIME = 500;
  35. // 导出粒度
  36. const GRANULARITY = {
  37. PROJECT: 1, //导出建设项目
  38. ENGINEERING: 2, //导出单项工程
  39. TENDER: 3 //导出单位工程
  40. };
  41. // 导出的文件类型选项
  42. const EXPORT_KIND = {
  43. Tender: 1, //投标
  44. Bid: 2, //招标
  45. Control: 3 //控制价
  46. };
  47. // 配置项
  48. const CONFIG = Object.freeze({
  49. TYPE,
  50. WHITE_SPACE,
  51. TAX_TYPE,
  52. ADJUST_TYPE,
  53. TIMEOUT_TIME,
  54. GRANULARITY,
  55. EXPORT_KIND
  56. });
  57. // 缓存项 不需要的时候需要清空
  58. let _cache = {
  59. // 失败列表
  60. failList: [],
  61. // 项目数据(不包含详细数据,项目管理数据)
  62. projectsData: {},
  63. // 当前导出类型,默认投标
  64. exportKind: EXPORT_KIND.Tender,
  65. // 记录拉取的单位工程项目详细数据,导出的时候,可能会导出多个文件,只有导出第一个文件的时候需要请求数据
  66. tenderDetailMap: {}
  67. };
  68. // 返回缓存项
  69. function getItem(key) {
  70. return _cache[key] || null;
  71. }
  72. // 设置缓存项
  73. function setItem(key, value) {
  74. // 与原数据是同类型的数据才可设置成功
  75. if (_cache[key] &&
  76. Object.prototype.toString.call(_cache[key]) ===
  77. Object.prototype.toString.call(value)) {
  78. _cache[key] = value;
  79. }
  80. }
  81. // 清空缓存项
  82. function clear() {
  83. _cache.failList = [];
  84. _cache.projectData = {};
  85. _cache.exportKind = EXPORT_KIND.Tender;
  86. _cache.tenderDetailMap = {};
  87. }
  88. const CACHE = Object.freeze({
  89. getItem,
  90. setItem,
  91. clear
  92. });
  93. /*
  94. * 定义不设置一个Node方法统一进入的原因:模板化比较直观,不分开定义节点的话,调用传参也很麻烦而且不直观。
  95. * 一个节点对应一个构造方法,方便调整配置、方便其他版本开发、接手的人看起来更直观
  96. * @param {String}name 节点名
  97. * {Array}attrs 节点属性数据
  98. * {Array}failList 失败列表
  99. * @return {void}
  100. * */
  101. function Element(name, attrs) {
  102. this.name = name;
  103. let checkData = check(name, attrs);
  104. this.fail = checkData.failHints;
  105. this.attrs = checkData.filterAttrs;
  106. handleXMLEntity(this.attrs);
  107. this.children = [];
  108. _cache.failList.push(...this.fail);
  109. }
  110. /*
  111. * xml字符实体的处理,这些特殊字符不处理会导致xml文件格式出错:""、<>、&
  112. * 要先处理&amp
  113. * */
  114. let _xmlEntity = {
  115. '&': '&amp;',
  116. '\n': '&#xA;',
  117. '"': '&quot;',
  118. '\'': '&apos;',
  119. '<': '&lt;',
  120. '>': '&gt;'
  121. };
  122. // 对每个元素的所有属性值进行特殊字符处理
  123. function handleXMLEntity(attrs) {
  124. for (let attr of attrs) {
  125. if (!attr.value) {
  126. continue;
  127. }
  128. for (let [key, value] of Object.entries(_xmlEntity)) {
  129. attr.value = attr.value.replace(new RegExp(key, 'g'), value);
  130. }
  131. }
  132. }
  133. // 获取处理实体字符后的数据
  134. function getParsedData(arr) {
  135. return arr.map(data => {
  136. for (let [key, value] of Object.entries(_xmlEntity)) {
  137. data = data.replace(new RegExp(key, 'g'), value);
  138. }
  139. return data;
  140. });
  141. }
  142. /*
  143. * 检查
  144. * 创建节点时检查节点的数据
  145. * 1.长度限制minLen,maxLen
  146. * 2.值的限制,固定范围:enumeration
  147. * @param {String}eleName 节点名称
  148. * {Array}datas 需要检查的属性数据
  149. * @return {Object} failHints没通过的属性提示 filterAttrs过滤后的属性数据(失败提示在属性是必须的时候才提示,如果该属性失败了,但是是非必要属性,那么该属性不显示)
  150. * */
  151. function check(eleName, datas) {
  152. let rst = {failHints: [], filterAttrs: []};
  153. let dateReg = /([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8])))/;
  154. for (let data of datas) {
  155. // 值统一转换成String
  156. data.value = !isDef(data.value) ? '' : String(data.value);
  157. if (data.whiteSpace && data.whiteSpace === WHITE_SPACE.COLLAPSE) { //处理空格相关
  158. data.value = data.value.replace(/[\r,\n,\t]/g, ' ');
  159. data.value = data.value.trim();
  160. data.value = data.value.replace(/\s{1,}/g, ' ');
  161. }
  162. /*if (!data.value && !data.minLen && !data.enumeration) { //值为空,且没有限制最小字符数,且没有限制值,则不需判断
  163. rst.filterAttrs.push(data);
  164. continue;
  165. }*/
  166. let isFail = false,
  167. tempFail = '';
  168. if (data.minLen && data.value.length < data.minLen){
  169. isFail = true;
  170. tempFail = data.failHint
  171. ? `${data.failHint}字符数不可小于${data.minLen}个。`
  172. :`${eleName}-“${data.name}”字符数不可小于${data.minLen}个。`;
  173. } else if (data.maxLen && data.value.length > data.maxLen) {
  174. isFail = true;
  175. tempFail = data.failHint
  176. ? `${data.failHint}字符数不可大于${data.maxLen}个。`
  177. : `${eleName}-“${data.name}”字符数不可大于${data.maxLen}个。`;
  178. } else if (data.enumeration && !data.enumeration.includes(data.value)) {
  179. isFail = true;
  180. let enumerationHint = data.enumerationHint
  181. ? data.enumerationHint.join(';')
  182. : data.enumeration.join(';');
  183. tempFail = data.failHint
  184. ? `${data.failHint}只能从“${enumerationHint}”中选择。`
  185. : `${eleName}-“${data.name}”只能从“${enumerationHint}”中选择。`;
  186. } else if (data.type && data.type === TYPE.DATE && !dateReg.test(data.value)) {
  187. isFail = true;
  188. tempFail = data.failHint
  189. ? `${data.failHint}日期格式必须是YYYY-MM-DD。`
  190. : `${eleName}-“${data.name}”日期格式必须是YYYY-MM-DD。`;
  191. } else if (data.type && data.type === TYPE.INT && !Number.isInteger(parseFloat(data.value))) {
  192. isFail = true;
  193. tempFail = data.failHint
  194. ? `${data.failHint}必须为整数。`
  195. : `${eleName}-“${data.name}”必须为整数。`;
  196. } else if (data.type && data.type === TYPE.DECIMAL && isNaN(parseFloat(data.value))) {
  197. isFail = true;
  198. tempFail = data.failHint
  199. ? `${data.failHint}必须为数值。`
  200. : `${eleName}-“${data.name}”必须为数值。`;
  201. } else if (data.type && data.type === TYPE.NUM2) {
  202. let v = parseFloat(data.value);
  203. if (isNaN(v)) {
  204. isFail = true;
  205. tempFail = data.failHint
  206. ? `${data.failHint}必须为数值。`
  207. : `${eleName}-“${data.name}”必须为数值。`;
  208. } else if (!data.value.length || (data.value.split('.').length > 1 && data.value.split('.')[1].length > 2)){
  209. isFail = true;
  210. }
  211. } else if (data.type && data.type === TYPE.BOOL && !['true', 'false'].includes(String(data.value))) {
  212. isFail = true;
  213. tempFail = data.failHint
  214. ? `${data.failHint}必须为true或false。`
  215. : `${eleName}-“${data.name}”必须为true或false。`;
  216. }
  217. if (!isFail || data.required) {
  218. rst.filterAttrs.push(data);
  219. }
  220. if (isFail && data.required && tempFail) {
  221. rst.failHints.push(tempFail);
  222. }
  223. }
  224. return rst;
  225. }
  226. // 等待一段时间
  227. function setTimeoutSync(handle, time) {
  228. return new Promise((resolve, reject) => {
  229. setTimeout(() => {
  230. if (handle && typeof handle === 'function') {
  231. handle();
  232. }
  233. resolve();
  234. }, time);
  235. });
  236. }
  237. // v是否定义了,不为undefined和null
  238. function isDef(v) {
  239. return typeof v !== 'undefined' && v !== null;
  240. }
  241. // v是否有值,不为undefined、null、''
  242. function hasValue(v) {
  243. return typeof v !== 'undefined' && v !== null && v !== '';
  244. }
  245. /*
  246. * 从fees数组中获取相关费用
  247. * @param {Array}fees 费用数组
  248. * {String}feeFields 费用字段
  249. * @return {Number}
  250. * @example getFee(source.fees, 'common.totalFee')
  251. * */
  252. function getFee(fees, feeFields) {
  253. if (!Array.isArray(fees)) {
  254. return 0;
  255. }
  256. let fields = feeFields.split('.');
  257. let fee = fees.find(data => data.fieldName === fields[0]);
  258. if (!fee) {
  259. return 0;
  260. }
  261. return fee[fields[1]] || 0;
  262. }
  263. /*
  264. * 根据key获取对应的基本信息、工程特征数据
  265. * @param {Array}data
  266. * {String}key
  267. * @return {String}
  268. * @example getValueByKey(source.basicInformation, 'projectScale')
  269. * */
  270. function getValueByKey(data, key) {
  271. for (let d of data) {
  272. if (d.key === key) {
  273. return d.value;
  274. }
  275. if (d.items && d.items.length > 0) {
  276. let findData = d.items.find(x => x.key === key);
  277. if (findData) {
  278. return findData.value;
  279. }
  280. }
  281. }
  282. return '';
  283. }
  284. // 数组打平成对象
  285. function arrayToObj(arr) {
  286. let rst = {};
  287. for (let data of arr) {
  288. rst[data.key] = data.value;
  289. }
  290. return rst;
  291. }
  292. /*
  293. * 检测层数
  294. * @param {Number}maxDepth(最大深度)
  295. * {Object}node(需要检测的清单树节点)
  296. * @return {Boolean}
  297. * */
  298. function validDepth(maxDepth, node) {
  299. let nodeDepth = node.depth();
  300. let allNodes = node.getPosterity();
  301. //检测相对深度
  302. for (let n of allNodes) {
  303. let relativeDepth = n.depth() - nodeDepth;
  304. if (relativeDepth > maxDepth) {
  305. return false;
  306. }
  307. }
  308. return true;
  309. }
  310. /*
  311. * 检测唯一性,有些属性在规定的数据范围内唯一
  312. * @param {Object}constraints(约束池)
  313. * {All}data(检测的数据)
  314. * {String}hint(提示已存在的内容)
  315. * {String}subHint(额外提示,有额外提示时,不用data提示)
  316. * @return {void}
  317. * */
  318. function checkUnique(constraints, data, hint, subHint) {
  319. if (constraints.includes(data)) {
  320. let failHint = subHint
  321. ? `${hint}“${subHint}”已存在`
  322. : `${hint}“${data}”已存在`;
  323. _cache.failList.push(failHint);
  324. } else if (data) {
  325. constraints.push(data);
  326. }
  327. }
  328. //根据数据的NextSiblingID进行排序,返回排序后的数组
  329. function sortByNext(datas) {
  330. let target = [],
  331. temp = {};
  332. for (let data of datas) {
  333. temp[data.ID] = {me: data, next: null, prev: null};
  334. }
  335. for (let data of datas) {
  336. let next = temp[data.NextSiblingID] || null;
  337. temp[data.ID].next = next;
  338. if (next) {
  339. next.prev = temp[data.ID];
  340. }
  341. }
  342. let first = null;
  343. for (let data of datas) {
  344. let me = temp[data.ID];
  345. if (!me.prev) {
  346. first = me;
  347. }
  348. }
  349. if (!first) {
  350. return datas;
  351. }
  352. while (first) {
  353. target.push(first.me);
  354. first = first.next;
  355. }
  356. return target;
  357. }
  358. /*
  359. * 根据粒度获取项目(不包含详细数据)数据
  360. * @param {Number}granularity 导出粒度
  361. * {Number}tenderID 单位工程ID
  362. * {String}userID 用户ID
  363. * @return {Object} 返回的数据结构:{children: [{children: []}]} 最外层为建设项目,中间为单项工程,最底层为单位工程
  364. * */
  365. async function getProjectByGranularity(granularity, tenderID, userID) {
  366. let projectsData = _cache.projectsData;
  367. // 没有数据,需要拉取
  368. if (!Object.keys(projectsData).length) {
  369. projectsData = await ajaxPost('/pm/api/getProjectByGranularity', {user_id: userID, tenderID: tenderID, granularity: granularity});
  370. _cache.projectsData = projectsData;
  371. }
  372. return projectsData;
  373. }
  374. /*
  375. * 通过getData接口获取单位工程详细数据
  376. * @param {Number}tenderID 单位工程ID
  377. * {String}userID 用户ID
  378. * @return {Object} 跟projectObj.project的数据结构一致
  379. * */
  380. async function getTenderDetail(tenderID, userID) {
  381. // 获取单位工程详细数据
  382. let tenderDetail = _cache.tenderDetailMap[tenderID];
  383. if (!tenderDetail) {
  384. tenderDetail = PROJECT.createNew(tenderID, userID);
  385. await tenderDetail.loadDataSync();
  386. _cache.tenderDetailMap[tenderID] = tenderDetail;
  387. }
  388. return tenderDetail;
  389. }
  390. /*
  391. * 提取要导出的数据
  392. * @param {Function}entryFuc 提取数据的入口方法
  393. * {Number}granularity 导出粒度: 1-建设项目、2-单项工程、3-单位工程
  394. * {Number}exportKind 导出的文件类型:1-投标、2-招标、3-控制价
  395. * {Number}tenderID 单位工程ID
  396. * {String}userID 用户ID
  397. * @return {Object} 数据结构为:{name: 'elementName', attrs: [...], children: [...], fail: [...]}
  398. * */
  399. async function extractExportData({entryFuc, granularity, exportKind, tenderID, userID}) {
  400. // 默认导出建设项目
  401. if (!granularity || ![1, 2, 3].includes(granularity)) {
  402. granularity = GRANULARITY.PROJECT;
  403. }
  404. // 默认导出投标文件
  405. if (!exportKind || ![1, 2, 3].includes(exportKind)) {
  406. exportKind = EXPORT_KIND.Tender;
  407. }
  408. // 拉取标段数据:建设项目、单项工程、单位工程数据
  409. let projectsData = await getProjectByGranularity(granularity, tenderID, userID);
  410. if (!projectsData) {
  411. throw '获取项目数据错误';
  412. }
  413. // 单项工程、单位工程按照树结构数据进行排序
  414. projectsData.children = sortByNext(projectsData.children);
  415. for (let engData of projectsData.children) {
  416. engData.children = sortByNext(engData.children);
  417. }
  418. }
  419. //转换基数表达式
  420. //1.有子项,则取固定清单对应基数
  421. //2.无子项,有基数,a.优先转换为行代号(不可自身) b.不能转换为行代号则找对应字典
  422. //3.基数中有无法转换的,设为金额
  423. function transformCalcBase(tenderDetail, node, {CalcBaseMap, FlagCalcBaseMap}) {
  424. let expr = node.data.calcBase || '';
  425. if (node.children.length) {
  426. let flag = node.getFlag();
  427. return FlagCalcBaseMap[flag] || '';
  428. }
  429. if (expr) {
  430. let illegal = false;
  431. let normalBase = _getNormalBase(expr),
  432. idBase = _getIDBase(expr);
  433. //普通基数转基数字典
  434. normalBase.forEach(base => {
  435. let replaceStr = CalcBaseMap[base];
  436. //转换成行代号的优先级比较高,进行清单匹配
  437. let flag = FlagCalcBaseMap[base];
  438. if (flag) {
  439. let flagNode = tenderDetail.mainTree.items.find(mNode => mNode.getFlag() === flag);
  440. //匹配到了 普通基数转换成行引用
  441. if (flagNode) {
  442. replaceStr = `F${flagNode.serialNo() + 1}`;
  443. }
  444. }
  445. //存在无法处理的基数
  446. if (!replaceStr) {
  447. illegal = true;
  448. return;
  449. }
  450. expr = expr.replace(new RegExp(base, 'g'), replaceStr);
  451. });
  452. //id引用转行代号引用
  453. idBase.forEach(base => {
  454. let id = base.match(/[^@]+/)[0];
  455. let theNode = tenderDetail.mainTree.getNodeByID(id),
  456. rowCode = theNode ? `F${theNode.serialNo() + 1}` : '';
  457. if (!rowCode) {
  458. illegal = true;
  459. return;
  460. }
  461. expr = expr.replace(new RegExp(base, 'g'), rowCode);
  462. });
  463. //不合法 返回金额
  464. if (illegal) {
  465. return getFee(node.data.fees, 'common.totalFee');
  466. }
  467. return expr;
  468. }
  469. //获取普通基数: {xxx}
  470. function _getNormalBase(str) {
  471. let reg = /{.+?}/g,
  472. matchs = str.match(reg);
  473. return matchs || [];
  474. }
  475. //获取id引用基数: @xxx-xxx-xx
  476. function _getIDBase(str) {
  477. let reg = /@.{36}/g,
  478. matchs = str.match(reg);
  479. return matchs || [];
  480. }
  481. }
  482. //转换基数说明,根据转换后的基数处理
  483. //1.行引用转换为对应行的名称
  484. //2.基数字典转换为中文
  485. function transformCalcBaseState(tenderDetail, expr, CalcStateMap) {
  486. if (!expr) {
  487. return '';
  488. }
  489. expr = String(expr);
  490. //提取基数
  491. let bases = expr.split(/[\+\-\*\/]/g);
  492. //提取操作符
  493. let oprs = expr.match(/[\+\-\*\/]/g);
  494. //转换后的基数
  495. let newBase = [];
  496. let illegal = false;
  497. for (let base of bases) {
  498. //行引用转换为名称.
  499. if (/F\d+/.test(base)) {
  500. let rowCode = base.match(/\d+/)[0],
  501. node = tenderDetail.mainTree.items[rowCode - 1];
  502. if (!node || !node.data.name) {
  503. illegal = true;
  504. break;
  505. }
  506. newBase.push(node && node.data.name ? node.data.name : '');
  507. } else if (CalcStateMap[base]){ //字典转换为中文
  508. newBase.push(CalcStateMap[base]);
  509. } else if (/^\d+(\.\d+)?$/.test(base)) { //金额
  510. newBase.push(base);
  511. } else {
  512. illegal = true;
  513. break;
  514. }
  515. }
  516. if (illegal) {
  517. return '';
  518. }
  519. let newExpr = '';
  520. for (let i = 0; i < newBase.length; i++) {
  521. newExpr += newBase[i];
  522. if (oprs && oprs[i]) {
  523. newExpr += oprs[i];
  524. }
  525. }
  526. return newExpr;
  527. }
  528. // 获取工程编号表格相关数据(导出需要弹出工程编号让用户选择)
  529. function getCodeSheetData(PMData) {
  530. let curCode = '0';
  531. let sheetData = [];
  532. sheetData.push(getObj(PMData));
  533. PMData.children.forEach(eng => {
  534. sheetData.push(getObj(eng));
  535. eng.children.forEach(tender => {
  536. sheetData.push(getObj(tender));
  537. });
  538. });
  539. //建设项目父ID设置为-1
  540. if (sheetData.length) {
  541. sheetData[0].ParentID = -1;
  542. sheetData[0].code = '';
  543. }
  544. return sheetData;
  545. function getObj(data) {
  546. return {
  547. collapsed: false,
  548. ID: data.ID,
  549. ParentID: data.ParentID,
  550. NextSiblingID: data.NextSiblingID,
  551. name: data.name,
  552. code: data.code || String(curCode++)
  553. };
  554. }
  555. }
  556. // 从srcEle节点中获取元素名为eleName的元素
  557. function _getElementFromSrc(srcEle, eleName) {
  558. if (!srcEle || !srcEle.children || !srcEle.children.length) {
  559. return [];
  560. }
  561. return srcEle.children.filter(ele => ele.name === eleName);
  562. }
  563. /*
  564. * 设置完工程编号后,更新原始数据的工程编号
  565. * 更新原始数据前需要将编号里的特殊字符进行转换
  566. * @param {Array}exportData 提取出来的需要导出的数据
  567. * {Array}codes 工程编号表中填写的工程编号
  568. * {String}EngineeringName 单项工程元素的名称
  569. * {String}tenderName 单位工程元素的名称
  570. * {String}codeName 编号属性的名称
  571. * @return {void}
  572. * */
  573. function setupCode(exportData, codes, EngineeringName, tenderName, codeName) {
  574. // 转换xml实体字符
  575. let parsedCodes = getParsedData(codes);
  576. // 给导出数据里的单项工程、单位工程填上用户设置的工程编号
  577. exportData.forEach(orgData => {
  578. let curIdx = 0;
  579. let engs = _getElementFromSrc(orgData.data, EngineeringName);
  580. engs.forEach(eng => {
  581. eng.attrs.find(attr => attr.name === codeName).value = parsedCodes[curIdx++];
  582. let tenders = _getElementFromSrc(eng, tenderName);
  583. tenders.forEach(tender => {
  584. tender.attrs.find(attr => attr.name === codeName).value = parsedCodes[curIdx++];
  585. });
  586. });
  587. });
  588. }
  589. const UTIL = Object.freeze({
  590. isDef,
  591. hasValue,
  592. setTimeoutSync,
  593. getFee,
  594. getValueByKey,
  595. arrayToObj,
  596. validDepth,
  597. checkUnique,
  598. sortByNext,
  599. getTenderDetail,
  600. getProjectByGranularity,
  601. transformCalcBase,
  602. transformCalcBaseState,
  603. getCodeSheetData,
  604. setupCode
  605. });
  606. // 开始标签
  607. function _startTag(ele) {
  608. let rst = `<${ele.name}`;
  609. for (let attr of ele.attrs) {
  610. rst += ` ${attr.name}="${attr.value}"`;
  611. }
  612. rst += ele.children.length > 0 ? '>' : '/>';
  613. return rst;
  614. }
  615. // 结束标签
  616. function _endTag(ele) {
  617. return `</${ele.name}>`;
  618. }
  619. // 拼接成xml字符串
  620. function _toXMLStr(eles) {
  621. let rst = '';
  622. for (let ele of eles) {
  623. rst += _startTag(ele);
  624. if (ele.children.length > 0) {
  625. rst += _toXMLStr(ele.children);
  626. rst += _endTag(ele);
  627. }
  628. }
  629. return rst;
  630. }
  631. //格式化xml字符串
  632. function _formatXml(text) {
  633. //去掉多余的空格
  634. text = '\n' + text.replace(/>\s*?</g, ">\n<");
  635. //调整格式
  636. let rgx = /\n(<(([^\?]).+?)(?:\s|\s*?>|\s*?(\/)>)(?:.*?(?:(?:(\/)>)|(?:<(\/)\2>)))?)/mg;
  637. let nodeStack = [];
  638. let output = text.replace(rgx, function($0, all, name, isBegin, isCloseFull1, isCloseFull2, isFull1, isFull2){
  639. let isClosed = (isCloseFull1 === '/') || (isCloseFull2 === '/' ) || (isFull1 === '/') || (isFull2 === '/');
  640. let prefix = '';
  641. if (isBegin === '!') {
  642. prefix = getPrefix(nodeStack.length);
  643. } else {
  644. if (isBegin !== '/') {
  645. prefix = getPrefix(nodeStack.length);
  646. if (!isClosed) {
  647. nodeStack.push(name);
  648. }
  649. } else {
  650. nodeStack.pop();
  651. prefix = getPrefix(nodeStack.length);
  652. }
  653. }
  654. let ret = '\n' + prefix + all;
  655. return ret;
  656. });
  657. let outputText = output.substring(1);
  658. return outputText;
  659. function getPrefix(prefixIndex) {
  660. let span = ' ';
  661. let output = [];
  662. for (let i = 0 ; i < prefixIndex; ++i) {
  663. output.push(span);
  664. }
  665. return output.join('');
  666. }
  667. }
  668. // 将数据转换为xml文件并导出
  669. async function exportFile(originalDatas) {
  670. let fileDatas = [];
  671. //源数据转换成blob
  672. for (let orgData of originalDatas) {
  673. //转换成xml字符串
  674. let xmlStr = _toXMLStr([orgData.data]);
  675. //加上xml声明
  676. xmlStr = `<?xml version="1.0" encoding="utf-8"?>${xmlStr}`;
  677. //格式化
  678. xmlStr = _formatXml(xmlStr);
  679. let blob = new Blob([xmlStr], {type: 'text/plain;charset=utf-8'});
  680. fileDatas.push({blob: blob, fileName: orgData.fileName});
  681. }
  682. //导出文件
  683. if (fileDatas.length === 1) {
  684. saveAs(fileDatas[0].blob, fileDatas[0].fileName);
  685. } else if (fileDatas.length > 1) { //导出压缩包
  686. let zip = new JSZip();
  687. for (let file of fileDatas) {
  688. zip.file(file.fileName, file.blob, {binary: true});
  689. }
  690. let zipFile = await zip.generateAsync({type: 'blob'});
  691. saveAs(zipFile, '重庆标准交换数据.zip');
  692. }
  693. }
  694. return {
  695. CONFIG,
  696. CACHE,
  697. UTIL,
  698. Element,
  699. exportFile
  700. };
  701. })();