Skip to content

Commit

Permalink
♻️ 重构部分路由处理
Browse files Browse the repository at this point in the history
  • Loading branch information
BTMuli committed Jan 17, 2025
1 parent a3c78ce commit da78d27
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 12 deletions.
6 changes: 4 additions & 2 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,13 @@ async function handleDeepLink(payload: string): Promise<void> {
}
if (payload.startsWith("router?path=")) {
const routerPath = payload.replace("router?path=", "");
if (router.currentRoute.value.path === routerPath) {
const curPath = router.currentRoute.value.path;
if (curPath === routerPath) {
showSnackbar.warn("已在当前页面!", 3000);
return;
}
await router.push(routerPath);
await router.push({ path: routerPath, query: {} });
window.location.pathname = routerPath;
return;
}
}
Expand Down
13 changes: 11 additions & 2 deletions src/pages/common/PostForum.vue
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ import showSnackbar from "@comp/func/snackbar.js";
import VpOverlaySearch from "@comp/viewPost/vp-overlay-search.vue";
import Mys from "@Mys/index.js";
import { onMounted, ref, shallowRef, watch } from "vue";
import { useRoute } from "vue-router";
import { useRoute, useRouter } from "vue-router";
import TGLogger from "@/utils/TGLogger.js";
import { createPost } from "@/utils/TGWindow.js";
Expand All @@ -139,7 +139,8 @@ const sortOrderList: Array<Omit<SortSelect, "icon">> = [
{ text: "热门", value: 3 },
];
const { gid, forum } = useRoute().params;
const route = useRoute();
const router = useRouter();
const curGid = ref<number>(2);
const curSortType = ref<number>(1);
const search = ref<string>("");
Expand All @@ -153,6 +154,9 @@ const posts = shallowRef<Array<TGApp.Plugins.Mys.Post.FullData>>([]);
onMounted(async () => {
await showLoading.start("正在加载帖子数据");
await loadForums();
let { gid, forum } = route.query;
if (!gid) gid = route.params.gid;
if (!forum) forum = route.params.forum;
if (gid && typeof gid === "string") curGid.value = Number(gid);
if (forum && typeof forum === "string") {
selectedForum.value = getForum(curGid.value, Number(forum));
Expand Down Expand Up @@ -264,6 +268,11 @@ async function getCurrentPosts(
async function freshPostData(): Promise<void> {
if (!selectedForum.value) return;
await router.push({
name: "酒馆",
params: route.params,
query: { gid: curGid.value, forum: selectedForum.value.value },
});
await showLoading.start(`正在刷新${getGameLabel(curGid.value)}帖子`);
const gameLabel = getGameLabel(curGid.value);
const forumLabel = getForum(curGid.value, selectedForum.value.value).text;
Expand Down
26 changes: 20 additions & 6 deletions src/pages/common/PostTopic.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<div class="post-topic-top" v-if="topicInfo">
<TMiImg :src="topicInfo.topic.cover" alt="cover" :ori="true" />
<div class="post-topic-info">
<span>{{ topicInfo.topic.name }}({{ topic }})</span>
<span>{{ topicInfo.topic.name }}({{ curTopic }})</span>
<span :title="topicInfo.topic.desc">{{ topicInfo.topic.desc }}</span>
</div>
</div>
Expand Down Expand Up @@ -92,19 +92,21 @@ import showSnackbar from "@comp/func/snackbar.js";
import VpOverlaySearch from "@comp/viewPost/vp-overlay-search.vue";
import Mys from "@Mys/index.js";
import { computed, onMounted, ref, shallowRef, watch } from "vue";
import { useRoute } from "vue-router";
import { useRoute, useRouter } from "vue-router";
import { createPost } from "@/utils/TGWindow.js";
import { getGameIcon } from "@/utils/toolFunc.js";
type SortSelect = { text: string; value: number };
type PostMiniData = { isLast: boolean; lastId: string; total: number };
const { gid, topic } = <{ gid: string; topic: string }>useRoute().params;
const route = useRoute();
const router = useRouter();
const showSearch = ref<boolean>(false);
const curGid = ref<number>(Number(gid));
const curGid = ref<number>(0);
const curSortType = ref<0 | 1 | 2>(0);
const search = ref<string>("");
const curTopic = ref<string>("");
const postRaw = shallowRef<PostMiniData>({ isLast: false, lastId: "", total: 0 });
const topicInfo = shallowRef<TGApp.Plugins.Mys.Topic.InfoData>();
const posts = shallowRef<Array<TGApp.Plugins.Mys.Post.FullData>>([]);
Expand All @@ -125,6 +127,13 @@ const sortList = computed<Array<SortSelect>>(() => {
});
onMounted(async () => {
let { gid, topic } = route.query;
if (!gid) gid = route.params.gid;
if (!topic) topic = route.params.topic;
if (!gid || typeof gid !== "string") gid = "0";
if (!topic || typeof topic !== "string") topic = "0";
curGid.value = Number(gid);
curTopic.value = topic;
await showLoading.start(`正在加载话题${topic}信息`);
const info = await Mys.Post.getTopicFullInfo(gid, topic);
if ("retcode" in info) {
Expand Down Expand Up @@ -154,8 +163,13 @@ watch(
async function firstLoad(): Promise<void> {
await showLoading.start(`正在加载话题${topicInfo.value?.topic.name}信息`);
await router.push({
name: "话题",
params: route.params,
query: { gid: curGid.value, topic: curTopic.value },
});
document.documentElement.scrollTo({ top: 0, behavior: "smooth" });
const postList = await Mys.Post.getTopicPostList(curGid.value, topic, curSortType.value);
const postList = await Mys.Post.getTopicPostList(curGid.value, curTopic.value, curSortType.value);
if ("retcode" in postList) {
await showLoading.end();
showSnackbar.error(`[${postList.retcode}] ${postList.message}`);
Expand All @@ -182,7 +196,7 @@ async function freshPostData(): Promise<void> {
const pageSize = mod20 === 0 ? 20 : 20 - mod20;
const postList = await Mys.Post.getTopicPostList(
curGid.value,
topic,
curTopic.value,
curSortType.value,
postRaw.value.lastId,
pageSize,
Expand Down
5 changes: 3 additions & 2 deletions src/router/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @file router index.ts
* @description 路由入口
* @since Beta v0.3.3
* @since Beta v0.6.8
*/

import { createRouter, createWebHistory } from "vue-router";
Expand All @@ -12,7 +12,8 @@ const router = createRouter({ history: createWebHistory(), routes: routes });

// 解决路由重复问题
router.afterEach((to, from) => {
if (from.name === to.name && from.fullPath !== to.fullPath) {
if (from.name === to.name) {
if (from.query !== to.query) return;
window.location.reload();
}
});
Expand Down

0 comments on commit da78d27

Please sign in to comment.