view.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * @Descripttion: 导入接口视图相关
  3. * @Author: vian
  4. * @Date: 2020-09-08 09:36:52
  5. */
  6. const IMPORT_VIEW = (() => {
  7. /**
  8. * 根据接口配置文件,设置可被接受的导入文件类型
  9. * @param {Object} $file - 文件选择Jquery dom
  10. * @return {Void}
  11. */
  12. function initFileAccept($file) {
  13. const set = new Set();
  14. Object
  15. .values(INTERFACE_CONFIG)
  16. .forEach(config => {
  17. Object
  18. .values(config.fileSuffix)
  19. .forEach(suffix => set.add(suffix));
  20. });
  21. const accept = [...set].join(',');
  22. $file.prop('accept', accept);
  23. }
  24. // 导入相关事件监听器
  25. function importListener() {
  26. // 文件选择变更
  27. $('#interface-import-file').change(function () {
  28. const file = $(this)[0].files[0];
  29. $('#interface-import-label').text(file && file.name || '请选择导入文件');
  30. });
  31. // 导入确认事件
  32. $('#interface-import-confirm').click(async function () {
  33. try {
  34. if (STATE.importing) {
  35. return;
  36. }
  37. STATE.importing = true;
  38. const file = $('#interface-import-file')[0].files[0];
  39. if (!file) {
  40. throw '请选择导入文件。';
  41. }
  42. // 按照地区动态加载导入脚本
  43. const parentArea = $('#import-parent-area').val();
  44. const subArea = $('#import-sub-area').val();
  45. if (!parentArea || !subArea) {
  46. throw '请选择有效地区。';
  47. }
  48. const areaKey = `${parentArea}@${subArea}`;
  49. await STD_INTERFACE.loadScriptByArea(areaKey, STD_INTERFACE.ScriptType.IMPORT);
  50. const importData = await INTERFACE_EXPORT_BASE.extractImportData(INTERFACE_IMPORT.entry, file, areaKey);
  51. $('#interface-import-modal').modal('hide');
  52. $.bootstrapLoading.progressStart('导入文件', true);
  53. $("#progress_modal_body").text('正在导入接口文件,请稍候……');
  54. //return;
  55. // 转换成File实例
  56. const blob = new Blob([JSON.stringify(importData)], { type: 'text/plain;charset=utf-8' });
  57. const key = `${uuid.v1()}.json`;
  58. const uploadFile = new File([blob], key);
  59. // 上传文件
  60. await projTreeObj.getUploadToken();
  61. await UPLOAD_CDN.uploadSync(uploadFile, key, projTreeObj.uptoken);
  62. // 下载并处理文件
  63. await ajaxPost('/pm/import/importInterface', { key });
  64. await importProcessChecking(key, null, (projectData) => handleProjectAfterChecking(projectData));
  65. } catch (err) {
  66. console.log(err);
  67. alert(err);
  68. } finally {
  69. projTreeObj.uptoken = null;
  70. setTimeout(function () {
  71. STATE.importing = false;
  72. }, 500);
  73. }
  74. });
  75. }
  76. return {
  77. importListener,
  78. initFileAccept,
  79. };
  80. })();
  81. $(document).ready(() => {
  82. $('#interface-import-modal').on('show.bs.modal', () => {
  83. $('#interface-import-file').val('');
  84. $('#interface-import-label').text('请选择导入文件');
  85. IMPORT_VIEW.initFileAccept($('#interface-import-file'));
  86. STD_INTERFACE.initInterfaceAreas($('#import-parent-area'), $('#import-sub-area'));
  87. });
  88. IMPORT_VIEW.importListener();
  89. });