提交 01df4118 编写于 作者: Q qiang

fix: request 支持 responseType 值为 arraybuffer(app-plus)

上级 1e15549b
......@@ -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,13 +83,13 @@ export const request = {
dataType: {
type: String,
validator (value, params) {
params.dataType = (value || dataType.JSON).toUpperCase()
params.dataType = (value || dataType.JSON).toLowerCase()
}
},
responseType: {
type: String,
validator (value, params) {
value = (value || '').toUpperCase()
value = (value || '').toLowerCase()
params.responseType = Object.values(responseType).indexOf(value) < 0 ? responseType.TEXT : value
}
}
......
import {
publish,
requireNativePlugin
requireNativePlugin,
base64ToArrayBuffer
} from '../../bridge'
let requestTaskId = 0
......@@ -15,7 +16,8 @@ export function createRequestTaskById (requestTaskId, {
url,
data,
header,
method = 'GET'
method = 'GET',
responseType
} = {}) {
const stream = requireNativePlugin('stream')
const headers = {}
......@@ -53,7 +55,7 @@ export function createRequestTaskById (requestTaskId, {
url: url.trim(),
// weex 官方文档有误,headers 类型实际 object,用 string 类型会无响应
headers,
type: 'text',
type: responseType === 'arraybuffer' ? 'base64' : 'text',
// weex 官方文档未说明实际支持 timeout,单位:ms
timeout: timeout || 6e5
}
......@@ -62,6 +64,7 @@ export function createRequestTaskById (requestTaskId, {
}
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
})
......
import {
decode
} from 'base64-arraybuffer'
export {
pack,
unpack,
invoke
}
from 'uni-core/service/bridge'
} from 'uni-core/service/bridge'
export function requireNativePlugin (name) {
return uni.requireNativePlugin(name)
......@@ -62,3 +65,7 @@ export function isTabBarPage (path = '') {
}
return false
}
export function base64ToArrayBuffer (data) {
return decode(data)
}
......@@ -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) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册