From 6075139690ec41adbf4b90677bcdf1dc7b68fa4c Mon Sep 17 00:00:00 2001 From: qiang Date: Sat, 3 Aug 2019 17:42:20 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BF=AE=E6=94=B9=20app=20=E7=AB=AF=20?= =?UTF-8?q?request=20=E5=AE=9E=E7=8E=B0=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app-plus/service/api/network/request.js | 149 +++++++++--------- 1 file changed, 75 insertions(+), 74 deletions(-) diff --git a/src/platforms/app-plus/service/api/network/request.js b/src/platforms/app-plus/service/api/network/request.js index f595cfb91..861b992f8 100644 --- a/src/platforms/app-plus/service/api/network/request.js +++ b/src/platforms/app-plus/service/api/network/request.js @@ -1,5 +1,6 @@ import { - publish + publish, + requireNativePlugin } from '../../bridge' const USER_AGENT = @@ -13,98 +14,98 @@ const publishStateChange = res => { 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, { url, data, header, method = 'GET' -} = {}) { - let abortTimeout - let xhr - // fixed by hxy 始终使用 plus 的 XHR - xhr = new plus.net.XMLHttpRequest() - xhr.open(method, url, true) - 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' - }) - } - } - } +} = {}) { + const stream = requireNativePlugin('stream') + const headers = {} + + let abortTimeout + let aborted let hasContentType = false - for (const name in header) { - if (header.hasOwnProperty(name)) { - if (!hasContentType && name.toLowerCase() === 'content-type') { - hasContentType = true - xhr.setRequestHeader('Content-Type', header[name]) // 大小写必须一致,否则部分服务器会返回invalid header name - } else { - xhr.setRequestHeader(name, header[name]) - } - } - } - if (!hasContentType && method === 'POST') { - xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8') + for (const name in header) { + if (!hasContentType && name.toLowerCase() === 'content-type') { + hasContentType = true + headers['Content-Type'] = header[name] + } else { + headers[name] = header[name] + } + } + + if (!hasContentType && method === 'POST') { + headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8' } - if (__uniConfig.crossDomain === true) { - xhr.setRequestHeader('User-Agent', USER_AGENT) + if (__uniConfig.crossDomain === true) { + headers['User-Agent'] = USER_AGENT } - if (__uniConfig.networkTimeout.request) { - abortTimeout = setTimeout(() => { - xhr.onreadystatechange = null - xhr.abort() + abortTimeout = setTimeout(() => { + aborted = true publishStateChange({ requestTaskId, state: 'fail', - data: xhr.responseText, statusCode: 0, errMsg: 'timeout' }) }, __uniConfig.networkTimeout.request) } - - if (typeof data !== 'string' && method === 'GET') { - data = null + const options = { + method, + url, + // weex 官方文档有误,headers 类型实际 object,用 string 类型会无响应 + headers, + type: 'text' + } + if (method !== 'GET') { + options.body = data } try { - xhr.send(data) - requestTasks[requestTaskId] = xhr + stream.fetch(options, ({ + 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) { return { requestTaskId, -- GitLab