From 5c3dd39683a86163d49f1c46e3e8d66065176201 Mon Sep 17 00:00:00 2001 From: liujiayii Date: Wed, 15 Oct 2025 16:12:50 +0800 Subject: [PATCH] =?UTF-8?q?feat(intercrptors):=20=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E4=BA=86queue=E5=92=8CwrapperHook=E7=9A=84=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E5=80=BC=E4=BB=A5=E6=94=AF=E6=8C=81=E5=BC=82=E6=AD=A5resolve(f?= =?UTF-8?q?alse))?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/uni-api/src/helpers/interceptor.ts | 31 ++++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/packages/uni-api/src/helpers/interceptor.ts b/packages/uni-api/src/helpers/interceptor.ts index d31543921a7..71ece5b53b9 100644 --- a/packages/uni-api/src/helpers/interceptor.ts +++ b/packages/uni-api/src/helpers/interceptor.ts @@ -22,7 +22,9 @@ export const scopedInterceptors: { [key: string]: Interceptors } = {} function wrapperHook(hook: Function, params?: Record) { return function (data: unknown) { - return hook(data, params) || data + const result = hook(data, params); + // 只有当结果为undefined或null时才返回data,保留false值 + return result === undefined || result === null ? data : result; } } @@ -35,12 +37,33 @@ function queue( for (let i = 0; i < hooks.length; i++) { const hook = hooks[i] if (promise) { - promise = Promise.resolve(wrapperHook(hook, params)) + const wrappedHook = wrapperHook(hook, params); + promise = promise.then((data) => { + const res = wrappedHook(data); + if (isPromise(res)) { + return res.then((res) => { + if (res === false) { + return { then() {}, catch() {} } as Promise; + } + return res; + }); + } + if (res === false) { + return { then() {}, catch() {} } as Promise; + } + return res; + }); } else { const res = hook(data, params) if (isPromise(res)) { - promise = Promise.resolve(res) - } + // 修改这里:处理异步返回 false 的情况 + promise = res.then((res) => { + if (res === false) { + return { then() {}, catch() {} } as Promise; + } + return res; + }); + } if (res === false) { return { then() {},