提交 55d307b6 编写于 作者: fxy060608's avatar fxy060608

feat(mp): add interceptor

上级 52a39440
......@@ -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,
......
......@@ -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) => {
......
export {
upx2px
}
from './upx2px'
export * from '../service/api/interceptor'
......@@ -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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册