mirror of
https://github.com/BTMuli/TeyvatGuide.git
synced 2025-12-13 09:28:14 +08:00
🎨 优化错误处理
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
/**
|
/**
|
||||||
* @file plugins/Mys/request/postReq.ts
|
* @file plugins/Mys/request/postReq.ts
|
||||||
* @description 帖子相关的获取
|
* @description 帖子相关的获取
|
||||||
* @since Beta v0.6.2
|
* @since Beta v0.6.3
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import TGHttp from "../../../utils/TGHttp.js";
|
import TGHttp from "../../../utils/TGHttp.js";
|
||||||
@@ -35,18 +35,20 @@ export async function getForumPostList(
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @description 获取单个帖子信息
|
* @description 获取单个帖子信息
|
||||||
* @since Beta v0.6.2
|
* @since Beta v0.6.3
|
||||||
* @param {number} postId 帖子 ID
|
* @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> {
|
export async function getPostFull(
|
||||||
return (
|
postId: number,
|
||||||
await TGHttp<TGApp.Plugins.Mys.Post.Response>(`${Mpabu}getPostFull`, {
|
): Promise<TGApp.Plugins.Mys.Post.FullData | TGApp.BBS.Response.Base> {
|
||||||
method: "GET",
|
const resp = await TGHttp<TGApp.Plugins.Mys.Post.Response>(`${Mpabu}getPostFull`, {
|
||||||
headers: { referer: Referer },
|
method: "GET",
|
||||||
query: { post_id: postId },
|
headers: { referer: Referer },
|
||||||
})
|
query: { post_id: postId },
|
||||||
).data.post;
|
});
|
||||||
|
if (resp.retcode !== 0) return <TGApp.BBS.Response.Base>resp;
|
||||||
|
return resp.data.post;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,15 +1,16 @@
|
|||||||
/**
|
/**
|
||||||
* @file plugins/Mys/utils/getGachaCard.ts
|
* @file plugins/Mys/utils/getGachaCard.ts
|
||||||
* @description Mys 插件抽卡工具
|
* @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 { AppCharacterData } from "../../../data/index.js";
|
||||||
import { getPostFull } from "../request/postReq.js";
|
import { getPostFull } from "../request/postReq.js";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description 根据单个卡池信息转为渲染用的卡池信息
|
* @description 根据单个卡池信息转为渲染用的卡池信息
|
||||||
* @since Beta v0.6.2
|
* @since Beta v0.6.3
|
||||||
* @param {TGApp.Plugins.Mys.Gacha.Data} data 卡池信息
|
* @param {TGApp.Plugins.Mys.Gacha.Data} data 卡池信息
|
||||||
* @param {string} poolCover 卡池封面
|
* @param {string} poolCover 卡池封面
|
||||||
* @returns {Promise<TGApp.Plugins.Mys.Gacha.RenderCard>}
|
* @returns {Promise<TGApp.Plugins.Mys.Gacha.RenderCard>}
|
||||||
@@ -17,36 +18,28 @@ import { getPostFull } from "../request/postReq.js";
|
|||||||
async function getGachaItemCard(
|
async function getGachaItemCard(
|
||||||
data: TGApp.Plugins.Mys.Gacha.Data,
|
data: TGApp.Plugins.Mys.Gacha.Data,
|
||||||
poolCover?: string,
|
poolCover?: string,
|
||||||
): Promise<TGApp.Plugins.Mys.Gacha.RenderCard> {
|
): Promise<TGApp.Plugins.Mys.Gacha.RenderCard | null> {
|
||||||
let cover = "/source/UI/empty.webp";
|
let cover = "/source/UI/empty.webp";
|
||||||
const postId: number | undefined = Number(data.activity_url.split("/").pop()) || undefined;
|
const postId: number | undefined = Number(data.activity_url.split("/").pop()) || undefined;
|
||||||
if (postId === undefined || isNaN(postId)) {
|
if (postId === undefined || isNaN(postId)) return null;
|
||||||
throw new Error("无法获取帖子 ID");
|
|
||||||
}
|
|
||||||
if (poolCover !== undefined) {
|
if (poolCover !== undefined) {
|
||||||
cover = poolCover;
|
cover = poolCover;
|
||||||
} else {
|
} else {
|
||||||
try {
|
const postResp = await getPostFull(postId);
|
||||||
console.log("调用 getPostData");
|
if ("retcode" in postResp) {
|
||||||
const post = await getPostFull(postId);
|
showSnackbar.error(`[${postResp.retcode}] ${postResp.message}`);
|
||||||
cover = post.cover?.url ?? post.post.images[0];
|
return null;
|
||||||
} catch (error) {
|
|
||||||
console.error(error);
|
|
||||||
}
|
}
|
||||||
|
cover = postResp.cover?.url ?? postResp.post.images[0];
|
||||||
}
|
}
|
||||||
const timeStr = `${data.start_time} ~ ${data.end_time}`;
|
const timeStr = `${data.start_time} ~ ${data.end_time}`;
|
||||||
const characters: TGApp.Plugins.Mys.Gacha.RenderItem[] = [];
|
const characters: TGApp.Plugins.Mys.Gacha.RenderItem[] = [];
|
||||||
for (const character of data.pool) {
|
for (const character of data.pool) {
|
||||||
const item: TGApp.Plugins.Mys.Gacha.RenderItem = {
|
const item: TGApp.Plugins.Mys.Gacha.RenderItem = { icon: character.icon, url: character.url };
|
||||||
icon: character.icon,
|
|
||||||
url: character.url,
|
|
||||||
};
|
|
||||||
const contentId = character.url.match(/(?<=content\/)\d+/)?.[0];
|
const contentId = character.url.match(/(?<=content\/)\d+/)?.[0];
|
||||||
if (contentId) {
|
if (contentId) {
|
||||||
const itemF = AppCharacterData.find((item) => item.contentId.toString() === contentId);
|
const itemF = AppCharacterData.find((item) => item.contentId.toString() === contentId);
|
||||||
if (itemF) {
|
if (itemF) item.info = itemF;
|
||||||
item.info = itemF;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
characters.push(item);
|
characters.push(item);
|
||||||
}
|
}
|
||||||
@@ -67,7 +60,7 @@ async function getGachaItemCard(
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @description 根据卡池信息转为渲染用的卡池信息
|
* @description 根据卡池信息转为渲染用的卡池信息
|
||||||
* @since Beta v0.4.4
|
* @since Beta v0.6.3
|
||||||
* @param {TGApp.Plugins.Mys.Gacha.Data[]} gachaData 卡池信息
|
* @param {TGApp.Plugins.Mys.Gacha.Data[]} gachaData 卡池信息
|
||||||
* @param {Record<number, string>} poolCover 卡池封面
|
* @param {Record<number, string>} poolCover 卡池封面
|
||||||
* @returns {Promise<TGApp.Plugins.Mys.Gacha.RenderCard[]>}
|
* @returns {Promise<TGApp.Plugins.Mys.Gacha.RenderCard[]>}
|
||||||
@@ -80,7 +73,7 @@ export async function getGachaCard(
|
|||||||
await Promise.allSettled(
|
await Promise.allSettled(
|
||||||
gachaData.map(async (data: TGApp.Plugins.Mys.Gacha.Data) => {
|
gachaData.map(async (data: TGApp.Plugins.Mys.Gacha.Data) => {
|
||||||
const item = await getGachaItemCard(data, poolCover?.[Number(data.id)]);
|
const item = await getGachaItemCard(data, poolCover?.[Number(data.id)]);
|
||||||
gachaCard.push(item);
|
if (item !== null) gachaCard.push(item);
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
return gachaCard;
|
return gachaCard;
|
||||||
|
|||||||
@@ -8,18 +8,19 @@
|
|||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
import { onMounted, reactive, ref } from "vue";
|
import { onMounted, ref } from "vue";
|
||||||
import JsonViewer from "vue-json-viewer";
|
import JsonViewer from "vue-json-viewer";
|
||||||
import { useRoute } from "vue-router";
|
import { useRoute } from "vue-router";
|
||||||
|
|
||||||
import TSwitchTheme from "../components/app/t-switchTheme.vue";
|
import TSwitchTheme from "../components/app/t-switchTheme.vue";
|
||||||
import showLoading from "../components/func/loading.js";
|
import showLoading from "../components/func/loading.js";
|
||||||
|
import showSnackbar from "../components/func/snackbar.js";
|
||||||
import Mys from "../plugins/Mys/index.js";
|
import Mys from "../plugins/Mys/index.js";
|
||||||
import TGLogger from "../utils/TGLogger.js";
|
import TGLogger from "../utils/TGLogger.js";
|
||||||
|
|
||||||
const postId = Number(useRoute().params.post_id);
|
const postId = Number(useRoute().params.post_id);
|
||||||
let jsonData = reactive<TGApp.Plugins.Mys.Post.FullData>(<TGApp.Plugins.Mys.Post.FullData>{});
|
const jsonData = ref<TGApp.Plugins.Mys.Post.FullData>();
|
||||||
let parseData = reactive<TGApp.Plugins.Mys.SctPost.Base[]>([]);
|
const parseData = ref<TGApp.Plugins.Mys.SctPost.Base[]>();
|
||||||
const isEmpty = ref<boolean>(false);
|
const isEmpty = ref<boolean>(false);
|
||||||
|
|
||||||
onMounted(async () => {
|
onMounted(async () => {
|
||||||
@@ -28,18 +29,19 @@ onMounted(async () => {
|
|||||||
showLoading.empty("错误的帖子ID!");
|
showLoading.empty("错误的帖子ID!");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
const resp = await Mys.Post.getPostFull(postId);
|
||||||
jsonData = await Mys.Post.getPostFull(postId);
|
if ("retcode" in resp) {
|
||||||
} catch (e) {
|
showLoading.empty("获取数据失败", `[${resp.retcode}]${resp.message}`);
|
||||||
showLoading.empty("获取数据失败", `帖子ID:${postId}`);
|
showSnackbar.error(`[${resp.retcode}]${resp.message}`);
|
||||||
await TGLogger.Error(`[${postId}]获取帖子数据失败:${e}`);
|
await TGLogger.Error(`[${postId}]获取帖子数据失败:${resp.retcode} ${resp.message}`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
jsonData.value = resp;
|
||||||
try {
|
try {
|
||||||
parseData = JSON.parse(jsonData.post.content);
|
parseData.value = JSON.parse(jsonData.value.post.content);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
try {
|
try {
|
||||||
parseData = JSON.parse(jsonData.post.structured_content);
|
parseData.value = JSON.parse(jsonData.value.post.structured_content);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
isEmpty.value = true;
|
isEmpty.value = true;
|
||||||
await TGLogger.Error(`[${postId}]解析帖子数据失败:${e}`);
|
await TGLogger.Error(`[${postId}]解析帖子数据失败:${e}`);
|
||||||
|
|||||||
@@ -94,6 +94,7 @@ import TPinWin from "../components/app/t-pinWin.vue";
|
|||||||
import TShareBtn from "../components/app/t-shareBtn.vue";
|
import TShareBtn from "../components/app/t-shareBtn.vue";
|
||||||
import TSwitchTheme from "../components/app/t-switchTheme.vue";
|
import TSwitchTheme from "../components/app/t-switchTheme.vue";
|
||||||
import showLoading from "../components/func/loading.js";
|
import showLoading from "../components/func/loading.js";
|
||||||
|
import showSnackbar from "../components/func/snackbar.js";
|
||||||
import TbCollect from "../components/post/tb-collect.vue";
|
import TbCollect from "../components/post/tb-collect.vue";
|
||||||
import TpAvatar from "../components/post/tp-avatar.vue";
|
import TpAvatar from "../components/post/tp-avatar.vue";
|
||||||
import TpParser from "../components/post/tp-parser.vue";
|
import TpParser from "../components/post/tp-parser.vue";
|
||||||
@@ -139,26 +140,21 @@ onMounted(async () => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
showLoading.update("正在获取数据...", `帖子ID: ${postId}`);
|
showLoading.update("正在获取数据...", `帖子ID: ${postId}`);
|
||||||
try {
|
const resp = await Mys.Post.getPostFull(postId);
|
||||||
postData.value = await Mys.Post.getPostFull(postId);
|
if ("retcode" in resp) {
|
||||||
showLoading.update("正在渲染数据...", `帖子ID: ${postId}`);
|
showLoading.empty(`[code]${resp.retcode}`, resp.message);
|
||||||
renderPost.value = getRenderPost(postData.value);
|
showSnackbar.error(`[${resp.retcode}] ${resp.message}`);
|
||||||
shareTitle.value = `Post_${postId}`;
|
await webviewWindow.getCurrentWebviewWindow().setTitle(`Post_${postId} ${resp.message}`);
|
||||||
await webviewWindow
|
await TGLogger.Error(`[t-post][${postId}][onMounted] ${resp.retcode}: ${resp.message}`);
|
||||||
.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`);
|
|
||||||
return;
|
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}`);
|
await TGLogger.Info(`[t-post][${postId}][onMounted] ${postData.value.post.subject}`);
|
||||||
// 打开 json
|
// 打开 json
|
||||||
const isDev = useAppStore().devMode ?? false;
|
const isDev = useAppStore().devMode ?? false;
|
||||||
|
|||||||
Reference in New Issue
Block a user