From 0b2c28f47f414348109da47b67d457f97b186f12 Mon Sep 17 00:00:00 2001 From: luofann Date: Tue, 10 Sep 2024 21:33:06 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=85=AC=E5=85=B1API=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=AF=BC=E5=87=BA=E5=AE=9E=E7=8E=B0=E6=96=B9=E5=BC=8F?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=20--bug=3D129838159=20#=20Reviewed,=20transa?= =?UTF-8?q?ction=20id:=2018032?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/pc/src/store/modules/apiRemote.js | 11 +++++++ frontend/pc/src/utils/ajax.js | 2 +- frontend/pc/src/utils/util.js | 31 +++++++++++++++++++ .../apiConfigure/apiTable.vue | 2 +- 4 files changed, 44 insertions(+), 2 deletions(-) diff --git a/frontend/pc/src/store/modules/apiRemote.js b/frontend/pc/src/store/modules/apiRemote.js index 8d9f425ce..b2f5032c3 100644 --- a/frontend/pc/src/store/modules/apiRemote.js +++ b/frontend/pc/src/store/modules/apiRemote.js @@ -21,6 +21,7 @@ */ import ajax from "../../utils/ajax"; +import { fileDownload } from "../../utils/util"; export default { namespaced: true, @@ -124,6 +125,16 @@ export default { return res; }); }, + export_api({}, id) { + return ajax.get(`postman/remote_api/${id}/exports/`, { + transitional: { + forcedJSONParsing: false, + }, + responseType: 'blob' + }).then((response) => { + fileDownload(response); + }) + }, // http://dev.paas-poc.o.qcloud.com:8000/api/postman/remote_api/1/run_api/ run_remote_api({ commit, state, dispatch }, params) { return ajax.post(`postman/remote_api/${params.id}/run_api/`, params).then((response) => { diff --git a/frontend/pc/src/utils/ajax.js b/frontend/pc/src/utils/ajax.js index 61bb49200..9f0e886dd 100644 --- a/frontend/pc/src/utils/ajax.js +++ b/frontend/pc/src/utils/ajax.js @@ -166,7 +166,7 @@ instance.interceptors.response.use( }; } - if (response.request.responseURL.includes('/api/plugin_service/')) { + if (response.request.responseURL.includes('/api/plugin_service/') || response.headers['content-type'] === 'application/octet-stream; charset=utf-8') { return response; } diff --git a/frontend/pc/src/utils/util.js b/frontend/pc/src/utils/util.js index 2c3a4cecb..4a1f9ae84 100644 --- a/frontend/pc/src/utils/util.js +++ b/frontend/pc/src/utils/util.js @@ -466,3 +466,34 @@ export function transCustomFormToTable (formStr) { return tables; } + + +export function fileDownload (res) { + let filename + const disposition = res.headers['content-disposition'].split(',') + const filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/ + const matches = filenameRegex.exec(disposition) + if (matches !== null && matches[1]) { + filename = decodeURIComponent(matches[1].replace(/(^utf-8)|['"]/g, '')) + } + const blob = new Blob([res.data], { type: 'text/plain;charset=UTF-8' }) + if (typeof window.navigator.msSaveBlob !== 'undefined') { + // hack old IE + window.navigator.msSaveBlob(blob, filename) + } else { + const eleLink = document.createElement('a') + const blobURL = window.URL.createObjectURL(blob) + eleLink.style.display = 'none' + eleLink.href = blobURL + eleLink.setAttribute('download', filename) + + // hack HTML5 download attribute + if (typeof eleLink.download === 'undefined') { + eleLink.setAttribute('target', '_blank') + } + document.body.appendChild(eleLink) + eleLink.click() + document.body.removeChild(eleLink) + window.URL.revokeObjectURL(blobURL) + } + }; diff --git a/frontend/pc/src/views/processManagement/apiConfigure/apiTable.vue b/frontend/pc/src/views/processManagement/apiConfigure/apiTable.vue index f3c1cd26d..399cf8760 100644 --- a/frontend/pc/src/views/processManagement/apiConfigure/apiTable.vue +++ b/frontend/pc/src/views/processManagement/apiConfigure/apiTable.vue @@ -526,7 +526,7 @@ } }, exportFlow(item) { - window.open(`${window.SITE_URL}api/postman/remote_api/${item.id}/exports/`); + this.$store.dispatch('apiRemote/export_api', item.id); }, async handleBeforeClose() { if (this.isFormChanged) {