Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
20岁爱吃必胜客
uni-app
提交
52a39440
U
uni-app
项目概览
20岁爱吃必胜客
/
uni-app
与 Fork 源项目一致
Fork自
DCloud / uni-app
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
U
uni-app
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
52a39440
编写于
7月 12, 2019
作者:
fxy060608
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat(h5): add interceptor
上级
9b08d6ba
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
366 addition
and
6 deletion
+366
-6
src/core/helpers/api.js
src/core/helpers/api.js
+3
-0
src/core/helpers/interceptor.js
src/core/helpers/interceptor.js
+201
-0
src/core/helpers/promise.js
src/core/helpers/promise.js
+12
-6
src/core/service/api/interceptor.js
src/core/service/api/interceptor.js
+13
-0
tests/unit/core/service/api/interceptor.spec.js
tests/unit/core/service/api/interceptor.spec.js
+137
-0
tests/unit/core/view/components/events.spec.bak.js
tests/unit/core/view/components/events.spec.bak.js
+0
-0
未找到文件。
src/core/helpers/api.js
浏览文件 @
52a39440
...
...
@@ -240,6 +240,9 @@ export function wrapperUnimplemented (name) {
}
export
function
wrapper
(
name
,
invokeMethod
,
extras
)
{
if
(
!
isFn
(
invokeMethod
))
{
return
invokeMethod
}
return
function
(...
args
)
{
if
(
isSyncApi
(
name
))
{
if
(
validateParams
(
name
,
args
,
-
1
))
{
...
...
src/core/helpers/interceptor.js
0 → 100644
浏览文件 @
52a39440
import
{
isFn
,
isPlainObject
}
from
'
uni-shared
'
import
{
shouldPromise
}
from
'
./promise
'
const
HOOKS
=
[
'
invoke
'
,
'
success
'
,
'
fail
'
,
'
complete
'
,
'
returnValue
'
]
const
globalInterceptors
=
{}
const
scopedInterceptors
=
{}
function
mergeHook
(
parentVal
,
childVal
)
{
const
res
=
childVal
?
parentVal
?
parentVal
.
concat
(
childVal
)
:
Array
.
isArray
(
childVal
)
?
childVal
:
[
childVal
]
:
parentVal
return
res
?
dedupeHooks
(
res
)
:
res
}
function
dedupeHooks
(
hooks
)
{
const
res
=
[]
for
(
let
i
=
0
;
i
<
hooks
.
length
;
i
++
)
{
if
(
res
.
indexOf
(
hooks
[
i
])
===
-
1
)
{
res
.
push
(
hooks
[
i
])
}
}
return
res
}
function
removeHook
(
hooks
,
hook
)
{
const
index
=
hooks
.
indexOf
(
hook
)
if
(
index
!==
-
1
)
{
hooks
.
splice
(
index
,
1
)
}
}
function
mergeInterceptorHook
(
interceptor
,
option
)
{
Object
.
keys
(
option
).
forEach
(
hook
=>
{
if
(
HOOKS
.
indexOf
(
hook
)
!==
-
1
&&
isFn
(
option
[
hook
]))
{
interceptor
[
hook
]
=
mergeHook
(
interceptor
[
hook
],
option
[
hook
])
}
})
}
function
removeInterceptorHook
(
interceptor
,
option
)
{
if
(
!
interceptor
||
!
option
)
{
return
}
Object
.
keys
(
option
).
forEach
(
hook
=>
{
if
(
HOOKS
.
indexOf
(
hook
)
!==
-
1
&&
isFn
(
option
[
hook
]))
{
removeHook
(
interceptor
[
hook
],
option
[
hook
])
}
})
}
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
)
}
}
export
function
removeInterceptor
(
method
,
option
)
{
if
(
typeof
method
===
'
string
'
)
{
if
(
isPlainObject
(
option
))
{
removeInterceptorHook
(
scopedInterceptors
[
method
],
option
)
}
else
{
delete
scopedInterceptors
[
method
]
}
}
else
if
(
isPlainObject
(
method
))
{
removeInterceptorHook
(
globalInterceptors
,
method
)
}
}
function
wrapperHook
(
hook
)
{
return
function
(
data
)
{
return
hook
(
data
)
||
data
}
}
function
isPromise
(
obj
)
{
return
!!
obj
&&
(
typeof
obj
===
'
object
'
||
typeof
obj
===
'
function
'
)
&&
typeof
obj
.
then
===
'
function
'
}
function
queue
(
hooks
,
data
)
{
let
promise
=
false
for
(
let
i
=
0
;
i
<
hooks
.
length
;
i
++
)
{
const
hook
=
hooks
[
i
]
if
(
promise
)
{
promise
=
Promise
.
then
(
wrapperHook
(
hook
))
}
else
{
const
res
=
hook
(
data
)
if
(
isPromise
(
res
))
{
promise
=
Promise
.
resolve
(
res
)
}
if
(
res
===
false
)
{
return
{
then
()
{}
}
}
}
}
return
promise
||
{
then
(
callback
)
{
return
callback
(
data
)
}
}
}
function
wrapperOptions
(
interceptor
,
options
=
{})
{
[
'
success
'
,
'
fail
'
,
'
complete
'
].
forEach
(
name
=>
{
if
(
Array
.
isArray
(
interceptor
[
name
]))
{
const
oldCallback
=
options
[
name
]
options
[
name
]
=
function
callbackInterceptor
(
res
)
{
queue
(
interceptor
[
name
],
res
).
then
((
res
)
=>
{
/* eslint-disable no-mixed-operators */
return
isFn
(
oldCallback
)
&&
oldCallback
(
res
)
||
res
})
}
}
})
return
options
}
export
function
wrapperReturnValue
(
method
,
returnValue
)
{
const
returnValueHooks
=
[]
if
(
Array
.
isArray
(
globalInterceptors
.
returnValue
))
{
returnValueHooks
.
push
(...
globalInterceptors
.
returnValue
)
}
const
interceptor
=
scopedInterceptors
[
method
]
if
(
interceptor
&&
Array
.
isArray
(
interceptor
.
returnValue
))
{
returnValueHooks
.
push
(...
interceptor
.
returnValue
)
}
returnValueHooks
.
forEach
(
hook
=>
{
returnValue
=
hook
(
returnValue
)
||
returnValue
})
return
returnValue
}
function
getApiInterceptorHooks
(
method
)
{
const
interceptor
=
Object
.
create
(
null
)
Object
.
keys
(
globalInterceptors
).
forEach
(
hook
=>
{
if
(
hook
!==
'
returnValue
'
)
{
interceptor
[
hook
]
=
globalInterceptors
[
hook
].
slice
()
}
})
const
scopedInterceptor
=
scopedInterceptors
[
method
]
if
(
scopedInterceptor
)
{
Object
.
keys
(
scopedInterceptor
).
forEach
(
hook
=>
{
if
(
hook
!==
'
returnValue
'
)
{
interceptor
[
hook
]
=
(
interceptor
[
hook
]
||
[]).
concat
(
scopedInterceptor
[
hook
])
}
})
}
return
interceptor
}
export
function
invokeApi
(
method
,
api
,
options
,
...
params
)
{
const
interceptor
=
getApiInterceptorHooks
(
method
)
if
(
interceptor
)
{
if
(
Array
.
isArray
(
interceptor
.
invoke
))
{
const
res
=
queue
(
interceptor
.
invoke
,
options
)
return
res
.
then
((
options
)
=>
{
return
api
(
wrapperOptions
(
interceptor
,
options
),
...
params
)
})
}
else
{
return
api
(
wrapperOptions
(
interceptor
,
options
),
...
params
)
}
}
return
api
(
options
,
...
params
)
}
export
const
promiseInterceptor
=
{
returnValue
(
res
)
{
if
(
!
isPromise
(
res
))
{
return
res
}
return
res
.
then
(
res
=>
{
return
res
[
1
]
}).
catch
(
res
=>
{
return
res
[
0
]
})
}
}
src/core/helpers/promise.js
浏览文件 @
52a39440
...
...
@@ -2,7 +2,13 @@ import {
isFn
}
from
'
uni-shared
'
const
SYNC_API_RE
=
/^
\$
|getSubNVueById|requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$|base64ToArrayBuffer|arrayBufferToBase64/
import
{
invokeApi
,
wrapperReturnValue
}
from
'
./interceptor
'
const
SYNC_API_RE
=
/^
\$
|interceptors|Interceptor$|getSubNVueById|requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$|base64ToArrayBuffer|arrayBufferToBase64/
const
CONTEXT_API_RE
=
/^create|Manager$/
...
...
@@ -49,10 +55,10 @@ export function promisify (name, api) {
}
return
function
promiseApi
(
options
=
{},
...
params
)
{
if
(
isFn
(
options
.
success
)
||
isFn
(
options
.
fail
)
||
isFn
(
options
.
complete
))
{
return
api
(
options
,
...
params
)
return
wrapperReturnValue
(
name
,
invokeApi
(
name
,
api
,
options
,
...
params
)
)
}
return
handlePromise
(
new
Promise
((
resolve
,
reject
)
=>
{
api
(
Object
.
assign
({},
options
,
{
return
wrapperReturnValue
(
name
,
handlePromise
(
new
Promise
((
resolve
,
reject
)
=>
{
invokeApi
(
name
,
api
,
Object
.
assign
({},
options
,
{
success
:
resolve
,
fail
:
reject
}),
...
params
)
...
...
@@ -68,6 +74,6 @@ export function promisify (name, api) {
)
}
}
}))
}))
)
}
}
}
src/core/service/api/interceptor.js
0 → 100644
浏览文件 @
52a39440
import
{
promiseInterceptor
}
from
'
uni-helpers/interceptor
'
export
{
addInterceptor
,
removeInterceptor
}
from
'
uni-helpers/interceptor
'
export
const
interceptors
=
{
promiseInterceptor
}
tests/unit/core/service/api/interceptor.spec.js
0 → 100644
浏览文件 @
52a39440
import
{
expect
}
from
'
chai
'
describe
(
'
接口`addInterceptor`
'
,
()
=>
{
it
(
'
同步拦截器 invoke
'
,
done
=>
{
const
setStorageInterceptor
=
{
invoke
(
args
)
{
args
.
data
=
2
}
}
uni
.
addInterceptor
(
'
setStorage
'
,
setStorageInterceptor
)
uni
.
setStorage
({
key
:
'
test
'
,
data
:
1
}).
then
(
function
()
{
expect
(
uni
.
getStorageSync
(
'
test
'
)).
eq
(
2
)
uni
.
removeInterceptor
(
'
setStorage
'
)
return
uni
.
setStorage
({
key
:
'
test
'
,
data
:
1
})
}).
then
(
function
()
{
expect
(
uni
.
getStorageSync
(
'
test
'
)).
eq
(
1
)
done
()
})
})
it
(
'
同步拦截器 callback
'
,
done
=>
{
const
setStorageInterceptor
=
{
success
(
res
)
{
res
.
data
=
2
},
complete
(
res
)
{
res
.
data
=
3
}
}
uni
.
addInterceptor
(
'
setStorage
'
,
setStorageInterceptor
)
uni
.
setStorage
({
key
:
'
test
'
,
data
:
1
,
success
(
res
)
{
expect
(
res
.
data
).
eq
(
2
)
},
complete
(
res
)
{
uni
.
removeInterceptor
(
'
setStorage
'
)
expect
(
res
.
data
).
eq
(
3
)
done
()
}
})
})
it
(
'
异步拦截器 invoke
'
,
done
=>
{
const
setStorageInterceptor
=
{
invoke
(
args
)
{
return
new
Promise
(
function
(
resolve
,
reject
)
{
setTimeout
(
function
()
{
args
.
data
=
2
resolve
(
args
)
},
200
)
})
}
}
uni
.
addInterceptor
(
'
setStorage
'
,
setStorageInterceptor
)
uni
.
setStorage
({
key
:
'
test
'
,
data
:
1
}).
then
(
function
()
{
expect
(
uni
.
getStorageSync
(
'
test
'
)).
eq
(
2
)
uni
.
removeInterceptor
(
'
setStorage
'
)
return
uni
.
setStorage
({
key
:
'
test
'
,
data
:
1
})
}).
then
(
function
()
{
expect
(
uni
.
getStorageSync
(
'
test
'
)).
eq
(
1
)
done
()
})
})
it
(
'
异步拦截器 callback
'
,
done
=>
{
const
setStorageInterceptor
=
{
success
(
res
)
{
return
new
Promise
(
function
(
resolve
,
reject
)
{
setTimeout
(
function
()
{
res
.
data
=
2
resolve
(
res
)
},
200
)
})
},
complete
(
res
)
{
return
new
Promise
(
function
(
resolve
,
reject
)
{
setTimeout
(
function
()
{
res
.
data
=
3
resolve
(
res
)
},
1000
)
})
}
}
uni
.
addInterceptor
(
'
setStorage
'
,
setStorageInterceptor
)
uni
.
setStorage
({
key
:
'
test
'
,
data
:
1
,
success
(
res
)
{
expect
(
res
.
data
).
eq
(
2
)
},
complete
(
res
)
{
expect
(
res
.
data
).
eq
(
3
)
uni
.
setStorage
({
key
:
'
test
'
,
data
:
1
}).
then
(
function
(
res
)
{
uni
.
removeInterceptor
(
'
setStorage
'
)
expect
(
res
[
1
].
data
).
eq
(
2
)
done
()
})
}
})
})
it
(
'
全局拦截器 promiseInterceptor
'
,
done
=>
{
uni
.
addInterceptor
(
uni
.
interceptors
.
promiseInterceptor
)
uni
.
setStorageSync
(
'
test
'
,
1
)
uni
.
getStorage
({
key
:
'
test
'
}).
then
(
res
=>
{
expect
(
res
.
data
).
eq
(
1
)
uni
.
removeInterceptor
(
uni
.
interceptors
.
promiseInterceptor
)
uni
.
getStorage
({
key
:
'
test
'
}).
then
(
res
=>
{
expect
(
res
[
1
].
data
).
eq
(
1
)
done
()
})
})
})
})
tests/unit/core/view/components/events.spec.js
→
tests/unit/core/view/components/events.spec.
bak.
js
浏览文件 @
52a39440
文件已移动
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录