--- import Layout from '@/layouts/Layout.astro' import type {CategoriesStats, Post, Tag} from "@/types"; import {createHttp} from "@/lib/request.ts"; import PostList from "@/components/Post/List.astro"; const http = createHttp(Astro.request.headers); const searchParams = Astro.url.searchParams; const t = searchParams.get("t") ?? ""; const c = searchParams.get("c") ?? ""; const [{data: categories}, {data: tags}] = await Promise.all([ http.get>("categories/stats"), http.get>("posts/tags"), ] as const) let f_count = 0 const findCategory = categories.find(item => { if (item.code === c) { f_count++ } return item.code === c }) const findTag = tags.find(item => { if (item.code === t) { f_count++ } 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 = f_count === 0 ? "所有文章" : [findCategory?.name, findTag?.name] .filter(Boolean) .join(" · ") const pageDescription = f_count === 0 ? "浏览全部文章,按分类和标签筛选你感兴趣的内容。" : `浏览${pageTitle}相关文章,共 ${count} 篇。` const buildSearchUrl = (type: 'category' | 'tag', code: string) => { const params = new URLSearchParams() if (findTag?.code) { params.set('t', findTag.code) } if (findCategory?.code) { params.set('c', findCategory.code) } if (type === 'category') { if (c === code) { params.delete('c') } else { params.set('c', code) } } else { if (t === code) { params.delete('t') } else { params.set('t', code) } } const query = params.toString() return query ? `/search?${query}` : '/search' } ---

{pageTitle}

{count}
{posts.length > 0 ? :
没有找到相关文章
}