From 55d307b60ed2bfdb148e0e1a6dee260f426e6d0d Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Fri, 12 Jul 2019 20:29:32 +0800 Subject: [PATCH] feat(mp): add interceptor --- build/rollup.config.js | 3 ++- src/core/helpers/interceptor.js | 9 +-------- src/core/runtime/base.js | 6 ++++++ src/core/runtime/index.js | 18 +++++++++--------- 4 files changed, 18 insertions(+), 18 deletions(-) create mode 100644 src/core/runtime/base.js diff --git a/build/rollup.config.js b/build/rollup.config.js index 2af5d4afb..2223cfdc6 100644 --- a/build/rollup.config.js +++ b/build/rollup.config.js @@ -41,7 +41,8 @@ module.exports = { alias({ 'uni-shared': path.resolve(__dirname, '../src/shared/util.js'), 'uni-platform': path.resolve(__dirname, '../src/platforms/' + process.env.UNI_PLATFORM), - 'uni-wrapper': path.resolve(__dirname, '../src/core/runtime/wrapper') + 'uni-wrapper': path.resolve(__dirname, '../src/core/runtime/wrapper'), + 'uni-helpers': path.resolve(__dirname, '../src/core/helpers') }), replace({ __GLOBAL__: platform.prefix, diff --git a/src/core/helpers/interceptor.js b/src/core/helpers/interceptor.js index 577105eea..cbf20c2d2 100644 --- a/src/core/helpers/interceptor.js +++ b/src/core/helpers/interceptor.js @@ -3,10 +3,6 @@ import { isPlainObject } from 'uni-shared' -import { - shouldPromise -} from './promise' - const HOOKS = [ 'invoke', 'success', @@ -68,9 +64,6 @@ function removeInterceptorHook (interceptor, option) { export function addInterceptor (method, option) { if (typeof method === 'string' && isPlainObject(option)) { - if (!shouldPromise(method)) { - return console.warn(`${method} 不支持设置拦截器`) - } mergeInterceptorHook(scopedInterceptors[method] || (scopedInterceptors[method] = {}), option) } else if (isPlainObject(method)) { mergeInterceptorHook(globalInterceptors, method) @@ -174,7 +167,7 @@ function getApiInterceptorHooks (method) { export function invokeApi (method, api, options, ...params) { const interceptor = getApiInterceptorHooks(method) - if (interceptor) { + if (interceptor && Object.keys(interceptor).length) { if (Array.isArray(interceptor.invoke)) { const res = queue(interceptor.invoke, options) return res.then((options) => { diff --git a/src/core/runtime/base.js b/src/core/runtime/base.js new file mode 100644 index 000000000..66bb97f8d --- /dev/null +++ b/src/core/runtime/base.js @@ -0,0 +1,6 @@ +export { + upx2px +} + from './upx2px' + +export * from '../service/api/interceptor' diff --git a/src/core/runtime/index.js b/src/core/runtime/index.js index 8ba99d37b..a00a4c94d 100644 --- a/src/core/runtime/index.js +++ b/src/core/runtime/index.js @@ -6,9 +6,7 @@ import { promisify } from '../helpers/promise' -import { - upx2px -} from './upx2px' +import * as baseApi from './base' import wrapper from './wrapper' @@ -47,8 +45,8 @@ let uni = {} if (typeof Proxy !== 'undefined' && __PLATFORM__ !== 'app-plus') { uni = new Proxy({}, { get (target, name) { - if (name === 'upx2px') { - return upx2px + if (baseApi[name]) { + return baseApi[name] } if (api[name]) { return promisify(name, api[name]) @@ -70,8 +68,10 @@ if (typeof Proxy !== 'undefined' && __PLATFORM__ !== 'app-plus') { return promisify(name, wrapper(name, __GLOBAL__[name])) } }) -} else { - uni.upx2px = upx2px +} else { + Object.keys(baseApi).forEach(name => { + uni[name] = baseApi[name] + }) if (__PLATFORM__ !== 'app-plus') { Object.keys(todoApi).forEach(name => { @@ -98,12 +98,12 @@ if (typeof Proxy !== 'undefined' && __PLATFORM__ !== 'app-plus') { } if (__PLATFORM__ === 'app-plus') { - if (typeof global !== 'undefined') { + if (typeof global !== 'undefined') { global.uni = uni global.UniEmitter = eventApi } } - + __GLOBAL__.createApp = createApp __GLOBAL__.createPage = createPage __GLOBAL__.createComponent = createComponent -- GitLab