refactor(router): 移除笔记路由并重构URL生成方法

- 移除了笔记路由,暂时禁用相关的noteRoute()函数。
- 重构了getCardUrl()和getNoteUrl()方法,使用URL构造器以增强可读性和可维护性。
- 在cardRoute()和noteRoute()中,将user_name从路由参数中移除,现在通过查询参数处理。
This commit is contained in:
ZZY 2024-09-22 23:17:34 +08:00
parent 3325d5aacc
commit f3ccb0361a
4 changed files with 30 additions and 28 deletions

View File

@ -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;
}

View File

@ -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, string> = {}): 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<string, string>),
}
},
}

View File

@ -26,7 +26,7 @@ const router = createRouter({
component: () => import("@/pages/Users.vue"),
},
cardRoute(),
noteRoute(),
// noteRoute(),
// {
// name: "noteShower",
// path: "/note/:note_name/:token?",

View File

@ -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, string> = {}): 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<string, string>),
}
},
}
}
}