diff --git a/src/core/helpers/api.js b/src/core/helpers/api.js index 4292d41c3f4dafaef06c2331e0453138321cbfad..aa15ba4e62b1b343c4bcd3d73ed9635600bbf516 100644 --- a/src/core/helpers/api.js +++ b/src/core/helpers/api.js @@ -20,7 +20,9 @@ import validateParam from './params' function invokeCallbackHandlerFail (err, apiName, callbackId) { const errMsg = `${apiName}:fail ${err}` - console.error(errMsg) + if (process.env.NODE_ENV !== 'production') { + console.error(errMsg) + } if (callbackId === -1) { throw new Error(errMsg) } @@ -38,6 +40,23 @@ const callbackApiParamTypes = [{ required: true }] +// 目前已用到的仅这三个 +// 完整的可能包含: +// beforeValidate, +// beforeSuccess, +// afterSuccess, +// beforeFail, +// afterFail, +// beforeCancel, +// afterCancel, +// beforeAll, +// afterAll +const IGNORE_KEYS = [ + 'beforeValidate', + 'beforeAll', + 'beforeSuccess' +] + function validateParams (apiName, paramsData, callbackId) { let paramTypes = protocol[apiName] if (!paramTypes && isCallbackApi(apiName)) { @@ -67,7 +86,7 @@ function validateParams (apiName, paramsData, callbackId) { const keys = Object.keys(paramTypes) for (let i = 0; i < keys.length; i++) { - if (keys[i] === 'beforeValidate') { + if (IGNORE_KEYS.indexOf(keys[i]) !== -1) { continue } const err = validateParam(keys[i], paramTypes, paramsData) @@ -149,6 +168,7 @@ function createApiCallback (apiName, params = {}, extras = {}) { afterFail, beforeCancel, afterCancel, + beforeAll, afterAll } = wrapperCallbacks @@ -172,6 +192,8 @@ function createApiCallback (apiName, params = {}, extras = {}) { res.errMsg = apiName + ':fail' + errDetail } + isFn(beforeAll) && beforeAll(res) + const errMsg = res.errMsg if (errMsg.indexOf(apiName + ':ok') === 0) { @@ -255,6 +277,7 @@ export function wrapperUnimplemented (name) { function wrapperExtras (name, extras) { const protocolOptions = protocol[name] if (protocolOptions) { + isFn(protocolOptions.beforeAll) && (extras.beforeAll = protocolOptions.beforeAll) isFn(protocolOptions.beforeSuccess) && (extras.beforeSuccess = protocolOptions.beforeSuccess) } } diff --git a/src/core/helpers/protocol/route/route.js b/src/core/helpers/protocol/route/route.js index 5bb59bcfa5b49761eba9759d3d4261d4abb29b39..534c4559772f2331f388d44dc7fcbf55153eb59c 100644 --- a/src/core/helpers/protocol/route/route.js +++ b/src/core/helpers/protocol/route/route.js @@ -72,15 +72,30 @@ function createValidator (type) { // 参数格式化 params.url = encodeQueryString(url) + + // 主要拦截目标为用户快速点击时触发的多次跳转,该情况,通常前后 url 是一样的 + if (navigatorLock === url) { + return `${navigatorLock} locked` + } + // 至少 onLaunch 之后,再启用lock逻辑(onLaunch之前可能开发者手动调用路由API,来提前跳转) + // enableNavigatorLock 临时开关(不对外开放),避免该功能上线后,有部分情况异常,可以让开发者临时关闭 lock 功能 + if (__uniConfig.ready && __uniConfig.enableNavigatorLock !== false) { + navigatorLock = url + } } } +let navigatorLock + function createProtocol (type, extras = {}) { return Object.assign({ url: { type: String, required: true, validator: createValidator(type) + }, + beforeAll () { + navigatorLock = '' } }, extras) }