101 lines
8.6 KiB
TypeScript
101 lines
8.6 KiB
TypeScript
import React, { useEffect, useMemo, useState } from "react";
|
|
import { AlertCircle, Bold, CheckCircle2, Heading2, Italic, Link as LinkIcon, List, Redo2, Save, Undo2, X } from "lucide-react";
|
|
import { EditorContent, useEditor } from "@tiptap/react";
|
|
import StarterKit from "@tiptap/starter-kit";
|
|
import Link from "@tiptap/extension-link";
|
|
import { apiPost } from "./api";
|
|
|
|
const SITE = "SIT0000000000001";
|
|
const AGREEMENT_PUBLISH_STATUS = "PUBLISHED";
|
|
type AgreementKey = "COOKIE_POLICY" | "PRIVACY_POLICY" | "TERMS_OF_SERVICE";
|
|
type AgreementTab = { key: AgreementKey; label: string; pageName: string; slug: string; seoDescription:string; robots:string };
|
|
type AgreementSeo={title:string;description:string;canonicalUrl:string;robots:string;h1:string};
|
|
const tabs: AgreementTab[] = [
|
|
{ key: "COOKIE_POLICY", label: "Cookies协议", pageName: "Cookies Policy", slug: "cookie-policy",seoDescription:"Learn how this website uses cookies to improve browsing and website functionality.",robots:"noindex,follow" },
|
|
{ key: "PRIVACY_POLICY", label: "隐私协议", pageName: "Privacy Policy", slug: "privacy-policy",seoDescription:"Learn how Shoplix collects, uses and protects personal information.",robots:"index,follow" },
|
|
{ key: "TERMS_OF_SERVICE", label: "服务协议", pageName: "Terms of Use", slug: "terms-of-service",seoDescription:"Review the terms and conditions that govern the use of the Shoplix website and services.",robots:"index,follow" },
|
|
];
|
|
|
|
/** 创建协议页的基础 SEO 默认值。 */
|
|
function defaultSeo(tab:AgreementTab):AgreementSeo{return{title:`${tab.pageName} | Shoplix`,description:tab.seoDescription,canonicalUrl:`/${tab.slug}`,robots:tab.robots,h1:tab.pageName}}
|
|
|
|
/** 固定协议页面配置,支持 Cookies、隐私和服务协议独立维护。 */
|
|
export default function AgreementSettingsPage() {
|
|
const [activeKey, setActiveKey] = useState<AgreementKey>("COOKIE_POLICY");
|
|
const [pageCodes, setPageCodes] = useState<Partial<Record<AgreementKey, string>>>({});
|
|
const [title, setTitle] = useState("");
|
|
const [notice, setNotice] = useState("");
|
|
const [noticeType,setNoticeType]=useState<"success"|"error">("success");
|
|
const [saving, setSaving] = useState(false);
|
|
const [seo,setSeo]=useState<AgreementSeo>(()=>defaultSeo(tabs[0]));
|
|
const activeTab = useMemo(() => tabs.find((tab) => tab.key === activeKey) || tabs[0], [activeKey]);
|
|
const editor = useEditor({ extensions: [StarterKit, Link.configure({ openOnClick: false })], content: "<p></p>" });
|
|
|
|
useEffect(() => {
|
|
void loadPages();
|
|
}, []);
|
|
useEffect(()=>{if(!notice||noticeType!=="success")return;const timer=window.setTimeout(()=>setNotice(""),3000);return()=>window.clearTimeout(timer)},[notice,noticeType]);
|
|
useEffect(() => {
|
|
const code = pageCodes[activeKey];
|
|
setSeo(defaultSeo(activeTab));
|
|
if (!code) {
|
|
setTitle(activeTab.pageName);
|
|
editor?.commands.setContent("<p></p>");
|
|
return;
|
|
}
|
|
void apiPost("/pages/detail", { siteCode: SITE, pageCode: code }).then((data) => {
|
|
setTitle(data.pageName || activeTab.pageName);
|
|
editor?.commands.setContent(data.pageContent || "<p></p>");
|
|
return apiPost("/seo/detail",{siteCode:SITE,pageType:"SYSTEM_PAGE",targetCode:code}).catch(()=>undefined);
|
|
}).then((seoData)=>{
|
|
if(!seoData)return;
|
|
setSeo({title:seoData.title||defaultSeo(activeTab).title,description:seoData.description||defaultSeo(activeTab).description,canonicalUrl:seoData.canonicalUrl||`/${activeTab.slug}`,robots:seoData.robots||activeTab.robots,h1:seoData.h1||activeTab.pageName});
|
|
}).catch((error) => {setNoticeType("error");setNotice(error instanceof Error ? error.message : "加载失败")});
|
|
}, [activeKey, pageCodes, editor, activeTab]);
|
|
|
|
async function loadPages() {
|
|
try {
|
|
const data = await apiPost("/pages/list", { siteCode: SITE });
|
|
const codes: Partial<Record<AgreementKey, string>> = {};
|
|
(data.items || []).forEach((item: { pageCode: string; systemPageType: AgreementKey }) => {
|
|
if (tabs.some((tab) => tab.key === item.systemPageType)) codes[item.systemPageType] = item.pageCode;
|
|
});
|
|
setPageCodes(codes);
|
|
} catch (error) {
|
|
setNoticeType("error");setNotice(error instanceof Error ? error.message : "加载失败");
|
|
}
|
|
}
|
|
|
|
async function save() {
|
|
if (!editor || !title.trim()) return;
|
|
try {
|
|
setSaving(true);
|
|
setNotice("");
|
|
const result = await apiPost("/pages/save", {
|
|
pageCode: pageCodes[activeKey], siteCode: SITE, pageType: "SYSTEM_PAGE", systemPageType: activeKey,
|
|
pageName: title.trim(), slug: activeTab.slug, pageContent: editor.getHTML(), summary: "", coverMediaCode: null,
|
|
sortNo: 30, status: AGREEMENT_PUBLISH_STATUS,
|
|
});
|
|
setPageCodes((current) => ({ ...current, [activeKey]: result.pageCode }));
|
|
await apiPost("/seo/save",{siteCode:SITE,pageType:"SYSTEM_PAGE",targetCode:result.pageCode,title:seo.title.trim()||title.trim(),description:seo.description.trim(),keywords:"",h1:seo.h1.trim()||title.trim(),canonicalUrl:seo.canonicalUrl.trim()||`/${activeTab.slug}`,robots:seo.robots,ogTitle:"",ogDescription:"",ogImageUrl:"",structuredData:null,status:AGREEMENT_PUBLISH_STATUS,headings:[]});
|
|
setNoticeType("success");setNotice("协议与 SEO 已保存");
|
|
} catch (error) {
|
|
setNoticeType("error");setNotice(error instanceof Error ? error.message : "保存失败");
|
|
} finally {
|
|
setSaving(false);
|
|
}
|
|
}
|
|
|
|
const addLink = () => { const href = window.prompt("请输入链接地址"); if (href) editor?.chain().focus().setLink({ href }).run(); };
|
|
return <div className="agreement-settings">
|
|
<div className="agreement-top"><div><h1>协议设置</h1><p>维护网站 Cookies、隐私和服务协议内容。</p></div><button className="primary" disabled={saving || !title.trim()} onClick={() => void save()}><Save size={16}/>{saving ? "保存中" : "保存"}</button></div>
|
|
<div className="agreement-tabs">{tabs.map((tab) => <button key={tab.key} className={tab.key === activeKey ? "active" : ""} onClick={() => setActiveKey(tab.key)}>{tab.label}</button>)}</div>
|
|
<section className="agreement-card"><div className="agreement-card-head"><h2>{activeTab.label}</h2></div>
|
|
<label className="agreement-title"><span>页面标题</span><input value={title} onChange={(event) => setTitle(event.target.value)} maxLength={100}/></label>
|
|
<label className="agreement-content"><span>页面详情</span><div className="rich-editor"><div className="rich-toolbar"><Tool title="撤销" onClick={() => editor?.chain().focus().undo().run()}><Undo2/></Tool><Tool title="重做" onClick={() => editor?.chain().focus().redo().run()}><Redo2/></Tool><Tool title="粗体" onClick={() => editor?.chain().focus().toggleBold().run()}><Bold/></Tool><Tool title="斜体" onClick={() => editor?.chain().focus().toggleItalic().run()}><Italic/></Tool><Tool title="二级标题" onClick={() => editor?.chain().focus().toggleHeading({ level: 2 }).run()}><Heading2/></Tool><Tool title="项目列表" onClick={() => editor?.chain().focus().toggleBulletList().run()}><List/></Tool><Tool title="链接" onClick={addLink}><LinkIcon/></Tool></div><EditorContent editor={editor}/></div></label>
|
|
</section>
|
|
<section className="agreement-card agreement-seo"><div className="agreement-card-head"><h2>SEO 设置</h2></div><div className="agreement-seo-grid"><label><span>SEO 标题 *</span><input maxLength={70} value={seo.title} onChange={(event)=>setSeo({...seo,title:event.target.value})}/></label><label><span>页面 H1 *</span><input maxLength={120} value={seo.h1} onChange={(event)=>setSeo({...seo,h1:event.target.value})}/></label><label className="wide"><span>SEO 描述 *</span><textarea maxLength={160} rows={3} value={seo.description} onChange={(event)=>setSeo({...seo,description:event.target.value})}/></label><label><span>Canonical *</span><input value={seo.canonicalUrl} onChange={(event)=>setSeo({...seo,canonicalUrl:event.target.value})}/></label><label><span>Robots</span><select value={seo.robots} onChange={(event)=>setSeo({...seo,robots:event.target.value})}><option value="index,follow">允许收录并跟踪链接</option><option value="noindex,follow">不收录但跟踪链接</option></select></label></div></section>{notice && <div className={`agreement-toast ${noticeType}`} role={noticeType==="error"?"alert":"status"}>{noticeType==="success"?<CheckCircle2 size={19}/>:<AlertCircle size={19}/>}<span>{notice}</span><button type="button" title="关闭提示" onClick={()=>setNotice("")}><X size={16}/></button></div>}
|
|
</div>;
|
|
}
|
|
function Tool({ title, onClick, children }: { title: string; onClick: () => void; children: React.ReactElement }) { return <button type="button" title={title} onClick={onClick}>{React.cloneElement(children, { size: 18 } as never)}</button>; }
|