fix: 修改线路切换逻辑

This commit is contained in:
shanmiteko 2021-10-14 16:15:39 +08:00
parent 41b28a133e
commit 423bc24939

View File

@ -6,20 +6,23 @@ const API = require('./api.bili');
class Line {
/**
* 智能线路切换
* @typedef {string} ErrorMsg
* @typedef {[ErrorMsg, any]} ResResult
* @param {any} default_value
* @typedef {boolean} iSwitch 是否切换
* @typedef {string} Msg 信息说明
* @typedef {[iSwitch, any, Msg]} ResResult
* @param {string} line_name
* @param {Array<(...arg) => Promise<ResResult | string>>} requests
* @param {(responseText: string) => ResResult} [pub_handler]
*/
constructor(default_value, line_name, requests, pub_handler) {
constructor(line_name, requests, pub_handler) {
this.line_name = line_name
this.requests = requests
this.valid_line = 0
this.switch_times = 0
this.default_value = default_value
if (pub_handler) this.pub_handler = pub_handler
/**
* @type {ResResult}
*/
this.res_result = [false, null, '']
}
/**
@ -53,26 +56,27 @@ class Line {
*/
async run(...args) {
const
{ line_name, requests, valid_line, default_value } = this,
{ line_name, requests, valid_line } = this,
resp = await requests[valid_line](...args);
let err, value;
if (typeof resp === 'string') {
[err, value] = this.pub_handler(resp)
this.res_result = this.pub_handler(resp)
} else {
[err, value] = resp
this.res_result = resp
}
if (!err) {
const [i_switch, value, msg] = this.res_result
if (!i_switch) {
log.info(line_name, msg)
this.storeLine(valid_line)
return value
}
if (this.switchLine()) {
log.warn(line_name, err)
log.warn(line_name, msg)
log.warn(line_name, `切换线路(${valid_line + 1}/${requests.length})`)
return await this.run()
} else {
log.error(line_name, err)
log.error(line_name, msg)
log.error(line_name, '所有备用线路均连接失败')
return default_value
return value
}
}
}
@ -463,7 +467,7 @@ const bili_client = {
/**
* 获取粉丝数的所有有效方式
*/
_getUserInfo: new Line(-1, '获取粉丝数', [
_getUserInfo: new Line('获取粉丝数', [
/**
* 线路一
* @param {number} uid
@ -493,9 +497,9 @@ const bili_client = {
], responseText => {
const res = strToJson(responseText);
if (res.code === 0) {
return [null, res.data.follower];
return [false, res.data.follower, 'ok'];
} else {
return [`出错 可能是访问过频繁\n${responseText}`, null]
return [true, 1, `出错 可能是访问过频繁\n${responseText}`]
}
}),
/**
@ -534,7 +538,7 @@ const bili_client = {
};
}
},
_autoAttention: new Line(1, '自动关注', [
_autoAttention: new Line('自动关注', [
(uid) => post({
url: API.RELATION_MODIFY,
config: {
@ -568,16 +572,15 @@ const bili_client = {
const res = strToJson(responseText);
switch (res.code) {
case 0:
log.info('自动关注', '关注+1');
return [null, 0]
return [false, 0, '关注+1']
case 22002:
return ['您已被对方拉入黑名单', -1]
return [false, -1, '您已被对方拉入黑名单']
case 22003:
return ['黑名单用户无法关注', -1]
return [false, -1, '黑名单用户无法关注', -1]
case 22015:
return ['账号异常', 2]
return [false, 2, '账号异常', 2]
default:
return [`未知错误\n${responseText}`, 1]
return [true, 1, `未知错误\n${responseText}`]
}
}),
/**