import React, { useEffect, useState } from "react"; import { Save } from "lucide-react"; import { apiPost } from "./api"; /** 当前后台维护的站点业务编号。 */ const SITE = "SIT0000000000001"; /** 固定页面 SEO 类型定义。 */ type SeoPageOption = { pageType: string; label: string; canonical: string; defaultTitle: string; defaultDescription: string; }; /** 固定页面 SEO 编辑模型。 */ type SeoForm = { title: string; description: string; keywords: string; h1: string; canonicalUrl: string; robots: string; ogTitle: string; ogDescription: string; ogImageUrl: string; structuredData: string; status: string; }; /** 支持独立配置的固定页面。 */ const pages: SeoPageOption[] = [ { pageType: "HOME", label: "首页", canonical: "/", defaultTitle: "Shoplix", defaultDescription: "Industrial products and solutions." }, { pageType: "PRODUCT_LIST", label: "产品分类页", canonical: "/products", defaultTitle: "Products | Shoplix", defaultDescription: "Explore our industrial products and technical solutions." }, { pageType: "NEWS_LIST", label: "新闻分类页", canonical: "/news", defaultTitle: "News | Shoplix", defaultDescription: "Read the latest company and industry news." }, { pageType: "CASE_LIST", label: "案例分类页", canonical: "/cases", defaultTitle: "Case Studies | Shoplix", defaultDescription: "Explore our project cases and industrial applications." }, { pageType: "HELP_CATEGORY_LIST", label: "帮助分类页", canonical: "/help", defaultTitle: "Help Center | Shoplix", defaultDescription: "Find product guidance and frequently asked questions." }, ]; /** 创建指定固定页面的默认 SEO 内容。 */ function defaultForm(page: SeoPageOption): SeoForm { return { title: page.defaultTitle, description: page.defaultDescription, keywords: "", h1: page.label, canonicalUrl: page.canonical, robots: "index,follow", ogTitle: "", ogDescription: "", ogImageUrl: "", structuredData: "", status: "DRAFT", }; } /** 首页及各内容分类入口的 SEO 设置页。 */ export default function SeoSettingsPage() { const [selected, setSelected] = useState(pages[0]); const [form, setForm] = useState(() => defaultForm(pages[0])); const [loading, setLoading] = useState(false); const [saving, setSaving] = useState(false); const [notice, setNotice] = useState(""); /** 加载当前固定页面的 SEO 配置,不存在时展示可直接保存的默认内容。 */ useEffect(() => { let active = true; setLoading(true); setNotice(""); setForm(defaultForm(selected)); apiPost("/seo/detail", { siteCode: SITE, pageType: selected.pageType, targetCode: "" }) .then((data) => { if (!active) return; setForm({ title: data.title || selected.defaultTitle, description: data.description || selected.defaultDescription, keywords: data.keywords || "", h1: data.h1 || selected.label, canonicalUrl: data.canonicalUrl || selected.canonical, robots: data.robots || "index,follow", ogTitle: data.ogTitle || "", ogDescription: data.ogDescription || "", ogImageUrl: data.ogImageUrl || "", structuredData: data.structuredData || "", status: data.status || "DRAFT", }); }) .catch(() => undefined) .finally(() => { if (active) setLoading(false); }); return () => { active = false; }; }, [selected]); /** 保存当前固定页面 SEO 配置。 */ const save = async () => { try { setSaving(true); setNotice(""); await apiPost("/seo/save", { siteCode: SITE, pageType: selected.pageType, targetCode: "", ...form, ogTitle: form.ogTitle || form.title, ogDescription: form.ogDescription || form.description, structuredData: form.structuredData || null, headings: [], }); setNotice(`${selected.label} SEO 设置已保存`); } catch (error) { setNotice(error instanceof Error ? error.message : "保存失败"); } finally { setSaving(false); } }; return (

页面 SEO 设置

分别维护首页及各内容分类入口的搜索引擎信息。

{notice &&
{notice}
}