mirror of
https://github.com/shanmiteko/LotteryAutoScript.git
synced 2026-06-04 21:01:17 +08:00
fix: detail设置默认值
This commit is contained in:
parent
656b7e45e9
commit
94889ef91c
@ -64,8 +64,9 @@ const DEFAULT_UA = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36
|
||||
* @returns {void}
|
||||
*/
|
||||
function HttpRequest(detail) {
|
||||
const { method, url, stream = false, config = {}, proxy, query, contents, headers = {}, success, failure } = detail;
|
||||
let { timeout = DEFAULT_TIMEOUT, wait = DEFAULT_WAIT, retry = DEFAULT_RETRY, redirect = DEFAULT_REDIRECT, retry_times = DEFAULT_RETRY_TIMES } = config;
|
||||
detail = setDefault(detail);
|
||||
const { method, url, stream, config, proxy, query, contents, headers, success, failure } = detail;
|
||||
const { timeout, wait, retry, redirect, retry_times } = config;
|
||||
const thisURL = new URL(url)
|
||||
, content = formatContents(headers["content-type"], contents)
|
||||
, request = thisURL.protocol === 'https:' ? https_request : http_request;
|
||||
@ -104,10 +105,10 @@ function HttpRequest(detail) {
|
||||
res.setEncoding(DEFAULT_DECODE)
|
||||
.on('data', chunk => { protodata += chunk })
|
||||
.on('error', async err => {
|
||||
if (retry && retry_times--) {
|
||||
if (retry && retry_times) {
|
||||
console.log(`[不期待响应]原因:${err.message} 尝试重新请求中...`);
|
||||
await delay(wait);
|
||||
detail.config.retry_times = retry_times;
|
||||
detail.config.retry_times = retry_times - 1;
|
||||
HttpRequest(detail);
|
||||
} else {
|
||||
failure(`[响应错误]${err.message} 响应数据:\n${protodata}`)
|
||||
@ -123,10 +124,10 @@ function HttpRequest(detail) {
|
||||
}
|
||||
})
|
||||
req.on('error', async err => {
|
||||
if (retry && retry_times--) {
|
||||
if (retry && retry_times) {
|
||||
console.log(`[请求失败]原因:${err.message} 尝试重新连接中...`);
|
||||
await delay(wait);
|
||||
detail.config.retry_times = retry_times;
|
||||
detail.config.retry_times = retry_times - 1;
|
||||
HttpRequest(detail);
|
||||
} else {
|
||||
failure(`[请求失败]: ${err.message}`);
|
||||
@ -150,6 +151,27 @@ function formatContents(type, contents) {
|
||||
return contents;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置默认
|
||||
* @param {RequestOptions} detail
|
||||
*/
|
||||
function setDefault(detail) {
|
||||
detail = {
|
||||
stream: false,
|
||||
headers: {},
|
||||
...detail
|
||||
}
|
||||
detail.config = {
|
||||
timeout: DEFAULT_TIMEOUT,
|
||||
wait: DEFAULT_WAIT,
|
||||
retry: DEFAULT_RETRY,
|
||||
redirect: DEFAULT_REDIRECT,
|
||||
retry_times: DEFAULT_RETRY_TIMES,
|
||||
...detail.config
|
||||
}
|
||||
return detail;
|
||||
}
|
||||
|
||||
/**
|
||||
* 延时函数
|
||||
* @param {number} time ms
|
||||
|
||||
Loading…
Reference in New Issue
Block a user