♻️ 重构部分路由处理

This commit is contained in:
目棃
2025-01-17 14:48:38 +08:00
parent a3c78ce134
commit da78d27239
4 changed files with 38 additions and 12 deletions

View File

@@ -212,11 +212,13 @@ async function handleDeepLink(payload: string): Promise<void> {
} }
if (payload.startsWith("router?path=")) { if (payload.startsWith("router?path=")) {
const routerPath = payload.replace("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); showSnackbar.warn("已在当前页面!", 3000);
return; return;
} }
await router.push(routerPath); await router.push({ path: routerPath, query: {} });
window.location.pathname = routerPath;
return; return;
} }
} }

View File

@@ -122,7 +122,7 @@ import showSnackbar from "@comp/func/snackbar.js";
import VpOverlaySearch from "@comp/viewPost/vp-overlay-search.vue"; import VpOverlaySearch from "@comp/viewPost/vp-overlay-search.vue";
import Mys from "@Mys/index.js"; import Mys from "@Mys/index.js";
import { onMounted, ref, shallowRef, watch } from "vue"; 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 TGLogger from "@/utils/TGLogger.js";
import { createPost } from "@/utils/TGWindow.js"; import { createPost } from "@/utils/TGWindow.js";
@@ -139,7 +139,8 @@ const sortOrderList: Array<Omit<SortSelect, "icon">> = [
{ text: "热门", value: 3 }, { text: "热门", value: 3 },
]; ];
const { gid, forum } = useRoute().params; const route = useRoute();
const router = useRouter();
const curGid = ref<number>(2); const curGid = ref<number>(2);
const curSortType = ref<number>(1); const curSortType = ref<number>(1);
const search = ref<string>(""); const search = ref<string>("");
@@ -153,6 +154,9 @@ const posts = shallowRef<Array<TGApp.Plugins.Mys.Post.FullData>>([]);
onMounted(async () => { onMounted(async () => {
await showLoading.start("正在加载帖子数据"); await showLoading.start("正在加载帖子数据");
await loadForums(); 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 (gid && typeof gid === "string") curGid.value = Number(gid);
if (forum && typeof forum === "string") { if (forum && typeof forum === "string") {
selectedForum.value = getForum(curGid.value, Number(forum)); selectedForum.value = getForum(curGid.value, Number(forum));
@@ -264,6 +268,11 @@ async function getCurrentPosts(
async function freshPostData(): Promise<void> { async function freshPostData(): Promise<void> {
if (!selectedForum.value) return; 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)}帖子`); await showLoading.start(`正在刷新${getGameLabel(curGid.value)}帖子`);
const gameLabel = getGameLabel(curGid.value); const gameLabel = getGameLabel(curGid.value);
const forumLabel = getForum(curGid.value, selectedForum.value.value).text; const forumLabel = getForum(curGid.value, selectedForum.value.value).text;

View File

@@ -4,7 +4,7 @@
<div class="post-topic-top" v-if="topicInfo"> <div class="post-topic-top" v-if="topicInfo">
<TMiImg :src="topicInfo.topic.cover" alt="cover" :ori="true" /> <TMiImg :src="topicInfo.topic.cover" alt="cover" :ori="true" />
<div class="post-topic-info"> <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> <span :title="topicInfo.topic.desc">{{ topicInfo.topic.desc }}</span>
</div> </div>
</div> </div>
@@ -92,7 +92,7 @@ import showSnackbar from "@comp/func/snackbar.js";
import VpOverlaySearch from "@comp/viewPost/vp-overlay-search.vue"; import VpOverlaySearch from "@comp/viewPost/vp-overlay-search.vue";
import Mys from "@Mys/index.js"; import Mys from "@Mys/index.js";
import { computed, onMounted, ref, shallowRef, watch } from "vue"; 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 { createPost } from "@/utils/TGWindow.js";
import { getGameIcon } from "@/utils/toolFunc.js"; import { getGameIcon } from "@/utils/toolFunc.js";
@@ -100,11 +100,13 @@ import { getGameIcon } from "@/utils/toolFunc.js";
type SortSelect = { text: string; value: number }; type SortSelect = { text: string; value: number };
type PostMiniData = { isLast: boolean; lastId: string; total: 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 showSearch = ref<boolean>(false);
const curGid = ref<number>(Number(gid)); const curGid = ref<number>(0);
const curSortType = ref<0 | 1 | 2>(0); const curSortType = ref<0 | 1 | 2>(0);
const search = ref<string>(""); const search = ref<string>("");
const curTopic = ref<string>("");
const postRaw = shallowRef<PostMiniData>({ isLast: false, lastId: "", total: 0 }); const postRaw = shallowRef<PostMiniData>({ isLast: false, lastId: "", total: 0 });
const topicInfo = shallowRef<TGApp.Plugins.Mys.Topic.InfoData>(); const topicInfo = shallowRef<TGApp.Plugins.Mys.Topic.InfoData>();
const posts = shallowRef<Array<TGApp.Plugins.Mys.Post.FullData>>([]); const posts = shallowRef<Array<TGApp.Plugins.Mys.Post.FullData>>([]);
@@ -125,6 +127,13 @@ const sortList = computed<Array<SortSelect>>(() => {
}); });
onMounted(async () => { 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}信息`); await showLoading.start(`正在加载话题${topic}信息`);
const info = await Mys.Post.getTopicFullInfo(gid, topic); const info = await Mys.Post.getTopicFullInfo(gid, topic);
if ("retcode" in info) { if ("retcode" in info) {
@@ -154,8 +163,13 @@ watch(
async function firstLoad(): Promise<void> { async function firstLoad(): Promise<void> {
await showLoading.start(`正在加载话题${topicInfo.value?.topic.name}信息`); 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" }); 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) { if ("retcode" in postList) {
await showLoading.end(); await showLoading.end();
showSnackbar.error(`[${postList.retcode}] ${postList.message}`); showSnackbar.error(`[${postList.retcode}] ${postList.message}`);
@@ -182,7 +196,7 @@ async function freshPostData(): Promise<void> {
const pageSize = mod20 === 0 ? 20 : 20 - mod20; const pageSize = mod20 === 0 ? 20 : 20 - mod20;
const postList = await Mys.Post.getTopicPostList( const postList = await Mys.Post.getTopicPostList(
curGid.value, curGid.value,
topic, curTopic.value,
curSortType.value, curSortType.value,
postRaw.value.lastId, postRaw.value.lastId,
pageSize, pageSize,

View File

@@ -1,7 +1,7 @@
/** /**
* @file router index.ts * @file router index.ts
* @description 路由入口 * @description 路由入口
* @since Beta v0.3.3 * @since Beta v0.6.8
*/ */
import { createRouter, createWebHistory } from "vue-router"; import { createRouter, createWebHistory } from "vue-router";
@@ -12,7 +12,8 @@ const router = createRouter({ history: createWebHistory(), routes: routes });
// 解决路由重复问题 // 解决路由重复问题
router.afterEach((to, from) => { 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(); window.location.reload();
} }
}); });