This commit is contained in:
bietiaop 2024-07-11 16:48:51 +08:00
parent 0c80a5ab9c
commit 512ced6305
2 changed files with 50 additions and 23 deletions

View file

@ -73,12 +73,16 @@ export class GachaLog extends ZZZPlugin {
async refreshGachaLog() { async refreshGachaLog() {
const uid = await this.getUID(); const uid = await this.getUID();
if (!uid) return false; if (!uid) return false;
const key = await getAuthKey(this.e, uid); try {
if (!key) { const key = await getAuthKey(this.e, this.User, uid);
await this.reply('authKey获取失败请检查cookie是否过期'); if (!key) {
return false; await this.reply('authKey获取失败请检查cookie是否过期');
return false;
}
this.getLog(key);
} catch (error) {
await this.reply(error.message);
} }
this.getLog(key);
} }
async getLog(key) { async getLog(key) {
const uid = await this.getUID(); const uid = await this.getUID();

View file

@ -1,5 +1,6 @@
import fetch from 'node-fetch'; import fs from 'fs';
import MysZZZApi from './mysapi.js'; import MysZZZApi from './mysapi.js';
import YAML from 'yaml';
let User; let User;
try { try {
User = (await import('../../xiaoyao-cvs-plugin/model/user.js')).default; User = (await import('../../xiaoyao-cvs-plugin/model/user.js')).default;
@ -7,18 +8,52 @@ try {
logger.warn('建议安装逍遥插件以获得更佳体验'); 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<void>} * @returns {Promise<void>}
*/ */
export async function getAuthKey(e, zzzUid, authAppid = 'csc') { export async function getAuthKey(e, _user, zzzUid, authAppid = 'csc') {
if (!User) { if (!User) {
throw new Error('未安装逍遥插件,无法自动刷新抽卡链接'); throw new Error('未安装逍遥插件,无法自动刷新抽卡链接');
} }
logger.mark(zzzUid); const uidData = _user.getUidData(zzzUid, 'zzz', e);
const user = new User(e); if (!uidData || uidData?.type != 'ck' || !uidData?.ltuid) {
await user.getCookie(e); throw new Error(`当前UID${zzzUid}未绑定cookie请切换UID后尝试`);
let ck = await user.getStoken(e.user_id); }
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};`; ck = `stuid=${ck.stuid};stoken=${ck.stoken};mid=${ck.mid};`;
const api = new MysZZZApi(zzzUid, ck); const api = new MysZZZApi(zzzUid, ck);
let type = 'zzzAuthKey'; let type = 'zzzAuthKey';
@ -40,15 +75,3 @@ export async function getAuthKey(e, zzzUid, authAppid = 'csc') {
res = await res.json(); res = await res.json();
return res?.data?.authkey; 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;
}