提交 c32e3c5c 编写于 作者: fxy060608's avatar fxy060608

build app-plus-nvue

上级 60751396
......@@ -5867,98 +5867,98 @@ var serviceContext = (function () {
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
};
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$1({
requestTaskId,
state: 'success',
data: xhr.responseText,
statusCode,
header: parseResponseHeaders(xhr.getAllResponseHeaders())
});
} else {
publishStateChange$1({
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$1({
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$1({
requestTaskId,
state: 'success',
data,
statusCode,
header: headers
});
} else {
publishStateChange$1({
requestTaskId,
state: 'fail',
statusCode,
errMsg: 'abort'
});
}
});
requestTasks[requestTaskId] = {
abort () {
aborted = true;
if (abortTimeout) {
clearTimeout(abortTimeout);
}
publishStateChange$1({
requestTaskId,
state: 'fail',
statusCode: 0,
errMsg: 'abort'
});
}
};
} catch (e) {
return {
requestTaskId,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册