Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
uni-app
提交
01df4118
U
uni-app
项目概览
DCloud
/
uni-app
13 天 前同步成功
通知
751
Star
38709
Fork
3642
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
8
列表
看板
标记
里程碑
合并请求
1
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
U
uni-app
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
8
Issue
8
列表
看板
标记
里程碑
合并请求
1
合并请求
1
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
01df4118
编写于
8月 16, 2019
作者:
Q
qiang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix: request 支持 responseType 值为 arraybuffer(app-plus)
上级
1e15549b
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
96 addition
and
86 deletion
+96
-86
src/core/helpers/protocol/network/request.js
src/core/helpers/protocol/network/request.js
+6
-6
src/platforms/app-plus/service/api/network/request.js
src/platforms/app-plus/service/api/network/request.js
+73
-70
src/platforms/app-plus/service/bridge.js
src/platforms/app-plus/service/bridge.js
+13
-6
src/platforms/h5/service/api/network/request.js
src/platforms/h5/service/api/network/request.js
+4
-4
未找到文件。
src/core/helpers/protocol/network/request.js
浏览文件 @
01df4118
...
...
@@ -13,11 +13,11 @@ const method = {
CONNECT
:
'
CONNECT
'
}
const
dataType
=
{
JSON
:
'
JSON
'
JSON
:
'
json
'
}
const
responseType
=
{
TEXT
:
'
TEXT
'
,
ARRAYBUFFER
:
'
ARRAYBUFFER
'
TEXT
:
'
text
'
,
ARRAYBUFFER
:
'
arraybuffer
'
}
const
encode
=
encodeURIComponent
...
...
@@ -83,14 +83,14 @@ export const request = {
dataType
:
{
type
:
String
,
validator
(
value
,
params
)
{
params
.
dataType
=
(
value
||
dataType
.
JSON
).
to
Upp
erCase
()
params
.
dataType
=
(
value
||
dataType
.
JSON
).
to
Low
erCase
()
}
},
responseType
:
{
type
:
String
,
validator
(
value
,
params
)
{
value
=
(
value
||
''
).
to
Upp
erCase
()
value
=
(
value
||
''
).
to
Low
erCase
()
params
.
responseType
=
Object
.
values
(
responseType
).
indexOf
(
value
)
<
0
?
responseType
.
TEXT
:
value
}
}
}
}
src/platforms/app-plus/service/api/network/request.js
浏览文件 @
01df4118
import
{
import
{
publish
,
requireNativePlugin
}
from
'
../../bridge
'
let
requestTaskId
=
0
const
requestTasks
=
{}
const
publishStateChange
=
res
=>
{
publish
(
'
onRequestTaskStateChange
'
,
res
)
delete
requestTasks
[
requestTaskId
]
}
export
function
createRequestTaskById
(
requestTaskId
,
{
url
,
data
,
header
,
method
=
'
GET
'
requireNativePlugin
,
base64ToArrayBuffer
}
from
'
../../bridge
'
let
requestTaskId
=
0
const
requestTasks
=
{}
const
publishStateChange
=
res
=>
{
publish
(
'
onRequestTaskStateChange
'
,
res
)
delete
requestTasks
[
requestTaskId
]
}
export
function
createRequestTaskById
(
requestTaskId
,
{
url
,
data
,
header
,
method
=
'
GET
'
,
responseType
}
=
{})
{
const
stream
=
requireNativePlugin
(
'
stream
'
)
const
headers
=
{}
let
abortTimeout
let
aborted
let
hasContentType
=
false
let
aborted
let
hasContentType
=
false
for
(
const
name
in
header
)
{
if
(
!
hasContentType
&&
name
.
toLowerCase
()
===
'
content-type
'
)
{
hasContentType
=
true
...
...
@@ -31,37 +33,38 @@ export function createRequestTaskById (requestTaskId, {
headers
[
name
]
=
header
[
name
]
}
}
if
(
!
hasContentType
&&
method
===
'
POST
'
)
{
headers
[
'
Content-Type
'
]
=
'
application/x-www-form-urlencoded; charset=UTF-8
'
}
headers
[
'
Content-Type
'
]
=
'
application/x-www-form-urlencoded; charset=UTF-8
'
}
const
timeout
=
__uniConfig
.
networkTimeout
.
request
if
(
timeout
)
{
const
timeout
=
__uniConfig
.
networkTimeout
.
request
if
(
timeout
)
{
abortTimeout
=
setTimeout
(()
=>
{
aborted
=
true
publishStateChange
({
requestTaskId
,
state
:
'
fail
'
,
statusCode
:
0
,
errMsg
:
'
timeout
'
})
},
timeout
)
}
aborted
=
true
publishStateChange
({
requestTaskId
,
state
:
'
fail
'
,
statusCode
:
0
,
errMsg
:
'
timeout
'
})
},
timeout
)
}
const
options
=
{
method
,
url
:
url
.
trim
(),
// weex 官方文档有误,headers 类型实际 object,用 string 类型会无响应
headers
,
type
:
'
text
'
,
type
:
responseType
===
'
arraybuffer
'
?
'
base64
'
:
'
text
'
,
// weex 官方文档未说明实际支持 timeout,单位:ms
timeout
:
timeout
||
6
e5
}
if
(
method
!==
'
GET
'
)
{
options
.
body
=
data
}
try
{
}
try
{
stream
.
fetch
(
options
,
({
ok
,
status
,
data
,
headers
...
...
@@ -77,7 +80,7 @@ export function createRequestTaskById (requestTaskId, {
publishStateChange
({
requestTaskId
,
state
:
'
success
'
,
data
,
data
:
ok
&&
responseType
===
'
arraybuffer
'
?
base64ToArrayBuffer
(
data
)
:
data
,
statusCode
,
header
:
headers
})
...
...
@@ -89,7 +92,7 @@ export function createRequestTaskById (requestTaskId, {
errMsg
:
'
abort
'
})
}
})
})
requestTasks
[
requestTaskId
]
=
{
abort
()
{
aborted
=
true
...
...
@@ -103,35 +106,35 @@ export function createRequestTaskById (requestTaskId, {
errMsg
:
'
abort
'
})
}
}
}
catch
(
e
)
{
return
{
requestTaskId
,
errMsg
:
'
createRequestTask:fail
'
}
}
return
{
requestTaskId
,
errMsg
:
'
createRequestTask:ok
'
}
}
export
function
createRequestTask
(
args
)
{
return
createRequestTaskById
(
++
requestTaskId
,
args
)
}
export
function
operateRequestTask
({
requestTaskId
,
operationType
}
=
{})
{
const
requestTask
=
requestTasks
[
requestTaskId
]
if
(
requestTask
&&
operationType
===
'
abort
'
)
{
requestTask
.
abort
()
return
{
errMsg
:
'
operateRequestTask:ok
'
}
}
return
{
errMsg
:
'
operateRequestTask:fail
'
}
}
}
catch
(
e
)
{
return
{
requestTaskId
,
errMsg
:
'
createRequestTask:fail
'
}
}
return
{
requestTaskId
,
errMsg
:
'
createRequestTask:ok
'
}
}
export
function
createRequestTask
(
args
)
{
return
createRequestTaskById
(
++
requestTaskId
,
args
)
}
export
function
operateRequestTask
({
requestTaskId
,
operationType
}
=
{})
{
const
requestTask
=
requestTasks
[
requestTaskId
]
if
(
requestTask
&&
operationType
===
'
abort
'
)
{
requestTask
.
abort
()
return
{
errMsg
:
'
operateRequestTask:ok
'
}
}
return
{
errMsg
:
'
operateRequestTask:fail
'
}
}
src/platforms/app-plus/service/bridge.js
浏览文件 @
01df4118
import
{
decode
}
from
'
base64-arraybuffer
'
export
{
pack
,
pack
,
unpack
,
invoke
}
from
'
uni-core/service/bridge
'
}
from
'
uni-core/service/bridge
'
export
function
requireNativePlugin
(
name
)
{
return
uni
.
requireNativePlugin
(
name
)
...
...
@@ -36,8 +39,8 @@ export function setStatusBarStyle (statusBarStyle) {
lastStatusBarStyle
=
statusBarStyle
plus
.
navigator
.
setStatusBarStyle
(
statusBarStyle
)
}
}
export
function
isTabBarPage
(
path
=
''
)
{
if
(
!
(
__uniConfig
.
tabBar
&&
Array
.
isArray
(
__uniConfig
.
tabBar
.
list
)))
{
return
false
...
...
@@ -61,4 +64,8 @@ export function isTabBarPage (path = '') {
}
}
return
false
}
}
export
function
base64ToArrayBuffer
(
data
)
{
return
decode
(
data
)
}
src/platforms/h5/service/api/network/request.js
浏览文件 @
01df4118
...
...
@@ -115,12 +115,12 @@ export function request ({
errMsg
:
'
request:fail timeout
'
})
},
timeout
)
xhr
.
responseType
=
responseType
.
toLowerCase
()
xhr
.
responseType
=
responseType
xhr
.
onload
=
function
()
{
clearTimeout
(
timer
)
let
statusCode
=
xhr
.
status
let
res
=
responseType
===
'
TEXT
'
?
xhr
.
responseText
:
xhr
.
response
if
(
responseType
===
'
TEXT
'
&&
dataType
===
'
JSON
'
)
{
let
res
=
responseType
===
'
text
'
?
xhr
.
responseText
:
xhr
.
response
if
(
responseType
===
'
text
'
&&
dataType
===
'
json
'
)
{
try
{
res
=
JSON
.
parse
(
res
)
}
catch
(
error
)
{
...
...
@@ -152,4 +152,4 @@ export function request ({
}
xhr
.
send
(
body
)
return
requestTask
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录