🎨 优化错误处理

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;

View File

@@ -8,18 +8,19 @@
</div>
</template>
<script lang="ts" setup>
import { onMounted, reactive, ref } from "vue";
import { onMounted, ref } from "vue";
import JsonViewer from "vue-json-viewer";
import { useRoute } from "vue-router";
import TSwitchTheme from "../components/app/t-switchTheme.vue";
import showLoading from "../components/func/loading.js";
import showSnackbar from "../components/func/snackbar.js";
import Mys from "../plugins/Mys/index.js";
import TGLogger from "../utils/TGLogger.js";
const postId = Number(useRoute().params.post_id);
let jsonData = reactive<TGApp.Plugins.Mys.Post.FullData>(<TGApp.Plugins.Mys.Post.FullData>{});
let parseData = reactive<TGApp.Plugins.Mys.SctPost.Base[]>([]);
const jsonData = ref<TGApp.Plugins.Mys.Post.FullData>();
const parseData = ref<TGApp.Plugins.Mys.SctPost.Base[]>();
const isEmpty = ref<boolean>(false);
onMounted(async () => {
@@ -28,18 +29,19 @@ onMounted(async () => {
showLoading.empty("错误的帖子ID");
return;
}
try {
jsonData = await Mys.Post.getPostFull(postId);
} catch (e) {
showLoading.empty("获取数据失败", `帖子ID:${postId}`);
await TGLogger.Error(`[${postId}]获取帖子数据失败:${e}`);
const resp = await Mys.Post.getPostFull(postId);
if ("retcode" in resp) {
showLoading.empty("获取数据失败", `[${resp.retcode}]${resp.message}`);
showSnackbar.error(`[${resp.retcode}]${resp.message}`);
await TGLogger.Error(`[${postId}]获取帖子数据失败:${resp.retcode} ${resp.message}`);
return;
}
jsonData.value = resp;
try {
parseData = JSON.parse(jsonData.post.content);
parseData.value = JSON.parse(jsonData.value.post.content);
} catch (err) {
try {
parseData = JSON.parse(jsonData.post.structured_content);
parseData.value = JSON.parse(jsonData.value.post.structured_content);
} catch (e) {
isEmpty.value = true;
await TGLogger.Error(`[${postId}]解析帖子数据失败:${e}`);

View File

@@ -94,6 +94,7 @@ import TPinWin from "../components/app/t-pinWin.vue";
import TShareBtn from "../components/app/t-shareBtn.vue";
import TSwitchTheme from "../components/app/t-switchTheme.vue";
import showLoading from "../components/func/loading.js";
import showSnackbar from "../components/func/snackbar.js";
import TbCollect from "../components/post/tb-collect.vue";
import TpAvatar from "../components/post/tp-avatar.vue";
import TpParser from "../components/post/tp-parser.vue";
@@ -139,26 +140,21 @@ onMounted(async () => {
return;
}
showLoading.update("正在获取数据...", `帖子ID: ${postId}`);
try {
postData.value = await Mys.Post.getPostFull(postId);
showLoading.update("正在渲染数据...", `帖子ID: ${postId}`);
renderPost.value = getRenderPost(postData.value);
shareTitle.value = `Post_${postId}`;
await webviewWindow
.getCurrentWebviewWindow()
.setTitle(`Post_${postId} ${postData.value.post.subject}`);
} catch (error) {
if (error instanceof Error) {
await TGLogger.Error(`[t-post][${postId}] ${error.name}: ${error.message}`);
showLoading.update(error.name, error.message, true);
} else {
console.error(error);
await TGLogger.Error(`[t-post][${postId}] 未知错误${JSON.stringify(error)}`);
showLoading.empty("帖子不存在或解析失败", "请检查帖子是否存在或者是否为合法的帖子");
}
await webviewWindow.getCurrentWebviewWindow().setTitle(`Post_${postId} Parsing Error`);
const resp = await Mys.Post.getPostFull(postId);
if ("retcode" in resp) {
showLoading.empty(`[code]${resp.retcode}`, resp.message);
showSnackbar.error(`[${resp.retcode}] ${resp.message}`);
await webviewWindow.getCurrentWebviewWindow().setTitle(`Post_${postId} ${resp.message}`);
await TGLogger.Error(`[t-post][${postId}][onMounted] ${resp.retcode}: ${resp.message}`);
return;
}
postData.value = resp;
showLoading.update("正在渲染数据...", `帖子ID: ${postId}`);
renderPost.value = getRenderPost(postData.value);
shareTitle.value = `Post_${postId}`;
await webviewWindow
.getCurrentWebviewWindow()
.setTitle(`Post_${postId} ${postData.value.post.subject}`);
await TGLogger.Info(`[t-post][${postId}][onMounted] ${postData.value.post.subject}`);
// 打开 json
const isDev = useAppStore().devMode ?? false;