♻️ 重构链接解析处理

This commit is contained in:
目棃
2024-07-26 22:35:51 +08:00
parent a7a0a8b0e0
commit 672fa2e536
4 changed files with 98 additions and 17 deletions

View File

@@ -7,6 +7,7 @@
</div> </div>
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import { emit } from "@tauri-apps/api/event";
import { onMounted, ref, watch } from "vue"; import { onMounted, ref, watch } from "vue";
import Mys from "../../plugins/Mys/index.js"; import Mys from "../../plugins/Mys/index.js";
@@ -95,8 +96,15 @@ async function toBBS(link: URL): Promise<void> {
} }
if (link.pathname.startsWith("//forum")) { if (link.pathname.startsWith("//forum")) {
const forumId = link.pathname.split("/").pop(); const forumId = link.pathname.split("/").pop();
const url = `https://www.miyoushe.com/ys/home/${forumId}`; const localPath = getLocalPath(forumId);
window.open(url); if (localPath === "") {
showSnackbar({
text: `不支持的链接:${link.href}`,
color: "warn",
});
return;
}
await emit("active_deep_link", `router?path=${localPath}`);
return; return;
} }
} }
@@ -105,12 +113,40 @@ async function toBBS(link: URL): Promise<void> {
color: "warn", color: "warn",
}); });
} }
function getLocalPath(forum?: string): string {
if (!forum) return "";
const forumLocalMap: Record<string, string> = {
"31": "/news/3", // 崩坏2官方
"6": "/news/1", // 崩坏3官方
"28": "/news/2", // 原神官方
"33": "/news/4", // 未定官方
"58": "/news/8", // 绝区零官方
"36": "/news/5", // 大别野公告
};
if (forumLocalMap[forum]) return forumLocalMap[forum];
const ysForums = ["26", "43", "29", "49", "50"];
const srForums = ["52", "61", "56", "62"];
const bh3Forums = ["1", "14", "4", "41"];
const bh2Forums = ["30", "51", "40"];
const wdForums = ["37", "60", "42", "38"];
const zzzForums = ["57", "59", "64", "65"];
const dbyForums = ["54", "35", "34", "39", "47", "48", "55", "36"];
if (ysForums.includes(forum)) return `/posts/2/${forum}`;
if (srForums.includes(forum)) return `/posts/6/${forum}`;
if (bh3Forums.includes(forum)) return `/posts/1/${forum}`;
if (bh2Forums.includes(forum)) return `/posts/3/${forum}`;
if (wdForums.includes(forum)) return `/posts/4/${forum}`;
if (zzzForums.includes(forum)) return `/posts/8/${forum}`;
if (dbyForums.includes(forum)) return `/posts/5/${forum}`;
return "";
}
</script> </script>
<style lang="css" scoped> <style lang="css" scoped>
.tgn-container { .tgn-container {
display: flex; display: flex;
padding: 5px; padding: 5px;
gap: 10px 10px; gap: 10px;
} }
.tgn-nav { .tgn-nav {

View File

@@ -49,7 +49,8 @@
<ToPostSearch :gid="curGid.toString()" v-model="showSearch" :keyword="search" /> <ToPostSearch :gid="curGid.toString()" v-model="showSearch" :keyword="search" />
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { nextTick, onMounted, ref, watch } from "vue"; import { nextTick, onBeforeMount, onMounted, ref, watch } from "vue";
import { useRoute } from "vue-router";
import showSnackbar from "../../components/func/snackbar.js"; import showSnackbar from "../../components/func/snackbar.js";
import TGameNav from "../../components/main/t-gamenav.vue"; import TGameNav from "../../components/main/t-gamenav.vue";
@@ -134,6 +135,10 @@ const gameList = {
大别野: 5, 大别野: 5,
}; };
// 路由
const gid = useRoute().params.gid;
const forum = useRoute().params.forum;
// 渲染参数 // 渲染参数
const curForumLabel = ref<string>("酒馆"); const curForumLabel = ref<string>("酒馆");
const forumItem = ref<string[]>(["酒馆", "攻略", "同人图", "COS", "硬核"]); const forumItem = ref<string[]>(["酒馆", "攻略", "同人图", "COS", "硬核"]);
@@ -162,12 +167,44 @@ const posts = ref<TGApp.Plugins.Mys.Post.FullData[]>([]);
const search = ref<string>(""); const search = ref<string>("");
const showSearch = ref<boolean>(false); const showSearch = ref<boolean>(false);
onBeforeMount(async () => {
if (gid && typeof gid === "string") {
const gameKeys = Object.values(gameList);
if (!gameKeys.includes(Number(gid))) {
showSnackbar({
text: `不存在GID为${gid}的游戏`,
color: "error",
});
return;
}
curGameLabel.value = <keyof typeof gameList>(
Object.keys(gameList)[gameKeys.indexOf(Number(gid))]
);
curGid.value = Number(gid);
}
if (forum && typeof forum === "string") {
const forumKeys = Object.keys(forumList[curGameLabel.value]);
if (!forumKeys.includes(forum)) {
showSnackbar({
text: `${curGameLabel.value}不存在ID为${forum}的版块`,
color: "error",
});
return;
}
curForumLabel.value = forum;
}
});
onMounted(async () => { onMounted(async () => {
await TGLogger.Info( await TGLogger.Info(
`[Posts][${curGameLabel.value}][onMounted][${curForumLabel.value}] 打开帖子列表`, `[Posts][${curGameLabel.value}][onMounted][${curForumLabel.value}] 打开帖子列表`,
); );
loading.value = true; loading.value = true;
await freshPostData(); if (gid && forum) {
freshCurForum(curForumLabel.value);
} else {
await freshPostData();
}
loading.value = false; loading.value = false;
}); });

View File

@@ -1,7 +1,7 @@
/** /**
* @file router/modules/main.ts * @file router/modules/main.ts
* @description 主路由模块 * @description 主路由模块
* @since Beta v0.4.5 * @since Beta v0.5.1
*/ */
const mainRoutes = [ const mainRoutes = [
@@ -21,7 +21,7 @@ const mainRoutes = [
component: async () => await import("../../pages/common/News.vue"), component: async () => await import("../../pages/common/News.vue"),
}, },
{ {
path: "/posts", path: "/posts/:gid?/:forum?",
name: "酒馆", name: "酒馆",
component: async () => await import("../../pages/common/Posts.vue"), component: async () => await import("../../pages/common/Posts.vue"),
}, },

View File

@@ -1,7 +1,7 @@
/** /**
* @file src/utils/linkParser.ts * @file src/utils/linkParser.ts
* @description 处理链接 * @description 处理链接
* @since Beta v0.5.0 * @since Beta v0.5.1
*/ */
import { emit } from "@tauri-apps/api/event"; import { emit } from "@tauri-apps/api/event";
@@ -52,7 +52,7 @@ export async function parsePost(link: string): Promise<false | string> {
/** /**
* @function parseLink * @function parseLink
* @since Beta v0.5.0 * @since Beta v0.5.1
* @description 处理链接 * @description 处理链接
* @param {string} link - 链接 * @param {string} link - 链接
* @param {boolean} useInner - 是否采用内置 JSBridge 打开 * @param {boolean} useInner - 是否采用内置 JSBridge 打开
@@ -83,16 +83,24 @@ export async function parseLink(
console.log(url.pathname, url.search); console.log(url.pathname, url.search);
// 处理特定路径 // 处理特定路径
if (url.pathname.startsWith("//discussion")) { if (url.pathname.startsWith("//discussion")) {
await emit("active_deep_link", "router?path=/posts"); const gid = url.pathname.split("/").pop();
const forum = url.searchParams.get("forum_id");
await emit("active_deep_link", `router?path=/posts/${gid}/${forum}`);
return true; return true;
} }
if (link === "mihoyobbs://homeForum?game_id=2&tab_type=2") { if (url.pathname.startsWith("//homeForum")) {
await emit("active_deep_link", "router?path=/news/2/news"); const game_id = url.searchParams.get("game_id");
return true; const tab_type = url.searchParams.get("tab_type");
} if (game_id && tab_type) {
if (link === "mihoyobbs://homeForum?game_id=8&tab_type=2") { const tabList = ["", "notice", "activity", "news"];
await emit("active_deep_link", "router?path=/news/8/news"); if (["1", "2", "3"].includes(tab_type)) {
return true; await emit(
"active_deep_link",
`router?path=/news/${game_id}/${tabList[parseInt(tab_type)]}`,
);
return true;
}
}
} }
} }
return false; return false;