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);