bug修复
This commit is contained in:
1 parent
b9cc84644c
commit
ec4ca451bb
8 files changed
+177
-36
No files matched your search
@@ -1,11 +1,12 @@
|
||||
import React, { useEffect, useMemo, useState } from "react";
|
||||
import { Bold, Heading2, Italic, Link as LinkIcon, List, Redo2, Save, Undo2 } from "lucide-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};
|
||||
@@ -23,8 +24,8 @@ export default function AgreementSettingsPage() {
|
||||
const [activeKey, setActiveKey] = useState<AgreementKey>("COOKIE_POLICY");
|
||||
const [pageCodes, setPageCodes] = useState<Partial<Record<AgreementKey, string>>>({});
|
||||
const [title, setTitle] = useState("");
|
||||
const [status, setStatus] = useState("DRAFT");
|
||||
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]);
|
||||
@@ -33,24 +34,23 @@ export default function AgreementSettingsPage() {
|
||||
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);
|
||||
setStatus("DRAFT");
|
||||
editor?.commands.setContent("<p></p>");
|
||||
return;
|
||||
}
|
||||
void apiPost("/pages/detail", { siteCode: SITE, pageCode: code }).then((data) => {
|
||||
setTitle(data.pageName || activeTab.pageName);
|
||||
setStatus(data.status || "DRAFT");
|
||||
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) => setNotice(error instanceof Error ? error.message : "加载失败"));
|
||||
}).catch((error) => {setNoticeType("error");setNotice(error instanceof Error ? error.message : "加载失败")});
|
||||
}, [activeKey, pageCodes, editor, activeTab]);
|
||||
|
||||
async function loadPages() {
|
||||
@@ -62,7 +62,7 @@ export default function AgreementSettingsPage() {
|
||||
});
|
||||
setPageCodes(codes);
|
||||
} catch (error) {
|
||||
setNotice(error instanceof Error ? error.message : "加载失败");
|
||||
setNoticeType("error");setNotice(error instanceof Error ? error.message : "加载失败");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,13 +74,13 @@ export default function AgreementSettingsPage() {
|
||||
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,
|
||||
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,headings:[]});
|
||||
setNotice("协议与 SEO 已保存");
|
||||
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) {
|
||||
setNotice(error instanceof Error ? error.message : "保存失败");
|
||||
setNoticeType("error");setNotice(error instanceof Error ? error.message : "保存失败");
|
||||
} finally {
|
||||
setSaving(false);
|
||||
}
|
||||
@@ -90,11 +90,11 @@ export default function AgreementSettingsPage() {
|
||||
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><select value={status} onChange={(event) => setStatus(event.target.value)}><option value="DRAFT">草稿</option><option value="PUBLISHED">已上架</option><option value="OFFLINE">已下架</option></select></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 && <p className="agreement-notice">{notice}</p>}
|
||||
<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>; }
|
||||
Reference in new issue
Block a user