🎨 优化错误处理

This commit is contained in:
目棃
2024-11-17 08:59:15 +08:00
parent 6b66467cff
commit c7b5bf34ef
4 changed files with 53 additions and 60 deletions

View File

@@ -1,7 +1,7 @@
/**
* @file plugins/Mys/request/postReq.ts
* @description 帖子相关的获取
* @since Beta v0.6.2
* @since Beta v0.6.3
*/
import TGHttp from "../../../utils/TGHttp.js";
@@ -35,18 +35,20 @@ export async function getForumPostList(
/**
* @description 获取单个帖子信息
* @since Beta v0.6.2
* @since Beta v0.6.3
* @param {number} postId 帖子 ID
* @return {Promise<TGApp.Plugins.Mys.Post.FullData>}
* @return {Promise<TGApp.Plugins.Mys.Post.FullData|TGApp.BBS.Response.Base>}
*/
export async function getPostFull(postId: number): Promise<TGApp.Plugins.Mys.Post.FullData> {
return (
await TGHttp<TGApp.Plugins.Mys.Post.Response>(`${Mpabu}getPostFull`, {
method: "GET",
headers: { referer: Referer },
query: { post_id: postId },
})
).data.post;
export async function getPostFull(
postId: number,
): Promise<TGApp.Plugins.Mys.Post.FullData | TGApp.BBS.Response.Base> {
const resp = await TGHttp<TGApp.Plugins.Mys.Post.Response>(`${Mpabu}getPostFull`, {
method: "GET",
headers: { referer: Referer },
query: { post_id: postId },
});
if (resp.retcode !== 0) return <TGApp.BBS.Response.Base>resp;
return resp.data.post;
}
/**

View File

@@ -1,15 +1,16 @@
/**
* @file plugins/Mys/utils/getGachaCard.ts
* @description Mys 插件抽卡工具
* @since Beta v0.6.2
* @since Beta v0.6.3
*/
import showSnackbar from "../../../components/func/snackbar.js";
import { AppCharacterData } from "../../../data/index.js";
import { getPostFull } from "../request/postReq.js";
/**
* @description 根据单个卡池信息转为渲染用的卡池信息
* @since Beta v0.6.2
* @since Beta v0.6.3
* @param {TGApp.Plugins.Mys.Gacha.Data} data 卡池信息
* @param {string} poolCover 卡池封面
* @returns {Promise<TGApp.Plugins.Mys.Gacha.RenderCard>}
@@ -17,36 +18,28 @@ import { getPostFull } from "../request/postReq.js";
async function getGachaItemCard(
data: TGApp.Plugins.Mys.Gacha.Data,
poolCover?: string,
): Promise<TGApp.Plugins.Mys.Gacha.RenderCard> {
): Promise<TGApp.Plugins.Mys.Gacha.RenderCard | null> {
let cover = "/source/UI/empty.webp";
const postId: number | undefined = Number(data.activity_url.split("/").pop()) || undefined;
if (postId === undefined || isNaN(postId)) {
throw new Error("无法获取帖子 ID");
}
if (postId === undefined || isNaN(postId)) return null;
if (poolCover !== undefined) {
cover = poolCover;
} else {
try {
console.log("调用 getPostData");
const post = await getPostFull(postId);
cover = post.cover?.url ?? post.post.images[0];
} catch (error) {
console.error(error);
const postResp = await getPostFull(postId);
if ("retcode" in postResp) {
showSnackbar.error(`[${postResp.retcode}] ${postResp.message}`);
return null;
}
cover = postResp.cover?.url ?? postResp.post.images[0];
}
const timeStr = `${data.start_time} ~ ${data.end_time}`;
const characters: TGApp.Plugins.Mys.Gacha.RenderItem[] = [];
for (const character of data.pool) {
const item: TGApp.Plugins.Mys.Gacha.RenderItem = {
icon: character.icon,
url: character.url,
};
const item: TGApp.Plugins.Mys.Gacha.RenderItem = { icon: character.icon, url: character.url };
const contentId = character.url.match(/(?<=content\/)\d+/)?.[0];
if (contentId) {
const itemF = AppCharacterData.find((item) => item.contentId.toString() === contentId);
if (itemF) {
item.info = itemF;
}
if (itemF) item.info = itemF;
}
characters.push(item);
}
@@ -67,7 +60,7 @@ async function getGachaItemCard(
/**
* @description 根据卡池信息转为渲染用的卡池信息
* @since Beta v0.4.4
* @since Beta v0.6.3
* @param {TGApp.Plugins.Mys.Gacha.Data[]} gachaData 卡池信息
* @param {Record<number, string>} poolCover 卡池封面
* @returns {Promise<TGApp.Plugins.Mys.Gacha.RenderCard[]>}
@@ -80,7 +73,7 @@ export async function getGachaCard(
await Promise.allSettled(
gachaData.map(async (data: TGApp.Plugins.Mys.Gacha.Data) => {
const item = await getGachaItemCard(data, poolCover?.[Number(data.id)]);
gachaCard.push(item);
if (item !== null) gachaCard.push(item);
}),
);
return gachaCard;