提交 60751396 编写于 作者: Q qiang

perf: 修改 app 端 request 实现方式

上级 1da07296
import { import {
publish publish,
requireNativePlugin
} from '../../bridge' } from '../../bridge'
const USER_AGENT = const USER_AGENT =
...@@ -13,98 +14,98 @@ const publishStateChange = res => { ...@@ -13,98 +14,98 @@ const publishStateChange = res => {
delete requestTasks[requestTaskId] delete requestTasks[requestTaskId]
} }
const parseResponseHeaders = headerStr => {
const headers = {}
if (!headerStr) {
return headers
}
const headerPairs = headerStr.split('\u000d\u000a')
for (let i = 0; i < headerPairs.length; i++) {
const headerPair = headerPairs[i]
const index = headerPair.indexOf('\u003a\u0020')
if (index > 0) {
const key = headerPair.substring(0, index)
const val = headerPair.substring(index + 2)
headers[key] = val
}
}
return headers
}
export function createRequestTaskById (requestTaskId, { export function createRequestTaskById (requestTaskId, {
url, url,
data, data,
header, header,
method = 'GET' method = 'GET'
} = {}) { } = {}) {
let abortTimeout const stream = requireNativePlugin('stream')
let xhr const headers = {}
// fixed by hxy 始终使用 plus 的 XHR
xhr = new plus.net.XMLHttpRequest() let abortTimeout
xhr.open(method, url, true) let aborted
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (abortTimeout) {
clearTimeout(abortTimeout)
}
xhr.onreadystatechange = null
const statusCode = xhr.status
if (statusCode) {
publishStateChange({
requestTaskId,
state: 'success',
data: xhr.responseText,
statusCode,
header: parseResponseHeaders(xhr.getAllResponseHeaders())
})
} else {
publishStateChange({
requestTaskId,
state: 'fail',
statusCode,
errMsg: 'abort'
})
}
}
}
let hasContentType = false let hasContentType = false
for (const name in header) { for (const name in header) {
if (header.hasOwnProperty(name)) { if (!hasContentType && name.toLowerCase() === 'content-type') {
if (!hasContentType && name.toLowerCase() === 'content-type') { hasContentType = true
hasContentType = true headers['Content-Type'] = header[name]
xhr.setRequestHeader('Content-Type', header[name]) // 大小写必须一致,否则部分服务器会返回invalid header name } else {
} else { headers[name] = header[name]
xhr.setRequestHeader(name, header[name]) }
} }
}
} if (!hasContentType && method === 'POST') {
if (!hasContentType && method === 'POST') { headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8'
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8')
} }
if (__uniConfig.crossDomain === true) { if (__uniConfig.crossDomain === true) {
xhr.setRequestHeader('User-Agent', USER_AGENT) headers['User-Agent'] = USER_AGENT
} }
if (__uniConfig.networkTimeout.request) { if (__uniConfig.networkTimeout.request) {
abortTimeout = setTimeout(() => { abortTimeout = setTimeout(() => {
xhr.onreadystatechange = null aborted = true
xhr.abort()
publishStateChange({ publishStateChange({
requestTaskId, requestTaskId,
state: 'fail', state: 'fail',
data: xhr.responseText,
statusCode: 0, statusCode: 0,
errMsg: 'timeout' errMsg: 'timeout'
}) })
}, __uniConfig.networkTimeout.request) }, __uniConfig.networkTimeout.request)
} }
const options = {
if (typeof data !== 'string' && method === 'GET') { method,
data = null url,
// weex 官方文档有误,headers 类型实际 object,用 string 类型会无响应
headers,
type: 'text'
}
if (method !== 'GET') {
options.body = data
} }
try { try {
xhr.send(data) stream.fetch(options, ({
requestTasks[requestTaskId] = xhr status,
data,
headers
}) => {
if (aborted) {
return
}
if (abortTimeout) {
clearTimeout(abortTimeout)
}
const statusCode = status
if (statusCode > 0) {
publishStateChange({
requestTaskId,
state: 'success',
data,
statusCode,
header: headers
})
} else {
publishStateChange({
requestTaskId,
state: 'fail',
statusCode,
errMsg: 'abort'
})
}
})
requestTasks[requestTaskId] = {
abort () {
aborted = true
if (abortTimeout) {
clearTimeout(abortTimeout)
}
publishStateChange({
requestTaskId,
state: 'fail',
statusCode: 0,
errMsg: 'abort'
})
}
}
} catch (e) { } catch (e) {
return { return {
requestTaskId, requestTaskId,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册