feat: connect company setup data and add about page

This commit is contained in:
2026-10-27 21:36:59 +08:00
parent f990df3023
commit 43edc632a5
8 changed files with 133 additions and 29 deletions
+29
View File
@@ -0,0 +1,29 @@
import { notFound } from "next/navigation";
import type { Metadata } from "next";
import { getSimpleContentPageBySlug } from "@/services/legal.service";
import { getPreviewOrFallback } from "@/services/preview.service";
import { createPageMetadata, getPageSEO } from "@/services/seo.service";
import PageSEO from "@/components/layout/PageSEO";
import type { SimpleContentPageDetail } from "@/types/wagtail";
export async function generateMetadata(): Promise<Metadata> {
return createPageMetadata(await getPageSEO("core.SimpleContentPage", "about").catch(() => null), "关于我们");
}
export default async function AboutPage() {
const fallback = () => getSimpleContentPageBySlug("about").catch(() => null);
const candidate = await getPreviewOrFallback<SimpleContentPageDetail>("core.simplecontentpage", "body", fallback);
const page = candidate?.meta.slug === "about" ? candidate : await fallback();
if (!page) notFound();
return (
<article className="mx-auto max-w-3xl px-6 py-24">
<PageSEO type="core.SimpleContentPage" slug="about" />
<h1 className="mb-8 text-4xl font-bold text-slate-900">{page.title}</h1>
<div
className="space-y-4 text-slate-700 [&_h2]:mt-8 [&_h2]:text-2xl [&_h2]:font-semibold [&_h2]:text-slate-900 [&_ul]:list-disc [&_ul]:pl-6 [&_li]:mt-1"
dangerouslySetInnerHTML={{ __html: page.body }}
/>
</article>
);
}
+9 -11
View File
@@ -5,21 +5,19 @@ import Header from "@/components/layout/Header";
import Footer from "@/components/layout/Footer"; import Footer from "@/components/layout/Footer";
import CookieConsent from "@/components/layout/CookieConsent"; import CookieConsent from "@/components/layout/CookieConsent";
import ReactQueryProvider from "@/providers/react-query-provider"; import ReactQueryProvider from "@/providers/react-query-provider";
import { getSiteSettings } from "@/services/site-settings.service";
// 面向中国大陆用户,避免依赖 next/font/google(构建期需访问 Google 服务器)。 // 面向中国大陆用户,避免依赖 next/font/google(构建期需访问 Google 服务器)。
// 生产环境建议自托管思源黑体 / HarmonyOS Sans 子集,这里先用系统字体栈占位。 // 生产环境建议自托管思源黑体 / HarmonyOS Sans 子集,这里先用系统字体栈占位。
export const metadata: Metadata = { export async function generateMetadata(): Promise<Metadata> {
title: { const settings = await getSiteSettings();
default: "企业官网", const companyName = settings?.company_name || "企业官网";
template: "%s | 企业官网", return {
}, title: { default: companyName, template: `%s | ${companyName}` },
description: "企业级 Headless CMS 驱动的现代化官网", description: `${companyName}的产品、解决方案与技术服务`,
icons: { };
icon: { url: "/brand/shiytech-icon.svg", type: "image/svg+xml" }, }
shortcut: "/brand/shiytech-icon.svg",
},
};
export default async function RootLayout({ children }: LayoutProps<"/">) { export default async function RootLayout({ children }: LayoutProps<"/">) {
const { isEnabled: isPreview } = await draftMode(); const { isEnabled: isPreview } = await draftMode();
+4 -7
View File
@@ -2,16 +2,13 @@ import { getSiteSettings } from "@/services/site-settings.service";
import { getNavigationMenu } from "@/services/navigation.service"; import { getNavigationMenu } from "@/services/navigation.service";
import Image from "next/image"; import Image from "next/image";
// 合规要求:中国大陆网站需展示 ICP 备案号并链接至工信部备案查询。
// 详见 documents/设计方案分析与完善版.md §2.15。
const FALLBACK_ICP_NUMBER = "冀ICP备2025130506号-1";
const FALLBACK_ICP_URL = "https://beian.miit.gov.cn/"; const FALLBACK_ICP_URL = "https://beian.miit.gov.cn/";
export default async function Footer() { export default async function Footer() {
// 优先使用后台配置的站点设置(apps.core.SiteSettings), // 优先使用后台配置的站点设置(apps.core.SiteSettings),
// 接口不可用时回退到硬编码的备案信息,保证构建/渲染始终可用。 // 未配置备案号时不展示其他企业的备案信息。
const [siteSettings, menu] = await Promise.all([getSiteSettings(), getNavigationMenu("footer")]); const [siteSettings, menu] = await Promise.all([getSiteSettings(), getNavigationMenu("footer")]);
const icpNumber = siteSettings?.icp_number || FALLBACK_ICP_NUMBER; const icpNumber = siteSettings?.icp_number;
const icpUrl = siteSettings?.icp_url || FALLBACK_ICP_URL; const icpUrl = siteSettings?.icp_url || FALLBACK_ICP_URL;
const qrcode = siteSettings?.wechat_qrcode; const qrcode = siteSettings?.wechat_qrcode;
const qrcodeUrl = qrcode?.url ? new URL(qrcode.url, process.env.NEXT_PUBLIC_API_URL || "http://localhost:8000").href : null; const qrcodeUrl = qrcode?.url ? new URL(qrcode.url, process.env.NEXT_PUBLIC_API_URL || "http://localhost:8000").href : null;
@@ -39,7 +36,7 @@ export default async function Footer() {
&copy; {new Date().getFullYear()}{" "} &copy; {new Date().getFullYear()}{" "}
{siteSettings?.company_name || "企业官网"}. All rights reserved. {siteSettings?.company_name || "企业官网"}. All rights reserved.
</p> </p>
<p className="mt-2"> {icpNumber && <p className="mt-2">
<a <a
href={icpUrl} href={icpUrl}
target="_blank" target="_blank"
@@ -48,7 +45,7 @@ export default async function Footer() {
> >
{icpNumber} {icpNumber}
</a> </a>
</p> </p>}
</div> </div>
</footer> </footer>
); );
+7 -7
View File
@@ -1,6 +1,6 @@
import Link from "next/link"; import Link from "next/link";
import Image from "next/image";
import { getNavigationMenu } from "@/services/navigation.service"; import { getNavigationMenu } from "@/services/navigation.service";
import { getSiteSettings } from "@/services/site-settings.service";
import MobileNavigation from "./MobileNavigation"; import MobileNavigation from "./MobileNavigation";
const fallbackNavItems = [ const fallbackNavItems = [
@@ -14,7 +14,8 @@ const fallbackNavItems = [
export default async function Header() { export default async function Header() {
// 优先使用后台配置的主导航(apps.core.NavigationMenu,name="main"), // 优先使用后台配置的主导航(apps.core.NavigationMenu,name="main"),
// 未配置或接口不可用时回退到静态导航项,保证构建/渲染始终可用。 // 未配置或接口不可用时回退到静态导航项,保证构建/渲染始终可用。
const menu = await getNavigationMenu("main"); const [menu, siteSettings] = await Promise.all([getNavigationMenu("main"), getSiteSettings()]);
const companyName = siteSettings?.company_name || "企业官网";
const navItems = menu?.items.length const navItems = menu?.items.length
? menu.items.map((item) => ({ ? menu.items.map((item) => ({
title: item.label, title: item.label,
@@ -28,13 +29,12 @@ export default async function Header() {
<div className="mx-auto flex h-16 max-w-7xl items-center justify-between px-6"> <div className="mx-auto flex h-16 max-w-7xl items-center justify-between px-6">
<Link <Link
href="/" href="/"
aria-label="世耀科技首页" aria-label={`${companyName}首页`}
className="flex shrink-0 items-center gap-2 whitespace-nowrap text-xl font-bold text-slate-900" className="min-w-0 max-w-[50%] truncate text-xl font-bold text-slate-900"
> >
<Image src="/brand/shiytech-icon.svg" alt="" width={40} height={40} priority /> <span>{companyName}</span>
<span>世耀科技</span>
</Link> </Link>
<nav aria-label="主导航" className="hidden items-center gap-6 md:flex lg:gap-8"> <nav aria-label="主导航" className="hidden flex-wrap items-center justify-end gap-4 xl:flex">
{navItems.map((item) => ( {navItems.map((item) => (
<Link <Link
key={item.href} key={item.href}
+1 -1
View File
@@ -18,7 +18,7 @@ export default function MobileNavigation({ items }: { items: NavigationItem[] })
return ( return (
<div <div
className="md:hidden" className="xl:hidden"
onKeyDown={(event) => { onKeyDown={(event) => {
if (event.key === "Escape" && isOpen) { if (event.key === "Escape" && isOpen) {
setIsOpen(false); setIsOpen(false);
+2 -1
View File
@@ -14,7 +14,7 @@ export const PREVIEW_TOKEN_COOKIE = "wagtail_preview_token";
/** content_type(`app_label.model`,小写)→ 对应前端路由的解析函数 */ /** content_type(`app_label.model`,小写)→ 对应前端路由的解析函数 */
const PREVIEW_PATH_RESOLVERS: Record< const PREVIEW_PATH_RESOLVERS: Record<
string, string,
(page: WagtailPageSummary) => string (page: WagtailPageSummary) => string | null
> = { > = {
"home.homepage": () => "/", "home.homepage": () => "/",
"blog.blogpage": (page) => `/blog/${page.meta.slug}`, "blog.blogpage": (page) => `/blog/${page.meta.slug}`,
@@ -22,6 +22,7 @@ const PREVIEW_PATH_RESOLVERS: Record<
"cases.casestudypage": (page) => `/cases/${page.meta.slug}`, "cases.casestudypage": (page) => `/cases/${page.meta.slug}`,
"solutions.solutionpage": (page) => `/solutions/${page.meta.slug}`, "solutions.solutionpage": (page) => `/solutions/${page.meta.slug}`,
"core.contactpage": () => "/contact", "core.contactpage": () => "/contact",
"core.simplecontentpage": (page) => page.meta.slug === "about" ? "/about" : null,
}; };
/** 根据 content_type 解析预览内容应跳转到的前端路径,未知类型返回 null。 */ /** 根据 content_type 解析预览内容应跳转到的前端路径,未知类型返回 null。 */
+4 -2
View File
@@ -242,7 +242,7 @@ test("feature grid displays configured icon text", () => {
}); });
test("footer renders configured navigation, contact and social fields with CMS QR URL", async () => { test("footer renders configured navigation, contact and social fields with CMS QR URL", async () => {
const settings = { company_name: "Company", contact_phone: "123456", contact_email: "hello@example.com", address: "Address", wechat_account: "Account", weibo_url: "https://weibo.com/example", wechat_qrcode: { url: "/media/qr.png", title: "QR" } }; const settings = { company_name: "Company", contact_phone: "123456", contact_email: "hello@example.com", address: "Address", icp_number: "测试备案号", icp_url: "https://beian.miit.gov.cn/", wechat_account: "Account", weibo_url: "https://weibo.com/example", wechat_qrcode: { url: "/media/qr.png", title: "QR" } };
const { default: Footer } = loadModule("components/layout/Footer.tsx", { const { default: Footer } = loadModule("components/layout/Footer.tsx", {
"@/services/site-settings.service": { getSiteSettings: async () => settings }, "@/services/site-settings.service": { getSiteSettings: async () => settings },
"@/services/navigation.service": { getNavigationMenu: async (name) => { assert.equal(name, "footer"); return { items: [{ label: "Contact", url: "/contact", open_in_new_tab: true }] }; } }, "@/services/navigation.service": { getNavigationMenu: async (name) => { assert.equal(name, "footer"); return { items: [{ label: "Contact", url: "/contact", open_in_new_tab: true }] }; } },
@@ -262,6 +262,8 @@ test("footer renders configured navigation, contact and social fields with CMS Q
assert.ok(elements.some((element) => element.props?.src === "https://cms.example.com/media/qr.png")); assert.ok(elements.some((element) => element.props?.src === "https://cms.example.com/media/qr.png"));
assert.ok(elements.some((element) => element.props?.children === settings.address)); assert.ok(elements.some((element) => element.props?.children === settings.address));
assert.ok(elements.some((element) => element.props?.children === settings.wechat_account)); assert.ok(elements.some((element) => element.props?.children === settings.wechat_account));
assert.ok(elements.some((element) => element.props?.href === settings.icp_url));
assert.ok(elements.some((element) => element.props?.children === settings.icp_number));
const fallback = loadModule("components/layout/Footer.tsx", { const fallback = loadModule("components/layout/Footer.tsx", {
"@/services/site-settings.service": { getSiteSettings: async () => null }, "@/services/site-settings.service": { getSiteSettings: async () => null },
"@/services/navigation.service": { getNavigationMenu: async () => null }, "@/services/navigation.service": { getNavigationMenu: async () => null },
@@ -269,5 +271,5 @@ test("footer renders configured navigation, contact and social fields with CMS Q
elements.length = 0; elements.length = 0;
collect(await fallback.default()); collect(await fallback.default());
assert.ok(elements.some((element) => element.props?.href === "/privacy-policy")); assert.ok(elements.some((element) => element.props?.href === "/privacy-policy"));
assert.ok(elements.some((element) => element.props?.children === "冀ICP备2025130506号-1")); assert.ok(!elements.some((element) => element.props?.href === settings.icp_url));
}); });
+77
View File
@@ -0,0 +1,77 @@
const assert = require("node:assert/strict");
const fs = require("node:fs");
const path = require("node:path");
const vm = require("node:vm");
const { test } = require("node:test");
const ts = require("typescript");
function loadModule(relativePath, mocks = {}) {
const source = fs.readFileSync(path.join(__dirname, "..", relativePath), "utf8");
const compiled = ts.transpileModule(source, {
compilerOptions: { target: ts.ScriptTarget.ES2020, module: ts.ModuleKind.CommonJS, jsx: ts.JsxEmit.ReactJSX },
}).outputText;
const module = { exports: {} };
vm.runInNewContext(compiled, {
module, exports: module.exports, process, URLSearchParams, URL,
require: (name) => name in mocks ? mocks[name] : require(name),
});
return module.exports;
}
function nodes(element) {
if (!element || typeof element !== "object") return [];
const children = element.props?.children;
return [element, ...(Array.isArray(children) ? children.flat(Infinity) : [children]).flatMap(nodes)];
}
test("header reads company name and CMS navigation", async () => {
const { default: Header } = loadModule("components/layout/Header.tsx", {
"@/services/navigation.service": { getNavigationMenu: async () => ({ items: [{ label: "关于我们", url: "/about", open_in_new_tab: false }] }) },
"@/services/site-settings.service": { getSiteSettings: async () => ({ company_name: "测试软件公司" }) },
"./MobileNavigation": { default: () => null },
});
const tree = nodes(await Header());
assert.ok(tree.some((node) => node.props?.["aria-label"] === "测试软件公司首页"));
assert.ok(tree.some((node) => node.props?.href === "/about"));
assert.ok(tree.some((node) => node.props?.children === "测试软件公司"));
assert.ok(!tree.some((node) => node.props?.children === "世耀科技"));
});
test("footer does not invent an ICP number for an unconfigured company", async () => {
const { default: Footer } = loadModule("components/layout/Footer.tsx", {
"@/services/navigation.service": { getNavigationMenu: async () => null },
"@/services/site-settings.service": { getSiteSettings: async () => ({ company_name: "测试软件公司", icp_number: "" }) },
});
const tree = nodes(await Footer());
assert.ok(!tree.some((node) => node.props?.href === "https://beian.miit.gov.cn/"));
assert.ok(!tree.some((node) => typeof node.props?.children === "string" && node.props.children.includes("冀ICP备")));
});
test("about page uses CMS content, supports drafts and returns 404 when missing", async () => {
const page = { title: "关于我们", meta: { slug: "about" }, body: "<p>企业介绍</p>" };
let content = page;
let draft = null;
const { default: About } = loadModule("app/about/page.tsx", {
"next/navigation": { notFound: () => { throw new Error("NOT_FOUND"); } },
"@/services/legal.service": { getSimpleContentPageBySlug: async (slug) => { assert.equal(slug, "about"); return content; } },
"@/services/preview.service": { getPreviewOrFallback: async (type, fields, fallback) => { assert.equal(type, "core.simplecontentpage"); assert.equal(fields, "body"); return draft ?? fallback(); } },
"@/services/seo.service": {},
"@/components/layout/PageSEO": { default: () => null },
});
assert.ok(nodes(await About()).some((node) => node.props?.dangerouslySetInnerHTML?.__html === page.body));
draft = { ...page, body: "<p>未发布的介绍</p>" };
assert.ok(nodes(await About()).some((node) => node.props?.dangerouslySetInnerHTML?.__html === draft.body));
draft = { ...page, meta: { slug: "privacy-policy" }, body: "<p>其他页面的草稿</p>" };
assert.ok(nodes(await About()).some((node) => node.props?.dangerouslySetInnerHTML?.__html === page.body));
draft = null;
content = null;
await assert.rejects(About(), /NOT_FOUND/);
});
test("simple content preview resolves only supported about route", () => {
const { resolvePreviewPath } = loadModule("services/preview.service.ts", {
"next/headers": {}, "./api-client": {},
});
assert.equal(resolvePreviewPath("core.simplecontentpage", { meta: { slug: "about" } }), "/about");
assert.equal(resolvePreviewPath("core.simplecontentpage", { meta: { slug: "unknown" } }), null);
});