From 8ffd341443c32e3f339af6958be6025a8b0851af Mon Sep 17 00:00:00 2001 From: hb0730 <1278032416@qq.com> Date: Sat, 30 Oct 2021 13:56:42 +0800 Subject: [PATCH] =?UTF-8?q?fix(axios):=20=E4=BF=AE=E5=A4=8D=E5=B0=81?= =?UTF-8?q?=E8=A3=85=E7=9A=84axios=E8=AF=B7=E6=B1=82=E8=BF=87=E5=BF=ABbefo?= =?UTF-8?q?reRequestCallback,beforeResponseCallback=E5=8F=AA=E8=A7=A6?= =?UTF-8?q?=E5=8F=91=E4=B8=80=E6=AC=A1=20(#84)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/http/core.ts | 27 ++++++++------------------- 1 file changed, 8 insertions(+), 19 deletions(-) diff --git a/src/utils/http/core.ts b/src/utils/http/core.ts index 3d3413767..623f1e990 100644 --- a/src/utils/http/core.ts +++ b/src/utils/http/core.ts @@ -41,12 +41,6 @@ class EnclosureHttp { // 记录当前这一次cancelToken的key private currentCancelTokenKey = ""; - private beforeRequestCallback: EnclosureHttpRequestConfig["beforeRequestCallback"] = - undefined; - - private beforeResponseCallback: EnclosureHttpRequestConfig["beforeResponseCallback"] = - undefined; - public get cancelTokenList(): Array { return this.sourceTokenList; } @@ -126,8 +120,8 @@ class EnclosureHttp { this.cancelRepeatRequest(); this.currentCancelTokenKey = cancelKey; // 优先判断post/get等方法是否传入回掉,否则执行初始化设置等回掉 - if (typeof this.beforeRequestCallback === "function") { - this.beforeRequestCallback($config); + if (typeof config.beforeRequestCallback === "function") { + config.beforeRequestCallback($config); return $config; } if (EnclosureHttp.initConfig.beforeRequestCallback) { @@ -158,20 +152,21 @@ class EnclosureHttp { const instance = EnclosureHttp.axiosInstance; instance.interceptors.response.use( (response: EnclosureHttpResoponse) => { + const $config = response.config; // 请求每次成功一次就删除当前canceltoken标记 - const cancelKey = EnclosureHttp.genUniqueKey(response.config); + const cancelKey = EnclosureHttp.genUniqueKey($config); this.deleteCancelTokenByCancelKey(cancelKey); + + NProgress.done(); // 优先判断post/get等方法是否传入回掉,否则执行初始化设置等回掉 - if (typeof this.beforeResponseCallback === "function") { - this.beforeResponseCallback(response); - this.beforeResponseCallback = undefined; + if (typeof $config.beforeResponseCallback === "function") { + $config.beforeResponseCallback(response); return response.data; } if (EnclosureHttp.initConfig.beforeResponseCallback) { EnclosureHttp.initConfig.beforeResponseCallback(response); return response.data; } - NProgress.done(); return response.data; }, (error: EnclosureHttpError) => { @@ -209,12 +204,6 @@ class EnclosureHttp { ...axiosConfig } as EnclosureHttpRequestConfig); // 单独处理自定义请求/响应回掉 - if (axiosConfig?.beforeRequestCallback) { - this.beforeRequestCallback = axiosConfig.beforeRequestCallback; - } - if (axiosConfig?.beforeResponseCallback) { - this.beforeResponseCallback = axiosConfig.beforeResponseCallback; - } return new Promise((resolve, reject) => { EnclosureHttp.axiosInstance .request(config)