From 512ced6305d0d8cb6575b5b581371e3c92792649 Mon Sep 17 00:00:00 2001 From: bietiaop <1527109126@qq.com> Date: Thu, 11 Jul 2024 16:48:51 +0800 Subject: [PATCH] fix: sk --- apps/gachalog.js | 14 ++++++++---- lib/authkey.js | 59 +++++++++++++++++++++++++++++++++--------------- 2 files changed, 50 insertions(+), 23 deletions(-) diff --git a/apps/gachalog.js b/apps/gachalog.js index 4d176d6..93d29f2 100644 --- a/apps/gachalog.js +++ b/apps/gachalog.js @@ -73,12 +73,16 @@ export class GachaLog extends ZZZPlugin { async refreshGachaLog() { const uid = await this.getUID(); if (!uid) return false; - const key = await getAuthKey(this.e, uid); - if (!key) { - await this.reply('authKey获取失败,请检查cookie是否过期'); - return false; + try { + const key = await getAuthKey(this.e, this.User, uid); + if (!key) { + await this.reply('authKey获取失败,请检查cookie是否过期'); + return false; + } + this.getLog(key); + } catch (error) { + await this.reply(error.message); } - this.getLog(key); } async getLog(key) { const uid = await this.getUID(); diff --git a/lib/authkey.js b/lib/authkey.js index f75dccc..bab151b 100644 --- a/lib/authkey.js +++ b/lib/authkey.js @@ -1,5 +1,6 @@ -import fetch from 'node-fetch'; +import fs from 'fs'; import MysZZZApi from './mysapi.js'; +import YAML from 'yaml'; let User; try { User = (await import('../../xiaoyao-cvs-plugin/model/user.js')).default; @@ -7,18 +8,52 @@ try { logger.warn('建议安装逍遥插件以获得更佳体验'); } +/** + * 获取 Stoken + * @param {*} e yunzai Event + * @param {string} mysid 米游社ID + * @returns + */ +export function getStoken(e, mysid = '') { + let userId = e.user_id; + let user = new User(e); + let file = `${user.stokenPath}${userId}.yaml`; + try { + let cks = fs.readFileSync(file, 'utf-8'); + cks = YAML.parse(cks); + for (let ck in cks) { + if (cks[ck]['stuid'] === mysid) { + return cks[ck]; + } + return null; + } + } catch (error) { + logger.debug(`[zzz:error]getStoken`, error); + return null; + } +} + /** * 此方法依赖逍遥插件 * @returns {Promise} */ -export async function getAuthKey(e, zzzUid, authAppid = 'csc') { +export async function getAuthKey(e, _user, zzzUid, authAppid = 'csc') { if (!User) { throw new Error('未安装逍遥插件,无法自动刷新抽卡链接'); } - logger.mark(zzzUid); - const user = new User(e); - await user.getCookie(e); - let ck = await user.getStoken(e.user_id); + const uidData = _user.getUidData(zzzUid, 'zzz', e); + if (!uidData || uidData?.type != 'ck' || !uidData?.ltuid) { + throw new Error(`当前UID${zzzUid}未绑定cookie,请切换UID后尝试`); + } + let ck = getStoken(e, uidData.ltuid); + if (!ck) { + throw new Error('获取cookie失败,请确认绑定了 cookie'); + } + if (uidData.ltuid !== ck.stuid) { + throw new Error( + `当前UID${zzzUid}查询所使用的米游社ID${ck.stuid}与当前切换的米游社ID${uidData.ltuid}不匹配,请切换UID后尝试` + ); + } ck = `stuid=${ck.stuid};stoken=${ck.stoken};mid=${ck.mid};`; const api = new MysZZZApi(zzzUid, ck); let type = 'zzzAuthKey'; @@ -40,15 +75,3 @@ export async function getAuthKey(e, zzzUid, authAppid = 'csc') { res = await res.json(); return res?.data?.authkey; } - -export async function getStoken(e) { - if (!User) { - throw new Error('未安装逍遥插件,无法自动刷新抽卡链接'); - } - let user = new User(e); - // set uid - await user.getCookie(e); - let ck = await user.getStoken(e.user_id); - ck = `stuid=${ck.stuid};stoken=${ck.stoken};mid=${ck.mid};`; - return ck; -}