From b8de14893492a5be806d885a91fd1769e3f8423d Mon Sep 17 00:00:00 2001 From: qlin Date: Thu, 13 Jan 2022 16:25:16 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=92=88=E5=AF=B9=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E7=9A=84=E6=83=85=E5=86=B5=E8=BF=9B=E8=A1=8C?= =?UTF-8?q?=E4=BC=98=E5=8C=96=20(#91)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/template/resDataAdaptor.js | 9 +++++++++ packages/fes-template-h5/src/pages/index.vue | 13 ++++++++----- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/packages/fes-plugin-request/src/template/resDataAdaptor.js b/packages/fes-plugin-request/src/template/resDataAdaptor.js index 03ffdb68..a045f62a 100644 --- a/packages/fes-plugin-request/src/template/resDataAdaptor.js +++ b/packages/fes-plugin-request/src/template/resDataAdaptor.js @@ -1,6 +1,15 @@ import { isFunction, isObject, isString } from './helpers'; export default async ({ response, responseDataAdaptor }, next) => { + // 如果 data 是 blob 并且 content-type 是 application/json,自动进行数据处理 + if (response.data instanceof Blob && response.headers['content-type'].startsWith('application/json') && response.data.type === 'application/json') { + const rawData = response.data; + try { + response.data = JSON.parse(await response.data.text()); + } catch { + response.data = rawData; + } + } if (isFunction(responseDataAdaptor) && response && (isObject(response.data) || isString(response.data))) { response.data = responseDataAdaptor(response.data); } diff --git a/packages/fes-template-h5/src/pages/index.vue b/packages/fes-template-h5/src/pages/index.vue index 85d16f8d..88138fc6 100644 --- a/packages/fes-template-h5/src/pages/index.vue +++ b/packages/fes-template-h5/src/pages/index.vue @@ -65,16 +65,19 @@ export default { }; const post = (id) => { - request('/get/api', { id }, { + request('/api', { id }, { + responseType: 'blob' + }).then((data) => { + console.log(data); }); }; get(1); - get(2); - get(3); + // get(2); + // get(3); - post(1); - post(2); + // post(1); + // post(2); post(3);