From 6eaf8c0e904fd747251bd75b07a14a55acca68a9 Mon Sep 17 00:00:00 2001 From: bietiaop <1527109126@qq.com> Date: Fri, 16 Aug 2024 21:09:42 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=80=F0=9F=91=80=F0=9F=91=80?= =?UTF-8?q?=F0=9F=91=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/user.js | 10 +++---- lib/mysapi/tool.js | 23 ++++++++++++++++ lib/plugin.js | 67 +++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 88 insertions(+), 12 deletions(-) diff --git a/apps/user.js b/apps/user.js index e0a8a42..33a9729 100644 --- a/apps/user.js +++ b/apps/user.js @@ -33,8 +33,8 @@ export class Panel extends ZZZPlugin { ); } async toBindDevice() { - const uid = await this.getUID(); - if (!uid) { + const ltuid = await this.getLtuid(); + if (!ltuid) { this.reply('未绑定UID'); this.finish('toBindDevice'); return false; @@ -66,14 +66,14 @@ export class Panel extends ZZZPlugin { this.reply('设备信息格式错误', false, { at: true, recallMsg: 100 }); return false; } - await redis.del(`ZZZ:DEVICE_FP:${uid}:FP`); - await redis.set(`ZZZ:DEVICE_FP:${uid}:BIND`, JSON.stringify(info)); + await redis.del(`ZZZ:DEVICE_FP:${ltuid}:FP`); + await redis.set(`ZZZ:DEVICE_FP:${ltuid}:BIND`, JSON.stringify(info)); const { deviceFp } = await this.getAPI(); if (!deviceFp) { await this.reply('绑定设备失败'); return false; } - logger.debug(`[UID:${uid}]绑定设备成功,deviceFp:${deviceFp}`); + logger.debug(`[LTUID:${ltuid}]绑定设备成功,deviceFp:${deviceFp}`); await this.reply('绑定设备成功', false, { at: true, recallMsg: 100 }); } catch (error) { this.reply('设备信息格式错误', false, { at: true, recallMsg: 100 }); diff --git a/lib/mysapi/tool.js b/lib/mysapi/tool.js index 1ff5500..53d7226 100644 --- a/lib/mysapi/tool.js +++ b/lib/mysapi/tool.js @@ -20,6 +20,7 @@ export default class ZZZApiTool { this.host = 'https://api-takumi.mihoyo.com/'; this.hostRecord = 'https://api-takumi-record.mihoyo.com/'; this.hostPublicData = 'https://public-data-api.mihoyo.com/'; + this.hostBbs = 'https://bbs-api.miyoushe.com/'; } else { this.gameBiz = 'nap_global'; this.host = 'https://sg-public-api.hoyolab.com/'; @@ -104,6 +105,28 @@ export default class ZZZApiTool { }, noDs: true, }, + deviceLogin: { + url: `${this.hostBbs}apihub/api/deviceLogin`, + body: { + app_version: '2.73.1', + device_id: data.deviceId, + device_name: `${deviceBrand}${modelName}`, + os_version: '33', + platform: 'Android', + registration_id: generateSeed(19), + }, + }, + saveDevice: { + url: `${this.hostBbs}apihub/api/saveDevice`, + body: { + app_version: '2.73.1', + device_id: data.deviceId, + device_name: `${deviceBrand}${modelName}`, + os_version: '33', + platform: 'Android', + registration_id: generateSeed(19), + }, + }, } : { getFp: { diff --git a/lib/plugin.js b/lib/plugin.js index f423289..e8dce14 100644 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -3,7 +3,7 @@ import { getCk } from './common.js'; import _ from 'lodash'; import NoteUser from '../../genshin/model/mys/NoteUser.js'; import settings from '../lib/settings.js'; -import fetch from 'node-fetch'; +import request from '../utils/request.js'; export class ZZZPlugin extends plugin { /** @@ -37,6 +37,30 @@ export class ZZZPlugin extends plugin { // 返回 UID return uid; } + + /** + * 获取用户 ltuid + * @returns {Promise} + * @memberof ZZZPlugin + * @description 获取用户 ltuid + * @example + * const ltuid = await this.getLtuid(); + * if (!ltuid) return false; + * @returns {Promise} + */ + async getLtuid() { + const uid = await this.getUID(); + if (!uid) return false; + const ck = await getCk(this.e); + if (!ck || Object.keys(ck).filter(k => ck[k].ck).length === 0) { + await this.reply('尚未绑定cookie,请先绑定cookie,或者#扫码登录'); + return false; + } + const currentCK = Object.values(ck).find(item => { + return item.ck && item.uid === uid; + }); + return currentCK?.ltuid || ''; + } /** * 获取米游社 API * @returns {Promise<{api: MysZZZApi, uid: string, deviceFp: string}>} @@ -55,11 +79,18 @@ export class ZZZPlugin extends plugin { try { // 创建米游社 API 对象 const api = new MysZZZApi(uid, ck); + const currentCK = Object.values(ck).find(item => { + return item.ck && item.uid === uid; + }); + const ltuid = currentCK?.ltuid || ''; + if (!ltuid) { + return { api: null, uid: null, deviceFp: null }; + } // 获取设备指纹 - let deviceFp = await redis.get(`ZZZ:DEVICE_FP:${uid}:FP`); + let deviceFp = await redis.get(`ZZZ:DEVICE_FP:${ltuid}:FP`); if (!deviceFp) { const sdk = api.getUrl('getFp'); - const res = await fetch(sdk.url, { + const res = await request(sdk.url, { headers: sdk.headers, method: 'POST', body: sdk.body, @@ -67,10 +98,32 @@ export class ZZZPlugin extends plugin { const fpRes = await res.json(); logger.debug(`[米游社][设备指纹]${JSON.stringify(fpRes)}`); deviceFp = fpRes?.data?.device_fp; - if (deviceFp) { - await redis.set(`ZZZ:DEVICE_FP:${uid}:FP`, deviceFp, { - EX: 86400 * 7, - }); + if (!deviceFp) { + return { api: null, uid: null, deviceFp: null }; + } + await redis.set(`ZZZ:DEVICE_FP:${ltuid}:FP`, deviceFp, { + EX: 86400 * 7, + }); + const deviceLogin = api.getUrl('deviceLogin'); + const saveDevice = api.getUrl('saveDevice'); + if (!!deviceLogin && !!saveDevice) { + logger.debug(`[米游社][设备登录]保存设备信息`); + try { + const login = await request(deviceLogin.url, { + headers: deviceLogin.headers, + method: 'POST', + body: deviceLogin.body, + }); + const save = await request(saveDevice.url, { + headers: saveDevice.headers, + method: 'POST', + body: saveDevice.body, + }); + const result = await Promise.all([login.json(), save.json()]); + logger.debug(`[米游社][设备登录]${JSON.stringify(result)}`); + } catch (error) { + logger.error(`[米游社][设备登录]${error.message}`); + } } } // 返回数据(API、UID、设备指纹)