view.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * @Descripttion: 导入接口视图相关
  3. * @Author: vian
  4. * @Date: 2020-09-08 09:36:52
  5. */
  6. const IMPORT_VIEW = (() => {
  7. const {
  8. EXPORT_KIND: { BID_INVITATION }
  9. } = window.commonConstants;
  10. /**
  11. * 根据接口配置文件,设置可被接受的导入文件类型,只导入招标文件
  12. * @param {Object} $file - 文件选择Jquery dom
  13. * @return {Void}
  14. */
  15. function initFileAccept($file) {
  16. const set = new Set();
  17. Object
  18. .values(INTERFACE_CONFIG)
  19. .forEach(config => {
  20. Object
  21. .entries(config.fileSuffix)
  22. .forEach(([type, suffix]) => {
  23. if (+type === BID_INVITATION) {
  24. set.add(suffix);
  25. }
  26. });
  27. });
  28. const accept = [...set].join(',');
  29. $file.prop('accept', accept);
  30. }
  31. // 导入相关事件监听器
  32. let importData = null;
  33. let areaKey = '';
  34. const onlyImportMatchBillsCompilations = ['广东@中山', '浙江@杭州'];
  35. function importListener() {
  36. // 文件选择变更
  37. $('#interface-import-file').change(function () {
  38. importData = null;
  39. areaKey = '';
  40. $('#import-rename-input').val('');
  41. $('#import-rename').hide();
  42. const file = $(this)[0].files[0];
  43. $('#interface-import-label').text(file && file.name || '请选择导入文件');
  44. });
  45. // 导入确认事件
  46. $('#interface-import-confirm').click(async function () {
  47. try {
  48. if (STATE.importing) {
  49. return;
  50. }
  51. STATE.importing = true;
  52. const file = $('#interface-import-file')[0].files[0];
  53. if (!file) {
  54. throw '请选择导入文件。';
  55. }
  56. // 按照地区动态加载导入脚本
  57. const parentArea = $('#import-parent-area').val();
  58. const subArea = $('#import-sub-area').val();
  59. if (!parentArea || !subArea) {
  60. throw '请选择有效地区。';
  61. }
  62. const curAreaKey = `${parentArea}@${subArea}`;
  63. if (!(importData && areaKey && areaKey === curAreaKey)) {
  64. areaKey = curAreaKey;
  65. await STD_INTERFACE.loadScriptByArea(areaKey, STD_INTERFACE.ScriptType.IMPORT);
  66. // const onlyImportMatchBills = areaKey === '广东@中山';
  67. const onlyImportMatchBills = onlyImportMatchBillsCompilations.includes(areaKey);
  68. importData = await INTERFACE_EXPORT_BASE.extractImportData(INTERFACE_IMPORT.entry, file, areaKey, false, onlyImportMatchBills, INTERFACE_IMPORT.handleAfterImport);
  69. }
  70. const constructionName = $('#import-rename').is(':visible') ? $('#import-rename-input').val() : importData.name;
  71. // 确定建设项目的名称(不允许重复)
  72. const sameDepthProjs = getProjs(projTreeObj.tree.selected);
  73. const matchedProject = sameDepthProjs.find(node => node.data.name === constructionName);
  74. if (matchedProject || !constructionName) {
  75. $('#import-rename-input').val(constructionName);
  76. $('#import-rename').show();
  77. $('#import-rename-input').focus();
  78. return;
  79. }
  80. importData.name = constructionName;
  81. $('#interface-import-modal').modal('hide');
  82. $.bootstrapLoading.progressStart('导入文件', true);
  83. $("#progress_modal_body").text('正在导入接口文件,请稍候……');
  84. //return;
  85. // 转换成File实例
  86. const blob = new Blob([JSON.stringify(importData)], { type: 'text/plain;charset=utf-8' });
  87. const key = `${uuid.v1()}.json`;
  88. const uploadFile = new File([blob], key);
  89. // 上传文件
  90. await projTreeObj.getUploadToken();
  91. await UPLOAD_CDN.uploadSync(uploadFile, key, projTreeObj.uptoken);
  92. // 下载并处理文件
  93. await ajaxPost('/pm/import/importInterface', { key });
  94. await importProcessChecking(key, null, (projectData) => handleProjectAfterChecking(projectData));
  95. } catch (err) {
  96. console.log(err);
  97. alert(err);
  98. } finally {
  99. projTreeObj.uptoken = null;
  100. setTimeout(function () {
  101. STATE.importing = false;
  102. }, 500);
  103. }
  104. });
  105. }
  106. return {
  107. importListener,
  108. initFileAccept,
  109. };
  110. })();
  111. $(document).ready(() => {
  112. $('#interface-import-modal').on('show.bs.modal', () => {
  113. $('#import-rename-input').val('');
  114. $('#import-rename').hide();
  115. $('#interface-import-file').val('');
  116. $('#interface-import-label').text('请选择导入文件');
  117. IMPORT_VIEW.initFileAccept($('#interface-import-file'));
  118. STD_INTERFACE.initInterfaceAreas($('#import-parent-area'), $('#import-sub-area'));
  119. });
  120. IMPORT_VIEW.importListener();
  121. });