From 51c83c2df270645ce7d9444dc042338a0571c7c7 Mon Sep 17 00:00:00 2001 From: winixt Date: Thu, 29 Jul 2021 11:24:44 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=20dataField=20?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/template/preventRepeatReq.js | 3 +- .../src/template/request.js | 3 +- .../src/template/setDataField.js | 4 +-- packages/fes-template-h5/src/app.js | 3 +- packages/fes-template-h5/src/pages/index.vue | 35 ++++++++++--------- 5 files changed, 26 insertions(+), 22 deletions(-) diff --git a/packages/fes-plugin-request/src/template/preventRepeatReq.js b/packages/fes-plugin-request/src/template/preventRepeatReq.js index 4c5ee34f..562ff7ac 100644 --- a/packages/fes-plugin-request/src/template/preventRepeatReq.js +++ b/packages/fes-plugin-request/src/template/preventRepeatReq.js @@ -46,7 +46,8 @@ export default async (ctx, next) => { if (requestMap.get(ctx.key) && !ctx.config.mergeRequest) { ctx.error = { type: 'REPEAT', - msg: '重复请求' + msg: '重复请求', + config: ctx.config }; return; } diff --git a/packages/fes-plugin-request/src/template/request.js b/packages/fes-plugin-request/src/template/request.js index c2a819bf..239b2403 100644 --- a/packages/fes-plugin-request/src/template/request.js +++ b/packages/fes-plugin-request/src/template/request.js @@ -124,7 +124,6 @@ function createContext(userConfig) { function getResponseCode(response) { if (response) { - if (response._rawData) return response._rawData.code; if (response.data) return response.data.code; } return null; @@ -181,7 +180,7 @@ export const request = (url, data, options = {}) => { return currentRequestInstance.request(context).then(async () => { if (!context.error) { - return context.config.useResonse ? context.response : context.response.data; + return context.config.useResonse ? context.response : context.filterData || context.response.data; } await handleRequestError(context); return Promise.reject(context.error); diff --git a/packages/fes-plugin-request/src/template/setDataField.js b/packages/fes-plugin-request/src/template/setDataField.js index 33ea2204..d2c3e821 100644 --- a/packages/fes-plugin-request/src/template/setDataField.js +++ b/packages/fes-plugin-request/src/template/setDataField.js @@ -1,10 +1,10 @@ import { isObject } from './helpers'; +// FEATURE: 后续支持 a.b.c export default async (ctx, next) => { const dataField = ctx.config.dataField ?? ctx.dataField; if (ctx.response && isObject(ctx.response.data) && dataField) { - ctx.response._rawData = ctx.response.data; - ctx.response.data = ctx.response.data[dataField]; + ctx.filterData = ctx.response.data[dataField]; } await next(); }; diff --git a/packages/fes-template-h5/src/app.js b/packages/fes-template-h5/src/app.js index 8d70d71f..0797ca30 100644 --- a/packages/fes-template-h5/src/app.js +++ b/packages/fes-template-h5/src/app.js @@ -7,8 +7,9 @@ export const request = { console.log('500 error'); }, default(error) { - console.log('default error'); console.log(error); + const msg = error?.data?.msg || error?.msg; + console.log(msg); } } }; diff --git a/packages/fes-template-h5/src/pages/index.vue b/packages/fes-template-h5/src/pages/index.vue index 0b72617b..ec4ffab3 100644 --- a/packages/fes-template-h5/src/pages/index.vue +++ b/packages/fes-template-h5/src/pages/index.vue @@ -23,10 +23,13 @@ export default { console.log('click Icon'); }; // request('/api', null, { - // mergeRequest: true // }).then((res) => { // console.log(res); // }); + request('/api', null, { + }).then((res) => { + console.log(res); + }); // request('/api', null, { // mergeRequest: true // }).then((res) => { @@ -68,21 +71,21 @@ export default { // }); // }, 3200); - request('/api', null, { - cache: true - }).then((res) => { - console.log(res); - }); - request('/api', null, { - cache: true - }).then((res) => { - console.log(res); - }); - request('/api', null, { - cache: true - }).then((res) => { - console.log(res); - }); + // request('/api', null, { + // cache: true + // }).then((res) => { + // console.log(res); + // }); + // request('/api', null, { + // cache: true + // }).then((res) => { + // console.log(res); + // }); + // request('/api', null, { + // cache: true + // }).then((res) => { + // console.log(res); + // }); // request('/api', null, { // // skipErrorHandler: [500]