fix: 修改程序退出逻辑

This commit is contained in:
shanmite 2021-02-23 20:48:21 +08:00
parent 209eeb9996
commit 4ea44b1203
5 changed files with 49 additions and 43 deletions

View File

@ -104,11 +104,11 @@ const Base = {
if (hitokkoto && (Date.now() % 7)) {
resolve(hitokkoto)
} else {
resolve('发条动态证明自己是真人[doge][doge][doge]')
resolve('[doge][doge][doge]')
}
},
failure: () => {
resolve('发条动态证明自己是真人[doge][doge][doge]')
resolve('[doge][doge][doge]')
}
})
});

View File

@ -35,9 +35,11 @@
* @property {SuccessCb} success 成功回调
* @property {FailureCb} failure 失败回调
*/
const { request: http_request } = require('http');
const { request: https_request } = require('https');
const { stringify } = require('querystring');
/**超时时间 */
const DEFAULT_TIMEOUT = 20000;
/**出错等待时间 */
@ -48,12 +50,13 @@ const DEFAULT_RETRY = true;
const DEFAULT_RETRY_TIMES = 6;
/**Google Chrome */
const DEFAULT_UA = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36';
/**
* @description 简化HTTP请求
* @param {RequestOptions} detail
* @returns {void}
*/
const HttpRequest = detail => {
function HttpRequest(detail) {
const { method, url, config = {}, proxy, query, contents, headers = {}, success, failure } = detail;
let { timeout = DEFAULT_TIMEOUT, wait = DEFAULT_WAIT, retry = DEFAULT_RETRY, retry_times = DEFAULT_RETRY_TIMES } = config;
const thisURL = new URL(url)
@ -96,21 +99,22 @@ const HttpRequest = detail => {
res.emit('error', new Error(`HTTP状态码: ${statusCode}`))
}
})
});
})
req.on('error', async err => {
if (retry && retry_times--) {
console.log('[请求失败]尝试重新连接中...');
await delay(wait);
HttpRequest(detail);
} else {
failure(`[请求失败]: ${err.message}`);
}
})
req.on('timeout', () => { req.destroy(new Error('请求超时')) })
.on('error', async err => {
if (retry && retry_times--) {
console.log('[请求失败]尝试重新连接中...');
await delay(wait);
HttpRequest(detail);
} else {
failure(`[请求失败]: ${err.message}`);
}
})
.end(content);
req.end(content)
}
/**
* 处理请求体
* 处理请求体
* 默认url编码字符串
* @private
* @param {string} type 请求的内容格式
@ -122,6 +126,7 @@ function formatContents(type, contents) {
if (contents) return stringify(contents)
return contents;
}
/**
* 延时函数
* @param {number} time ms
@ -134,6 +139,6 @@ function delay(time) {
}, time);
});
}
module.exports = {
HttpRequest
}
module.exports = { HttpRequest }

View File

@ -44,7 +44,7 @@ class Monitor extends Public {
} else {
this.AllMyLotteryInfo = alldyid;
}
this.startLottery();
await this.startLottery();
}
/**
* 启动
@ -52,28 +52,22 @@ class Monitor extends Public {
*/
async startLottery() {
const allLottery = await this.filterLotteryInfo();
const len = allLottery.length;
let index = 0;
if (len === 0) {
eventBus.emit('Turn_on_the_Monitor');
} else {
let status = 0;
if (allLottery.length) {
let dyids = [];
for (const Lottery of allLottery) {
if (await this.go(Lottery) === 0) {
Base.getHiToKoTo().then(sentence => {
BiliAPI.createDynamic(sentence);
})
break
}
status = await this.go(Lottery);
if (!status) break
dyids.push(Lottery.dyid);
if (index++ === len - 1) {
console.log('开始转发下一组动态');
eventBus.emit('Turn_on_the_Monitor');
}
}
MyStorage.updateDyid((Array.from(new Set([...dyids, ...this.AllMyLotteryInfo.split(',')]))).toString())
console.log('开始转发下一组动态');
} else {
status = 1;
console.log('无未转发抽奖');
}
return;
status ?
eventBus.emit('Turn_on_the_Monitor') : eventBus.emit('Turn_off_the_Monitor');
}
/**
* 抽奖配置

View File

@ -63,6 +63,15 @@ const pushplus = (text, desp) => {
*/
function start() {
return new Promise(resolve => {
function createRandomDynamic() {
Public.prototype.checkAllDynamic(GlobalVar.myUID, 1).then(Dynamic => {
if (Dynamic.allModifyDynamicResArray[0].type === 1) {
Base.getHiToKoTo().then(sentence => {
BiliAPI.createDynamic(sentence);
})
}
})
}
let times = Base.counter();
/* 注册事件 */
eventBus.on('Turn_on_the_Monitor', () => {
@ -70,13 +79,7 @@ function start() {
console.log('所有动态转发完毕');
console.log('[运行结束]目前无抽奖信息,过一会儿再来看看吧');
times.clear();
Public.prototype.checkAllDynamic(GlobalVar.myUID, 1).then(Dynamic => {
if (Dynamic.allModifyDynamicResArray[0].type === 1) {
Base.getHiToKoTo().then(sentence => {
BiliAPI.createDynamic(sentence);
})
}
})
createRandomDynamic()
resolve()
return
}
@ -84,6 +87,11 @@ function start() {
const nlottery = Number(lottery);
(new Monitor(isNaN(nlottery) ? lottery : nlottery)).init();
});
eventBus.on('Turn_off_the_Monitor', () => {
console.log('[运行结束]访问频繁程序自动关闭');
createRandomDynamic()
resolve()
})
eventBus.emit('Turn_on_the_Monitor');
});
}

View File

@ -29,5 +29,4 @@ const { NUMBER, CLEAR, COOKIE, SCKEY, PUSH_PLUS_TOKEN } = process.env;
break;
}
}
process.exit(0)
}))();