144 lines
7.6 KiB
TypeScript
144 lines
7.6 KiB
TypeScript
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<SeoForm>(() => 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 (
|
||
|
|
<div className="seo-settings-page">
|
||
|
|
<div className="seo-settings-heading">
|
||
|
|
<div><h1>页面 SEO 设置</h1><p>分别维护首页及各内容分类入口的搜索引擎信息。</p></div>
|
||
|
|
<button className="primary" disabled={saving || loading || !form.title || !form.description || !form.h1} onClick={() => void save()}><Save size={17}/>{saving ? "保存中" : "保存"}</button>
|
||
|
|
</div>
|
||
|
|
<nav className="seo-page-tabs" aria-label="固定页面 SEO 类型">
|
||
|
|
{pages.map((page) => <button key={page.pageType} className={selected.pageType === page.pageType ? "active" : ""} onClick={() => setSelected(page)}>{page.label}</button>)}
|
||
|
|
</nav>
|
||
|
|
{notice && <div className="notice">{notice}</div>}
|
||
|
|
<section className="seo-settings-card">
|
||
|
|
<div className="seo-settings-form">
|
||
|
|
<label className="wide"><span>SEO 标题 *</span><input maxLength={70} value={form.title} onChange={(event) => setForm({ ...form, title: event.target.value })}/><small>{form.title.length} / 70</small></label>
|
||
|
|
<label className="wide"><span>SEO 描述 *</span><textarea rows={4} maxLength={160} value={form.description} onChange={(event) => setForm({ ...form, description: event.target.value })}/><small>{form.description.length} / 160</small></label>
|
||
|
|
<label className="wide"><span>SEO 关键词</span><input maxLength={255} placeholder="多个关键词使用英文逗号分隔" value={form.keywords} onChange={(event) => setForm({ ...form, keywords: event.target.value })}/></label>
|
||
|
|
<label><span>页面 H1 *</span><input maxLength={120} value={form.h1} onChange={(event) => setForm({ ...form, h1: event.target.value })}/></label>
|
||
|
|
<label><span>Canonical</span><input maxLength={500} placeholder={selected.canonical} value={form.canonicalUrl} onChange={(event) => setForm({ ...form, canonicalUrl: event.target.value })}/></label>
|
||
|
|
<label><span>Robots</span><select value={form.robots} onChange={(event) => setForm({ ...form, robots: event.target.value })}><option value="index,follow">允许索引并跟踪链接</option><option value="index,nofollow">允许索引、不跟踪链接</option><option value="noindex,follow">不索引、跟踪链接</option><option value="noindex,nofollow">不索引且不跟踪链接</option></select></label>
|
||
|
|
<label><span>发布状态</span><select value={form.status} onChange={(event) => setForm({ ...form, status: event.target.value })}><option value="DRAFT">草稿</option><option value="PUBLISHED">已发布</option><option value="OFFLINE">已下线</option></select></label>
|
||
|
|
<label><span>Open Graph 标题</span><input maxLength={100} placeholder="默认使用 SEO 标题" value={form.ogTitle} onChange={(event) => setForm({ ...form, ogTitle: event.target.value })}/></label>
|
||
|
|
<label><span>Open Graph 图片</span><input maxLength={500} placeholder="https://..." value={form.ogImageUrl} onChange={(event) => setForm({ ...form, ogImageUrl: event.target.value })}/></label>
|
||
|
|
<label className="wide"><span>Open Graph 描述</span><textarea rows={3} maxLength={200} placeholder="默认使用 SEO 描述" value={form.ogDescription} onChange={(event) => setForm({ ...form, ogDescription: event.target.value })}/></label>
|
||
|
|
<label className="wide"><span>JSON-LD 结构化数据</span><textarea className="code-input" rows={7} placeholder='例如 {"@context":"https://schema.org"}' value={form.structuredData} onChange={(event) => setForm({ ...form, structuredData: event.target.value })}/></label>
|
||
|
|
</div>
|
||
|
|
</section>
|
||
|
|
</div>
|
||
|
|
);
|
||
|
|
}
|