view.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /**
  2. *
  3. *
  4. * @author Zhong
  5. * @date 2019/6/5
  6. * @version
  7. */
  8. // 导出接口相关
  9. const EXPORT_VIEW = (() => {
  10. 'use strict';
  11. const _base = INTERFACE_EXPORT_BASE;
  12. const _util = _base.UTIL;
  13. const _cache = _base.CACHE;
  14. // 导出数据缓存,为了自检完后,再导出的时候不需要重新运行相关提取程序。(暂时取消了自检,但还是留着这个缓存机制)
  15. let _exportCache = [];
  16. // 操作状态
  17. const STATE = {
  18. checking: false, // 自检
  19. exporting: false, // 导出
  20. };
  21. // 回到初始状态,需要清空cache中的数据
  22. function resetState() {
  23. _exportCache = [];
  24. _cache.clear();
  25. }
  26. // 设置提示高度样式
  27. function setHeightByInfo(infos) {
  28. if (infos.length > 20) {
  29. $('#hintBox_caption').addClass('export-check');
  30. }
  31. }
  32. //事件监听
  33. function exportListener() {
  34. // 导出接口
  35. $('#interface-export-confirm').click(async function () {
  36. const pr = new SCComponent.InitProgressBar();
  37. try {
  38. const checkedDatas = $('#interface-export-modal input[type="checkbox"]:checked');
  39. if (!checkedDatas.length) {
  40. throw '请勾选导出文件。';
  41. }
  42. const parentArea = $('#export-parent-area').val();
  43. const subArea = $('#export-sub-area').val();
  44. if (!parentArea || !subArea) {
  45. throw '请选择有效地区。';
  46. }
  47. // 按照地区动态加载导出脚本
  48. const areaKey = `${parentArea}@${subArea}`;
  49. await STD_INTERFACE.loadScriptByArea(areaKey, STD_INTERFACE.ScriptType.EXPORT);
  50. if (STATE.exporting) {
  51. return;
  52. }
  53. STATE.exporting = true;
  54. if (!_exportCache || !_exportCache.length) {
  55. pr.start('导出数据接口', '正在导出文件,请稍候……');
  56. for (const checkedData of checkedDatas) {
  57. const boqType = parseInt($(checkedData).val());
  58. const requestForSummaryInfo = INTERFACE_EXPORT.requestForSummaryInfo || null;
  59. const projectID = projectObj.project.ID();
  60. const exportData = await _base.extractExportData(INTERFACE_EXPORT.entry, requestForSummaryInfo, boqType, areaKey, projectID, userID);
  61. const failList = _util.transformFailList(_cache.getItem('failList'));
  62. if (failList.length) {
  63. // 自检提示错误,打断导出
  64. setHeightByInfo(failList);
  65. throw failList.join('<br/>');
  66. }
  67. _exportCache.push(...exportData);
  68. }
  69. }
  70. if (_exportCache && _exportCache.length) {
  71. // 导出文件
  72. await _base.exportFile(_exportCache, INTERFACE_EXPORT.saveAsFile || INTERFACE_EXPORT_BASE.defaultSaveAs);
  73. }
  74. $('#interface-export-modal').modal('hide');
  75. } catch (err) {
  76. console.log(err);
  77. alert(err);
  78. } finally {
  79. _cache.setItem('failList', []);
  80. pr.end();
  81. setTimeout(() => {
  82. STATE.exporting = false;
  83. }, 300);
  84. }
  85. });
  86. //导出窗口--------
  87. $('#interface-export-modal').on('hide.bs.modal', function () {
  88. resetState();
  89. STATE.checking = false;
  90. STATE.exporting = false;
  91. $('#interface-export-modal input[type="checkbox"]:eq(0)').prop('checked', true);
  92. });
  93. $('#interface-export-modal input[type="checkbox"]').click(function () {
  94. resetState();
  95. });
  96. }
  97. return { exportListener, resetState }
  98. })();
  99. $(document).ready(() => {
  100. $('#interface-export-modal').on('show.bs.modal', () => STD_INTERFACE.initInterfaceAreas($('#export-parent-area'), $('#export-sub-area')));
  101. EXPORT_VIEW.exportListener();
  102. })