view.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. function importListener() {
  35. // 文件选择变更
  36. $('#interface-import-file').change(function () {
  37. importData = null;
  38. areaKey = '';
  39. $('#import-rename-input').val('');
  40. $('#import-rename').hide();
  41. const file = $(this)[0].files[0];
  42. $('#interface-import-label').text(file && file.name || '请选择导入文件');
  43. });
  44. // 导入确认事件
  45. $('#interface-import-confirm').click(async function () {
  46. try {
  47. if (STATE.importing) {
  48. return;
  49. }
  50. STATE.importing = true;
  51. const file = $('#interface-import-file')[0].files[0];
  52. if (!file) {
  53. throw '请选择导入文件。';
  54. }
  55. // 按照地区动态加载导入脚本
  56. const parentArea = $('#import-parent-area').val();
  57. const subArea = $('#import-sub-area').val();
  58. if (!parentArea || !subArea) {
  59. throw '请选择有效地区。';
  60. }
  61. const curAreaKey = `${parentArea}@${subArea}`;
  62. if (!(importData && areaKey && areaKey === curAreaKey)) {
  63. areaKey = curAreaKey;
  64. await STD_INTERFACE.loadScriptByArea(areaKey, STD_INTERFACE.ScriptType.IMPORT);
  65. const onlyImportMatchBills = areaKey === '广东@中山';
  66. importData = await INTERFACE_EXPORT_BASE.extractImportData(INTERFACE_IMPORT.entry, file, areaKey, false, onlyImportMatchBills);
  67. }
  68. const constructionName = $('#import-rename').is(':visible') ? $('#import-rename-input').val() : importData.name;
  69. // 确定建设项目的名称(不允许重复)
  70. const sameDepthProjs = getProjs(projTreeObj.tree.selected);
  71. const matchedProject = sameDepthProjs.find(node => node.data.name === constructionName);
  72. if (matchedProject || !constructionName) {
  73. $('#import-rename-input').val(constructionName);
  74. $('#import-rename').show();
  75. $('#import-rename-input').focus();
  76. return;
  77. }
  78. importData.name = constructionName;
  79. $('#interface-import-modal').modal('hide');
  80. $.bootstrapLoading.progressStart('导入文件', true);
  81. $("#progress_modal_body").text('正在导入接口文件,请稍候……');
  82. //return;
  83. // 转换成File实例
  84. const blob = new Blob([JSON.stringify(importData)], { type: 'text/plain;charset=utf-8' });
  85. const key = `${uuid.v1()}.json`;
  86. const uploadFile = new File([blob], key);
  87. // 上传文件
  88. await projTreeObj.getUploadToken();
  89. await UPLOAD_CDN.uploadSync(uploadFile, key, projTreeObj.uptoken);
  90. // 下载并处理文件
  91. await ajaxPost('/pm/import/importInterface', { key });
  92. await importProcessChecking(key, null, (projectData) => handleProjectAfterChecking(projectData));
  93. } catch (err) {
  94. console.log(err);
  95. alert(err);
  96. } finally {
  97. projTreeObj.uptoken = null;
  98. setTimeout(function () {
  99. STATE.importing = false;
  100. }, 500);
  101. }
  102. });
  103. }
  104. return {
  105. importListener,
  106. initFileAccept,
  107. };
  108. })();
  109. $(document).ready(() => {
  110. $('#interface-import-modal').on('show.bs.modal', () => {
  111. $('#import-rename-input').val('');
  112. $('#import-rename').hide();
  113. $('#interface-import-file').val('');
  114. $('#interface-import-label').text('请选择导入文件');
  115. IMPORT_VIEW.initFileAccept($('#interface-import-file'));
  116. STD_INTERFACE.initInterfaceAreas($('#import-parent-area'), $('#import-sub-area'));
  117. });
  118. IMPORT_VIEW.importListener();
  119. });