view.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 importData = await INTERFACE_EXPORT_BASE.extractImportData(INTERFACE_IMPORT.entry, file, areaKey);
  58. $('#interface-import-modal').modal('hide');
  59. $.bootstrapLoading.progressStart('导入文件', true);
  60. $("#progress_modal_body").text('正在导入接口文件,请稍候……');
  61. //return;
  62. // 转换成File实例
  63. const blob = new Blob([JSON.stringify(importData)], { type: 'text/plain;charset=utf-8' });
  64. const key = `${uuid.v1()}.json`;
  65. const uploadFile = new File([blob], key);
  66. // 上传文件
  67. await projTreeObj.getUploadToken();
  68. await UPLOAD_CDN.uploadSync(uploadFile, key, projTreeObj.uptoken);
  69. // 下载并处理文件
  70. await ajaxPost('/pm/import/importInterface', { key });
  71. await importProcessChecking(key, null, (projectData) => handleProjectAfterChecking(projectData));
  72. } catch (err) {
  73. console.log(err);
  74. alert(err);
  75. } finally {
  76. projTreeObj.uptoken = null;
  77. setTimeout(function () {
  78. STATE.importing = false;
  79. }, 500);
  80. }
  81. });
  82. }
  83. return {
  84. importListener,
  85. initFileAccept,
  86. };
  87. })();
  88. $(document).ready(() => {
  89. $('#interface-import-modal').on('show.bs.modal', () => {
  90. $('#interface-import-file').val('');
  91. $('#interface-import-label').text('请选择导入文件');
  92. IMPORT_VIEW.initFileAccept($('#interface-import-file'));
  93. STD_INTERFACE.initInterfaceAreas($('#import-parent-area'), $('#import-sub-area'));
  94. });
  95. IMPORT_VIEW.importListener();
  96. });