fix: 尝试兼容过🐎插件的event

This commit is contained in:
bietiaop 2024-08-23 13:12:48 +08:00
parent c6b5f7ab44
commit 069250f0ee
2 changed files with 20 additions and 2 deletions

View file

@ -17,9 +17,15 @@ export default class MysZZZApi extends MysApi {
super(uid, cookie, option, true); super(uid, cookie, option, true);
// 初始化 uid、server、apiTool // 初始化 uid、server、apiTool
this.uid = uid; this.uid = uid;
// 获取玩家的服务器
this.server = this.getServer(uid); this.server = this.getServer(uid);
// 初始化 apiTool
this.apiTool = new ZZZApiTool(uid, this.server); this.apiTool = new ZZZApiTool(uid, this.server);
// 绑定过🐎插件 (如果存在)
this.handler = option?.handler || {}; this.handler = option?.handler || {};
// 绑定yunzai event (如果存在)
this.e = option?.e || {};
// 获取 cookie 和设备 ID
if (typeof this.cookie !== 'string' && this.cookie) { if (typeof this.cookie !== 'string' && this.cookie) {
const ck = Object.values(this.cookie).find(item => { const ck = Object.values(this.cookie).find(item => {
return item.ck && item.uid === uid; return item.ck && item.uid === uid;
@ -30,6 +36,7 @@ export default class MysZZZApi extends MysApi {
this._device = ck?.device_id || ck?.device; this._device = ck?.device_id || ck?.device;
this.cookie = ck?.ck; this.cookie = ck?.ck;
} }
// 如果没有设备ID生成设备ID
if (!this._device) { if (!this._device) {
this._device = crypto.randomUUID(); this._device = crypto.randomUUID();
} }
@ -40,7 +47,9 @@ export default class MysZZZApi extends MysApi {
* @returns {string} * @returns {string}
*/ */
getServer() { getServer() {
// 获取 UID
const _uid = this.uid?.toString(); const _uid = this.uid?.toString();
// 如果 UID 长度小于 10说明是官服
if (_uid.length < 10) { if (_uid.length < 10) {
return 'prod_gf_cn'; // 官服 return 'prod_gf_cn'; // 官服
} }
@ -80,6 +89,7 @@ export default class MysZZZApi extends MysApi {
if (query) url += `?${query}`; if (query) url += `?${query}`;
// 如果传入了 query 参数,将 query 参数拼接到 url 上 // 如果传入了 query 参数,将 query 参数拼接到 url 上
if (data.query) { if (data.query) {
// 拼接 query
let str = ''; let str = '';
for (let key in data.query) { for (let key in data.query) {
if (data.query[key] === undefined) continue; if (data.query[key] === undefined) continue;
@ -90,7 +100,9 @@ export default class MysZZZApi extends MysApi {
}); });
} else str += `${key}=${data.query[key]}&`; } else str += `${key}=${data.query[key]}&`;
} }
// 去除最后一个 &
str = str.slice(0, -1); str = str.slice(0, -1);
// 拼接到 url 上
if (url.includes('?')) { if (url.includes('?')) {
url += `&${str}`; url += `&${str}`;
} else { } else {
@ -101,6 +113,7 @@ export default class MysZZZApi extends MysApi {
if (body) body = JSON.stringify(body); if (body) body = JSON.stringify(body);
// 获取请求头 // 获取请求头
let headers = this.getHeaders(query, body); let headers = this.getHeaders(query, body);
// 如果有设备指纹,写入设备指纹
if (data.deviceFp) { if (data.deviceFp) {
headers['x-rpc-device_fp'] = data.deviceFp; headers['x-rpc-device_fp'] = data.deviceFp;
// 兼容喵崽 // 兼容喵崽
@ -108,13 +121,15 @@ export default class MysZZZApi extends MysApi {
} }
// 写入 cookie // 写入 cookie
headers.cookie = this.cookie; headers.cookie = this.cookie;
// 写入设备ID // 写入设备ID(默认继承的)
if (this._device) { if (this._device) {
headers['x-rpc-device_id'] = this._device; headers['x-rpc-device_id'] = this._device;
} }
// 如果有设备ID写入设备ID传入的这里是绑定设备方法1中的设备ID
if (data.deviceId) { if (data.deviceId) {
headers['x-rpc-device_id'] = data.deviceId; headers['x-rpc-device_id'] = data.deviceId;
} }
// 如果有设备信息,写入设备信息
if (data?.deviceInfo && data?.modelName) { if (data?.deviceInfo && data?.modelName) {
const deviceBrand = data.deviceInfo?.split('/')[0]; const deviceBrand = data.deviceInfo?.split('/')[0];
try { try {
@ -133,6 +148,7 @@ export default class MysZZZApi extends MysApi {
} }
default: default:
} }
// 如果是获取 AuthKey写入额外参数
if (type === 'zzzAuthKey') { if (type === 'zzzAuthKey') {
let extra = { let extra = {
DS: this.getDS2(), DS: this.getDS2(),
@ -251,7 +267,7 @@ export default class MysZZZApi extends MysApi {
// 如果有注册的mys.req.err调用 // 如果有注册的mys.req.err调用
if (!!this?.handler && this?.handler?.has('mys.req.err')) { if (!!this?.handler && this?.handler?.has('mys.req.err')) {
logger.mark( logger.mark(
`[米游社绝区零查询失败][UID:${this.uid}][qq:${this.userId}] 遇到验证码,尝试调用 Handler mys.req.err` `[米游社绝区零查询失败][UID:${this.uid}][qq:${this?.e?.userId}] 遇到验证码,尝试调用 Handler mys.req.err`
); );
res = res =
(await this.handler.call('mys.req.err', this.e, { (await this.handler.call('mys.req.err', this.e, {

View file

@ -64,6 +64,7 @@ export class ZZZPlugin extends plugin {
* @returns {Promise<{api: MysZZZApi, uid: string, deviceFp: string}>} * @returns {Promise<{api: MysZZZApi, uid: string, deviceFp: string}>}
*/ */
async getAPI() { async getAPI() {
this.e.game = 'zzz';
// 直接调用获取 UID // 直接调用获取 UID
const uid = await this.getUID(); const uid = await this.getUID();
// 获取用户的 cookie // 获取用户的 cookie
@ -77,6 +78,7 @@ export class ZZZPlugin extends plugin {
// 创建米游社 API 对象 // 创建米游社 API 对象
const api = new MysZZZApi(uid, ck, { const api = new MysZZZApi(uid, ck, {
handler: this.e?.runtime?.handler || {}, handler: this.e?.runtime?.handler || {},
e: this.e,
}); });
const currentCK = Object.values(ck).find(item => { const currentCK = Object.values(ck).find(item => {
return item.ck && item.uid === uid; return item.ck && item.uid === uid;