typo: 代码注释

This commit is contained in:
bietiaop 2024-07-15 14:25:42 +08:00
parent 51b3908afd
commit 4c90ca5354
12 changed files with 457 additions and 139 deletions

View file

@ -6,49 +6,62 @@ import settings from '../lib/settings.js';
export class ZZZPlugin extends plugin {
/**
*
* @returns {Promise<string>}
* 获取用户 UID如果需要同时获取API可以直接调用 getAPI
* @returns {Promise<string | boolean>}
*/
async getUID() {
// 默认为当前用户
let user = this.e;
// 获取配置
const query = settings.getConfig('config').query;
const allow = _.get(query, 'others', true);
// 如果 at 存在且允许查看其他用户
if (this.e.at && allow) {
// 将当前用户的 user_id 设置为 at 对象的 user_id
this.e.user_id = this.e.at;
// 将当前用户设置为 at 对象
user = this.e.at;
}
// 获取用户信息(米游社),因此这里会导致查询一次米游社的信息
this.User = await NoteUser.create(user);
// let uid = this.e.msg.match(/\d+/)?.[0];
// 获取用户 UID
const uid = this.User?.getUid('zzz');
// 如果 UID 不存在,说明没有绑定 cookie
if (!uid) {
await this.reply('uid为空米游社查询请先绑定cookie其他查询请携带uid');
return false;
}
// 返回 UID
return uid;
}
/**
*
* 获取米游社 API
* @returns {Promise<{api: MysZZZApi, uid: string, deviceFp: string}>}
*/
async getAPI() {
let uid = await this.getUID();
// 直接调用获取 UID
const uid = await this.getUID();
if (!uid) return false;
// 获取用户的 cookie
const ck = await getCk(this.e);
// 如果 cookie 不存在或者 cookie 为空,说明没有绑定 cookie
if (!ck || Object.keys(ck).filter(k => ck[k].ck).length === 0) {
await this.reply('尚未绑定cookie请先绑定cookie');
return false;
}
// 创建米游社 API 对象
const api = new MysZZZApi(uid, ck);
// 获取设备指纹
let deviceFp = await redis.get(`ZZZ:DEVICE_FP:${uid}`);
if (!deviceFp) {
let sdk = api.getUrl('getFp');
let res = await fetch(sdk.url, {
const sdk = api.getUrl('getFp');
const res = await fetch(sdk.url, {
headers: sdk.headers,
method: 'POST',
body: sdk.body,
});
let fpRes = await res.json();
const fpRes = await res.json();
deviceFp = fpRes?.data?.device_fp;
if (deviceFp) {
await redis.set(`ZZZ:DEVICE_FP:${uid}`, deviceFp, {
@ -56,19 +69,25 @@ export class ZZZPlugin extends plugin {
});
}
}
// 返回数据API、UID、设备指纹
return { api, uid, deviceFp };
}
/**
*
* 获取玩家信息当调用此方法时会获取用户的玩家信息并将其保存到`e.playerCard`方便渲染用户信息此部分请查阅`lib/render.js`中两个模块的作用
* @returns {Promise<boolean | object>}
*/
async getPlayerInfo() {
// 获取 米游社 API
const { api } = await this.getAPI();
if (!api) return false;
// 获取用户信息
let userData = await api.getFinalData(this.e, 'zzzUser');
if (!userData) return false;
// 取第一个用户信息
userData = userData?.list[0];
// 获取用户头像
let avatar = this.e.bot.avatar;
if (this.e?.user_id) {
avatar = `https://q1.qlogo.cn/g?b=qq&s=0&nk=${this.e.user_id}`;
@ -77,10 +96,12 @@ export class ZZZPlugin extends plugin {
} else if (this.e.friend?.getAvatarUrl) {
avatar = await this.e.friend.getAvatarUrl();
}
// 写入数据
this.e.playerCard = {
avatar: avatar,
player: userData,
};
// 返回数据
return userData;
}
}