From 555135aedb16a1ccc7b7f03326b49365d2a6922e Mon Sep 17 00:00:00 2001 From: xy <10816187@qq.com> Date: Fri, 14 Aug 2026 21:28:41 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=94=A8=E6=88=B7=E6=89=8B=E5=8A=A8?= =?UTF-8?q?=E8=BE=93=E5=85=A5query=E6=9D=A1=E4=BB=B6=E5=AF=BC=E8=87=B4?= =?UTF-8?q?=E7=9A=84=E6=90=9C=E7=B4=A2=E5=BC=82=E5=B8=B8=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/search.astro | 47 ++++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/src/pages/search.astro b/src/pages/search.astro index 518bf50..8556dc6 100644 --- a/src/pages/search.astro +++ b/src/pages/search.astro @@ -10,15 +10,9 @@ const searchParams = Astro.url.searchParams; const t = searchParams.get("t") ?? ""; const c = searchParams.get("c") ?? ""; -const [{data: categories}, {data: tags}, {data: posts}] = await Promise.all([ +const [{data: categories}, {data: tags}] = await Promise.all([ http.get>("categories/stats"), http.get>("posts/tags"), - http.get>("posts/search", { - searchParams: { - category_code: c ?? "", - tag_code: t ?? "", - } - }), ] as const) let f_count = 0 @@ -36,6 +30,14 @@ const findTag = tags.find(item => { return item.code === t }) +// 搜索条件必须匹配 不允许非法值 +const {data:posts} = await http.get>("posts/search", { + searchParams: { + category_code: findCategory?.code ?? "", + tag_code: findTag?.code ?? "", + } +}) + const count = posts.length const pageTitle = @@ -49,24 +51,33 @@ const pageDescription = : `浏览${pageTitle}相关文章,共 ${count} 篇。` const buildSearchUrl = (type: 'category' | 'tag', code: string) => { - const params = { - t: t, - c: c, + const params = new URLSearchParams() + + if (findTag?.code) { + params.set('t', findTag.code) + } + + if (findCategory?.code) { + params.set('c', findCategory.code) } if (type === 'category') { - params.c = c === code ? "" : code + if (c === code) { + params.delete('c') + } else { + params.set('c', code) + } } else { - params.t = t === code ? "" : code + if (t === code) { + params.delete('t') + } else { + params.set('t', code) + } } - const searchParams = new URLSearchParams( - Object.entries(params).filter(([, value]) => value !== '') - ); + const query = params.toString() - const query = searchParams.toString(); - - return query ? `/search?${query}` : '/search'; + return query ? `/search?${query}` : '/search' } ---