From f3ccb0361a4f6a9302c73c730681079cea24a7ce Mon Sep 17 00:00:00 2001 From: ZZY <2450266535@qq.com> Date: Sun, 22 Sep 2024 23:17:34 +0800 Subject: [PATCH] =?UTF-8?q?refactor(router):=20=E7=A7=BB=E9=99=A4=E7=AC=94?= =?UTF-8?q?=E8=AE=B0=E8=B7=AF=E7=94=B1=E5=B9=B6=E9=87=8D=E6=9E=84URL?= =?UTF-8?q?=E7=94=9F=E6=88=90=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除了笔记路由,暂时禁用相关的noteRoute()函数。 - 重构了getCardUrl()和getNoteUrl()方法,使用URL构造器以增强可读性和可维护性。 - 在cardRoute()和noteRoute()中,将user_name从路由参数中移除,现在通过查询参数处理。 --- src/components/cards/Card.vue | 2 +- src/router/card.ts | 27 ++++++++++++++------------- src/router/index.ts | 2 +- src/router/note.ts | 27 ++++++++++++++------------- 4 files changed, 30 insertions(+), 28 deletions(-) diff --git a/src/components/cards/Card.vue b/src/components/cards/Card.vue index 47b87ab..310b0a2 100644 --- a/src/components/cards/Card.vue +++ b/src/components/cards/Card.vue @@ -29,7 +29,7 @@ let { card, idx } = defineProps<{ function getRealHref(href: string, user_name: string = "$") { if (href.startsWith('$')) { - href = `/card/${user_name}${href.substring(1)}`; + href = `/card${href.substring(1)}?user_name=${user_name}`; } return href; } diff --git a/src/router/card.ts b/src/router/card.ts index 19922f4..f924e4b 100644 --- a/src/router/card.ts +++ b/src/router/card.ts @@ -1,30 +1,31 @@ import { type RouteRecordRaw } from "vue-router"; import { file_api } from "./config.json"; -export function getCardUrl(path: string = file_api.cards.home): string { - return `${file_api.root_url}/` + - `${file_api.cards.url}/` + - `${path}?` + - `type=${file_api.cards.type}` +export function getCardUrl(path: string = file_api.cards.home, query: Record = {}): string { + const url = new URL(window.location.origin); + url.pathname = `${file_api.root_url}/${file_api.cards.url}/${path}`; + + url.searchParams.set('type', file_api.cards.type); + Object.entries(query).forEach(([key, value]) => { + url.searchParams.set(key, value); + }) + return url.toString(); }; export default function cardRoute(): RouteRecordRaw { return { name: "cardShower", - path: "/card/:user_name/:pages_path*", + path: "/card/:pages_path*", component: () => import("@/pages/CardsShower.vue"), props(to) { - let path: string; - if (Array.isArray(to.params.pages_path)) { - path = to.params.pages_path.join("/"); - } else { - path = to.params.pages_path; - } + const path = Array.isArray(to.params.pages_path) + ? to.params.pages_path.join("/") + : to.params.pages_path; return { ...to.params, path, - url: getCardUrl(path), + url: getCardUrl(path, to.query as Record), } }, } diff --git a/src/router/index.ts b/src/router/index.ts index 4b65e2b..9e2bffd 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -26,7 +26,7 @@ const router = createRouter({ component: () => import("@/pages/Users.vue"), }, cardRoute(), - noteRoute(), + // noteRoute(), // { // name: "noteShower", // path: "/note/:note_name/:token?", diff --git a/src/router/note.ts b/src/router/note.ts index 8bfc6e6..11b8230 100644 --- a/src/router/note.ts +++ b/src/router/note.ts @@ -1,11 +1,15 @@ import { type RouteRecordRaw } from "vue-router"; import { file_api } from "./config.json"; -export function getNoteUrl(path: string = file_api.cards.home): string { - return `${file_api.root_url}/` + - `${file_api.notes.url}/` + - `${path}?` + - `type=${file_api.notes.type}` +export function getNoteUrl(path: string = file_api.cards.home, query: Record = {}): string { + const url = new URL(window.location.origin); + url.pathname = `${file_api.root_url}/${file_api.notes.url}/${path}`; + + url.searchParams.set('type', file_api.notes.type); + Object.entries(query).forEach(([key, value]) => { + url.searchParams.set(key, value); + }) + return url.toString(); }; export default function noteRoute(): RouteRecordRaw { @@ -14,18 +18,15 @@ export default function noteRoute(): RouteRecordRaw { path: "/note/:pages_path*", component: () => import("@/pages/NoteShower.vue"), props(to) { - let path: string; - if (Array.isArray(to.params.pages_path)) { - path = to.params.pages_path.join("/"); - } else { - path = to.params.pages_path; - } + const path = Array.isArray(to.params.pages_path) + ? to.params.pages_path.join("/") + : to.params.pages_path; return { ...to.params, path, - url: getNoteUrl(path), + url: getNoteUrl(path, to.query as Record), } }, } -} +} \ No newline at end of file