import ParallaxHero from "@/app/[locale]/blog/ParallaxHero"; import TablerEyeFilled from "@/components/icons/eye"; import { Link as I18nLink, Locale, LOCALES } from "@/i18n/routing"; import { getPosts } from "@/lib/getBlogs"; import { constructMetadata } from "@/lib/metadata"; import dayjs from "dayjs"; import { Metadata } from "next"; import { getTranslations } from "next-intl/server"; type Params = Promise<{ locale: string }>; type SearchParams = { page?: string; type?: string }; type MetadataProps = { params: Params; }; export async function generateMetadata({ params, }: MetadataProps): Promise { const { locale } = await params; const t = await getTranslations({ locale, namespace: "Blog" }); return constructMetadata({ page: "Blog", title: t("title"), description: t("description"), locale: locale as Locale, path: `/blog`, canonicalUrl: `/blog`, }); } export default async function Page({ params, searchParams, }: { params: Params; searchParams?: SearchParams; }) { const { locale } = await params; let { posts } = await getPosts(locale); const t = await getTranslations("Blog"); // 分类筛选(公司新闻/专家访谈/行业动态/公告) const type = (searchParams?.type || "").toLowerCase(); const typeLabelMap: Record = { company: "公司新闻", experts: "专家访谈", industry: "行业动态", notice: "公告", }; const typeLabel = typeLabelMap[type]; if (type) { posts = posts.filter((p) => { const metaType = String((p.metadata as any)?.type || "").toLowerCase(); const tags: string[] = (p.tags as any) || []; const hitMeta = metaType === type; const hitTag = Array.isArray(tags) && tags.some((t) => String(t).toLowerCase() === type); return hitMeta || hitTag; }); } const pageRaw = searchParams?.page; const page = Math.max(1, parseInt(pageRaw || "1", 10)); const pageSize = 10; const totalPages = Math.max(1, Math.ceil(posts.length / pageSize)); const start = (page - 1) * pageSize; const end = start + pageSize; const pagePosts = posts.slice(start, end); return (
{/* 顶部操作区:标签 + 面包屑 */}
首页 / 新闻中心 {typeLabel && ( <> / {typeLabel} )}
{/* 列表 */}
{pagePosts.map((post) => { const year = dayjs(post.date).format("YYYY"); const monthDay = dayjs(post.date).format("MM-DD"); const views = (post.metadata?.views as number) || 0; return (
{post.title}
访问量:{views}
{year}
{monthDay}
); })}
{"<"} {(() => { const items: (number | string)[] = []; const showTotal = totalPages; const maxNumbers = 4; const startNum = 1; const endNum = Math.min(showTotal, maxNumbers); for (let n = startNum; n <= endNum; n++) items.push(n); if (showTotal > maxNumbers + 1) items.push("..."); if (showTotal > maxNumbers) items.push(showTotal); return items.map((it, idx) => { if (typeof it === "string") return ( {it} ); const isActive = it === page; return ( {it} ); }); })()} {">"} 跳往
); } export async function generateStaticParams() { return LOCALES.map((locale) => ({ locale })); }