view.js 2.9 KB

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