view.js 3.4 KB

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