Skip to content

Commit

Permalink
optimize: code
Browse files Browse the repository at this point in the history
  • Loading branch information
snowtafir committed Dec 16, 2024
1 parent ac9f46f commit 45f2467
Show file tree
Hide file tree
Showing 15 changed files with 158 additions and 127 deletions.
52 changes: 27 additions & 25 deletions apps/bilibili.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export default class YukiBili extends plugin {
}
const { has_more, items } = data || {};

let infoName: string;
let infoName: string = '';
if (code === 0 && has_more === false) {
this.e.reply(`检测到该uid的主页空间动态内容为空,\n执行uid:${uid} 校验...`);

Expand All @@ -190,7 +190,7 @@ export default class YukiBili extends plugin {
}
}

let name: string | number;
let name: string | number = '';
if (Array.isArray(items)) {
if (items.length > 0) {
name = items[0].modules?.module_author?.name || uid;
Expand Down Expand Up @@ -281,26 +281,28 @@ export default class YukiBili extends plugin {
if (this.e.isMaster) {
try {
const token = await applyLoginQRCode(this.e);

let biliLoginCk = await pollLoginQRCode(this.e, token);

if (lodash.trim(biliLoginCk).length != 0) {
await saveLoginCookie(this.e, biliLoginCk);
this.e.reply(`get bilibili LoginCk:成功!`);
const result = await postGateway(biliLoginCk); //激活ck

const { code, data } = await result.data; // 解析校验结果

switch (code) {
case 0:
(logger ?? Bot.logger)?.mark(`优纪插件:获取biliLoginCK,Gateway校验成功:${JSON.stringify(data)}`);
break;
default:
(logger ?? Bot.logger)?.mark(`优纪插件:获取biliLoginCK,Gateway校验失败:${JSON.stringify(data)}`);
break;
if (token) {
let biliLoginCk = await pollLoginQRCode(this.e, token);
if (biliLoginCk) {
if (lodash.trim(biliLoginCk).length != 0) {
await saveLoginCookie(this.e, biliLoginCk);
this.e.reply(`get bilibili LoginCk:成功!`);
const result = await postGateway(biliLoginCk); //激活ck

const { code, data } = await result.data; // 解析校验结果

switch (code) {
case 0:
(logger ?? Bot.logger)?.mark(`优纪插件:获取biliLoginCK,Gateway校验成功:${JSON.stringify(data)}`);
break;
default:
(logger ?? Bot.logger)?.mark(`优纪插件:获取biliLoginCK,Gateway校验失败:${JSON.stringify(data)}`);
break;
}
} else {
this.e.reply(`get bilibili LoginCk:失败X﹏X`);
}
}
} else {
this.e.reply(`get bilibili LoginCk:失败X﹏X`);
}
} catch (Error) {
(logger ?? Bot.logger)?.info(`yuki-plugin Login bilibili Failed:${Error}`);
Expand Down Expand Up @@ -350,7 +352,7 @@ export default class YukiBili extends plugin {
if (!param.buvid3 || !param._uuid || !param.buvid4 || !param.DedeUserID) {
await this.e.reply('发送的cookie字段缺失\n请添加完整cookie\n获取方法查看仓库主页。');

const missingCookies = [];
const missingCookies: string[] = [];
if (!param.buvid3 || param.buvid3.length === 0) {
missingCookies.push('buvid3');
}
Expand Down Expand Up @@ -472,7 +474,7 @@ export default class YukiBili extends plugin {
} else {
let subData = this.biliPushData || { group: {}, private: {} };

const messages = [];
const messages: string[] = [];

const typeMap = {
DYNAMIC_TYPE_AV: '视频',
Expand Down Expand Up @@ -533,7 +535,7 @@ export default class YukiBili extends plugin {
async singelSubDynamicPushList() {
let subData = this.biliPushData || { group: {}, private: {} };

const messages = [];
const messages: string[] = [];

const typeMap = {
DYNAMIC_TYPE_AV: '视频',
Expand Down Expand Up @@ -636,7 +638,7 @@ export default class YukiBili extends plugin {
return;
}

const messages = [];
const messages: string[] = [];

for (let index = 0; index < Math.min(data.result.length, 5); index++) {
const item: { uname: string; mid: number; fans: number } = data.result[index];
Expand Down
2 changes: 1 addition & 1 deletion apps/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export default class YukiHelp extends plugin {
} else {
return;
}
let msg = [];
let msg: any[] = [];
msg.push(segment.image(imgRes.img[0]));
await this.e.reply(msg);
}
Expand Down
2 changes: 1 addition & 1 deletion apps/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export default class YukiVersion extends plugin {
} else {
return;
}
let msg = [];
let msg: any[] = [];
msg.push(segment.image(imgRes.img[0]));
await this.e.reply(msg);
}
Expand Down
6 changes: 3 additions & 3 deletions apps/weibo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ export default class YukiWeibo extends plugin {
} else {
let subData = this.weiboPushData || { group: {}, private: {} };

const messages = [];
const messages: string[] = [];

const typeMap = {
DYNAMIC_TYPE_AV: '视频',
Expand Down Expand Up @@ -262,7 +262,7 @@ export default class YukiWeibo extends plugin {
async singelSubDynamicPushList() {
let subData = this.weiboPushData || { group: {}, private: {} };

const messages = [];
const messages: string[] = [];

const typeMap = {
DYNAMIC_TYPE_AV: '视频',
Expand Down Expand Up @@ -360,7 +360,7 @@ export default class YukiWeibo extends plugin {
return;
}

const messages = [];
const messages: string[] = [];

messages.push(
`-----微博-----
Expand Down
31 changes: 18 additions & 13 deletions models/bilibili/bilibili.main.models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@ export async function applyLoginQRCode(e: any) {
throw new Error(`获取B站登录二维码URL网络请求失败,状态码: ${response.status}`);
}

const res: {
const res = (await response.json()) as {
code?: number;
message?: string;
ttl?: number;
data?: {
url?: string;
qrcode_key?: string;
};
} = await response.json();
};

if (res?.code === 0) {
const qrcodeKey = res.data.qrcode_key;
const qrcodeUrl = res.data.url;
const qrcodeKey = res?.data?.qrcode_key;
const qrcodeUrl = res?.data?.url;
let loginUrlQrcodeData = await QRCode.toDataURL(`${qrcodeUrl}`);
const LoginPropsData: LoginProps = {
data: { url: loginUrlQrcodeData }
Expand All @@ -60,7 +60,7 @@ export async function applyLoginQRCode(e: any) {
const { img } = qrCodeImage;
qrCodeBufferArray = img;
}
let msg = [];
let msg: string[] = [];
if (qrCodeBufferArray.length === 0) {
msg.push('渲染二维码图片失败,请查看终端输出的实时日志,\n复制哔哩登陆二维码URL,使用在线或本地二维码生成工具生成二维码并扫码。');
} else {
Expand Down Expand Up @@ -91,7 +91,7 @@ export async function pollLoginQRCode(e: any, qrcodeKey: string) {
throw new Error(`处理B站登录token网络请求失败,状态码: ${response.status}`);
}

const data: {
const data = (await response.json()) as {
code?: number;
message?: string;
ttl?: number;
Expand All @@ -102,13 +102,13 @@ export async function pollLoginQRCode(e: any, qrcodeKey: string) {
code?: number;
message?: string;
};
} = await response.json();
};

if (data.code === 0) {
if (data.data.code === 0) {
if (data?.data?.code === 0) {
// 登录成功,获取 cookie
const LoginCookie = response.headers.get('set-cookie');
let loginCk: string = '';
let loginCk: string | null = '';
try {
const nomalCk = await getNewTempCk();
loginCk = `${nomalCk}${LoginCookie}`;
Expand All @@ -118,18 +118,18 @@ export async function pollLoginQRCode(e: any, qrcodeKey: string) {
}
e.reply(`~B站登陆成功~`);
return loginCk;
} else if (data.data.code === 86101) {
} else if (data?.data?.code === 86101) {
// 未扫码
// 继续轮询
await new Promise(resolve => setTimeout(resolve, 2000));
(logger ?? Bot.logger)?.mark(`优纪插件:扫码B站登录:未扫码,轮询中...`);
return pollLoginQRCode(e, qrcodeKey);
} else if (data.data.code === 86090) {
} else if (data?.data?.code === 86090) {
// 已扫码未确认
// 继续轮询
await new Promise(resolve => setTimeout(resolve, 2000));
return pollLoginQRCode(e, qrcodeKey);
} else if (data.data.code === 86038) {
} else if (data?.data?.code === 86038) {
// 二维码已失效
e.reply('B站登陆二维码已失效');
return null;
Expand Down Expand Up @@ -532,7 +532,12 @@ export async function cookieWithBiliTicket(cookie: string): Promise<string> {
const csrf = await readSavedCookieItems(cookie, ['bili_jct'], false);
const { ticket, ttl } = await getBiliTicket(csrf);
await redis.set(BiliJctKey, ticket, { EX: ttl });
return cookie + `;bili_ticket=${ticket};`;
if (ticket && ttl) {
await redis.set(BiliJctKey, ticket, { EX: ttl });
return cookie + `;bili_ticket=${ticket};`;
} else {
return cookie;
}
} catch (error) {
logger?.error(`${error}`);
return cookie;
Expand Down
Loading

0 comments on commit 45f2467

Please sign in to comment.