refactor: 重构lib代码(无实质性功能更新,可不更新)

This commit is contained in:
bietiaop 2024-08-22 13:34:08 +08:00
parent a7f06d404b
commit aa3b7928ec
26 changed files with 547 additions and 483 deletions

View file

@ -4,6 +4,7 @@ import crypto from 'crypto';
import ZZZApiTool from './mysapi/tool.js';
import { randomString } from '../utils/data.js';
import MysApi from '../../genshin/model/mys/mysApi.js';
import { MysError } from './error.js';
// const DEVICE_ID = randomString(32).toUpperCase()
// const DEVICE_NAME = randomString(_.random(1, 10));
@ -18,6 +19,7 @@ export default class MysZZZApi extends MysApi {
this.uid = uid;
this.server = this.getServer(uid);
this.apiTool = new ZZZApiTool(uid, this.server);
this.handler = option?.handler || {};
if (typeof this.cookie !== 'string' && this.cookie) {
const ck = Object.values(this.cookie).find(item => {
return item.ck && item.uid === uid;
@ -239,81 +241,32 @@ export default class MysZZZApi extends MysApi {
* @param data 查询请求的数据
* @returns {Promise<*|boolean>}
*/
async checkCode(e, res, type, data = {}) {
if (!res || !e) {
e.reply('米游社接口请求失败,暂时无法查询');
return false;
async checkCode(res, type, data = {}) {
if (!res) {
throw new Error('米游社接口返回数据为空');
}
this.e = e;
this.e.isZZZ = true;
res.retcode = Number(res.retcode);
switch (res.retcode) {
case 0:
break;
case 10102:
if (res.message === 'Data is not public for the user') {
this.e.reply(`\nUID:${this.uid},米游社数据未公开`, false, {
at: this.userId,
});
} else {
this.e.reply(`UID:${this.uid},请先去米游社绑定角色`);
}
break;
case 10041:
case 5003:
this.e.reply(
`UID:${this.uid},米游社账号异常,暂时无法查询,请发送 %绑定设备帮助 查看如何绑定设备`
const code = String(res.retcode);
if (code === '1034' || code === '10035') {
// 如果有注册的mys.req.err调用
if (!!this?.handler && this?.handler?.has('mys.req.err')) {
logger.mark(
`[米游社绝区零查询失败][UID:${this.uid}][qq:${this.userId}] 遇到验证码,尝试调用 Handler mys.req.err`
);
break;
case 10035:
case 1034: {
let handler = this.e.runtime?.handler || {};
// 如果有注册的mys.req.err调用
if (handler.has('mys.req.err')) {
logger.mark(
`[米游社zzz查询失败][UID:${this.uid}][qq:${this.userId}] 遇到验证码,尝试调用 Handler mys.req.err`
);
res =
(await handler.call('mys.req.err', this.e, {
mysApi: this,
type,
res,
data,
mysInfo: this,
})) || res;
}
if (!res || res?.retcode === 1034 || res?.retcode === 10035) {
logger.mark(
`[米游社zzz查询失败][UID:${this.uid}][qq:${this.userId}] 遇到验证码`
);
this.e.reply('米游社zzz查询遇到验证码请稍后再试');
}
break;
res =
(await this.handler.call('mys.req.err', this.e, {
mysApi: this,
type,
res,
data,
mysInfo: this,
})) || res;
}
default:
if (/(登录|login)/i.test(res.message)) {
logger.mark(`[ck失效][UID:${this.uid}]`);
this.e.reply(`UID:${this.uid}米游社cookie已失效`);
} else {
this.e.reply(
`米游社接口报错,暂时无法查询:${res.message || 'error'}`
);
}
break;
}
if (res.retcode !== 0) {
logger.mark(
`[米游社zzz接口报错]${JSON.stringify(res)}UID${this.uid}`
);
throw new Error({
type: 'mysApi',
uid: this.uid,
retcode: res.retcode,
result: res,
});
if (code === '0') {
return res;
}
return res;
throw new MysError(code, this.uid, res);
}
/**
@ -323,8 +276,8 @@ export default class MysZZZApi extends MysApi {
* @param {{deviceFp?: string; query: Record<string, any>; headers: object;}} data
* @param {boolean} cached
*/
async getFinalData(e, type, data = {}, cached = false) {
if (!data.headers) data.headers = {};
async getFinalData(type, data = {}, cached = false) {
if (!data?.headers) data.headers = {};
if (data.deviceFp) {
data.headers['x-rpc-device_fp'] = data.deviceFp;
}
@ -364,7 +317,7 @@ export default class MysZZZApi extends MysApi {
}
}
const result = await this.getData(type, data, cached);
const _data = await this.checkCode(e, result, type, {});
const _data = await this.checkCode(result, type, {});
if (!_data || _data.retcode !== 0) return false;
return _data.data;
}