import React, { useEffect, useState } from "react"; import { createRoot } from "react-dom/client"; import { Boxes, Building2, Code2, ChevronDown, FileText, FolderTree, Globe2, HelpCircle, Image, Mail, Menu, Newspaper, PanelsTopLeft, Paintbrush, Plus, Search, Settings, ScrollText, Users, X, } from "lucide-react"; import "./styles.css"; import ProductEditor from "./ProductEditor"; import ContentEditor from "./ContentEditor"; import CategoryManager from "./CategoryManager"; import InquirySettingsPanel from "./InquirySettingsPanel"; import PageEditor from "./PageEditor"; import DecorationEditor from "./DecorationEditor"; import CustomCodeManager from "./CustomCodeManager"; import MediaCenter from "./MediaCenter"; import LoginPage, { type Account } from "./LoginPage"; import SettingsPage from "./SettingsPage"; import SeoSettingsPage from "./SeoSettingsPage"; import NavigationManager from "./NavigationManager"; import AgreementSettingsPage from "./AgreementSettingsPage"; import AboutUsSettingsPage from "./AboutUsSettingsPage"; import { apiPost } from "./api"; import { statusLabel } from "./statusLabels"; const SITE_WEB = window.location.hostname === "admin.steliro.com" ? "https://site.steliro.com" : "http://localhost:5174"; const SITE = "SIT0000000000001"; type Section = | "products" | "product-categories" | "NEWS" | "NEWS-categories" | "CASE" | "CASE-categories" | "HELP" | "HELP-categories" | "pages" | "navigation" | "seo-settings" | "decoration" | "custom-code" | "agreements" | "about-us" | "media" | "inquiries" | "inquiry-settings" | "settings"; type EditorialSection = "NEWS" | "CASE" | "HELP"; type ContentEditorState = { contentType: EditorialSection; contentCode: string; }; type Item = { productCode?: string; contentCode?: string; pageCode?: string; productName?: string; title?: string; pageName?: string; slug: string; status: string; model?: string; publishTime?: number; inquiryCode?: string; customerName?: string; customerEmail?: string; companyName?: string; countryCode?: string; countryName?: string; provinceName?: string; cityName?: string; clientIp?: string; readStatus?: number; inquiryTitle?: string; sourceType?: string; statusLabel?: string; createTime?: number; }; /** 将毫秒时间戳格式化为本地列表时间。 */ function formatTime(value?: number): string { if (!value) return "--"; return new Intl.DateTimeFormat("zh-CN", { year: "numeric", month: "2-digit", day: "2-digit", hour: "2-digit", minute: "2-digit", hour12: false, }).format(new Date(value)); } async function post(path: string, body: unknown) { return apiPost(path, body); } const frontSiteMenus = [ { key: "pages" as Section, label: "页面", icon: FileText }, { key: "about-us" as Section, label: "关于我们", icon: Building2 }, { key: "navigation" as Section, label: "菜单导航", icon: Menu }, { key: "decoration" as Section, label: "店铺装修", icon: Paintbrush }, { key: "custom-code" as Section, label: "自定义代码", icon: Code2 }, { key: "seo-settings" as Section, label: "页面 SEO", icon: Search }, { key: "agreements" as Section, label: "协议设置", icon: ScrollText }, ]; const menus = [ { key: "media" as Section, label: "素材中心", icon: Image }, { key: "inquiries" as Section, label: "询盘", icon: Mail }, ]; const sectionLabels: Record = { products: "产品", "product-categories": "产品分类", NEWS: "新闻", "NEWS-categories": "新闻分类", CASE: "案例", "CASE-categories": "案例分类", HELP: "帮助", "HELP-categories": "帮助分类", pages: "页面", navigation: "菜单导航", decoration: "店铺装修", "custom-code": "自定义代码", media: "素材中心", inquiries: "询盘", "inquiry-settings": "询盘设置", "seo-settings": "页面 SEO", agreements: "协议设置", "about-us": "关于我们", settings: "系统设置", }; function App({ account, onAccount, onLogout, }: { account: Account; onAccount: (value: Account) => void; onLogout: () => void; }) { const contentRoutes: Record = { NEWS: "news", CASE: "cases", HELP: "help", }; const resolveContentRoute = (): ContentEditorState | null => { const match = window.location.pathname.match( /^\/(news|cases|help)\/(new|([^/]+)\/edit)$/, ); if (!match) return null; const contentType = ( { news: "NEWS", cases: "CASE", help: "HELP" } as const )[match[1] as "news" | "cases" | "help"]; return { contentType, contentCode: match[2] === "new" ? "" : decodeURIComponent(match[3]), }; }; const initialContentEditor = resolveContentRoute(); const [section, setSection] = useState
( initialContentEditor?.contentType || (window.location.pathname.startsWith("/pages/") ? "pages" : "products"), ); const [items, setItems] = useState([]); const [error, setError] = useState(""); const [query, setQuery] = useState(""); const [open, setOpen] = useState(false); const [saving, setSaving] = useState(false); const resolveProductRoute = () => { if (window.location.pathname === "/products/new") return ""; const match = window.location.pathname.match(/^\/products\/([^/]+)\/edit$/); return match ? decodeURIComponent(match[1]) : null; }; const resolvePageRoute = () => { if (window.location.pathname === "/pages/new") return ""; const match = window.location.pathname.match(/^\/pages\/([^/]+)\/edit$/); return match ? decodeURIComponent(match[1]) : null; }; const [editingProduct, setEditingProduct] = useState( resolveProductRoute, ); const [editingContent, setEditingContent] = useState(initialContentEditor); const [editingPage, setEditingPage] = useState( resolvePageRoute, ); const [productMenuOpen, setProductMenuOpen] = useState(true); const [contentMenuOpen, setContentMenuOpen] = useState(true); const [frontSiteMenuOpen, setFrontSiteMenuOpen] = useState(true); const navigateSection = (next: Section) => { setEditingProduct(null); setEditingContent(null); setEditingPage(null); setSection(next); window.history.pushState({}, "", "/"); }; const [form, setForm] = useState({ name: "", slug: "", summary: "", body: "", model: "", status: "DRAFT", }); const load = async () => { try { setError(""); const data = section.endsWith("-categories") ? { items: [] } : section === "settings" ? { items: [] } : section === "navigation" ? { items: [] } : section === "agreements" ? { items: [] } : section === "about-us" ? { items: [] } : section === "products" ? await post("/products/list", { siteCode: SITE }) : section === "pages" ? await post("/pages/list", { siteCode: SITE }) : section === "decoration" ? { items: [] } : section === "inquiries" ? await post("/inquiries/list", { siteCode: SITE }) : section === "inquiry-settings" ? { items: [] } : await post("/contents/list", { siteCode: SITE, contentType: section, }); setItems(data.items || []); } catch (e) { setError(e instanceof Error ? e.message : "加载失败"); setItems([]); } }; useEffect(() => { void load(); }, [section]); useEffect(() => { const handlePopState = () => { setEditingProduct(resolveProductRoute()); setEditingPage(resolvePageRoute()); const content = resolveContentRoute(); setEditingContent(content); if (content) setSection(content.contentType); }; window.addEventListener("popstate", handlePopState); return () => window.removeEventListener("popstate", handlePopState); }, []); const openProductEditor = (productCode?: string) => { const path = productCode ? `/products/${encodeURIComponent(productCode)}/edit` : "/products/new"; window.history.pushState({}, "", path); setEditingProduct(productCode || ""); }; const closeProductEditor = () => { window.history.pushState({}, "", "/"); setEditingProduct(null); void load(); }; const openContentEditor = ( contentType: EditorialSection, contentCode?: string, ) => { const route = contentRoutes[contentType]; const path = contentCode ? `/${route}/${encodeURIComponent(contentCode)}/edit` : `/${route}/new`; window.history.pushState({}, "", path); setEditingContent({ contentType, contentCode: contentCode || "" }); }; const closeContentEditor = () => { window.history.pushState({}, "", "/"); setEditingContent(null); }; const openPageEditor = (pageCode?: string) => { window.history.pushState( {}, "", pageCode ? `/pages/${encodeURIComponent(pageCode)}/edit` : "/pages/new", ); setEditingPage(pageCode || ""); }; const closePageEditor = () => { window.history.pushState({}, "", "/"); setEditingPage(null); }; const title = sectionLabels[section]; const filtered = items.filter((x) => JSON.stringify(x).toLowerCase().includes(query.toLowerCase()), ); const changeItemStatus = async (item: Item) => { const status = item.status === "PUBLISHED" ? "OFFLINE" : "PUBLISHED"; try { setError(""); if (section === "products" && item.productCode) await post("/products/status", { siteCode: SITE, productCode: item.productCode, status, }); else if (["NEWS", "CASE", "HELP"].includes(section) && item.contentCode) await post("/contents/status", { siteCode: SITE, contentCode: item.contentCode, contentType: section, status, }); await load(); } catch (operationError) { setError( operationError instanceof Error ? operationError.message : "状态更新失败", ); } }; const deleteItem = async (item: Item) => { if (!window.confirm("删除后该内容将不再展示,确认删除吗?")) return; try { setError(""); if (section === "products" && item.productCode) await post("/products/delete", { siteCode: SITE, productCode: item.productCode, }); else if (["NEWS", "CASE", "HELP"].includes(section) && item.contentCode) await post("/contents/delete", { siteCode: SITE, contentCode: item.contentCode, contentType: section, }); await load(); } catch (operationError) { setError( operationError instanceof Error ? operationError.message : "删除失败", ); } }; const save = async () => { try { setSaving(true); setError(""); if (section === "products") await post("/products/save", { siteCode: SITE, productName: form.name, slug: form.slug, model: form.model, summary: form.summary, productContent: form.body, featured: false, inquiryEnabled: true, sortNo: 0, status: form.status, specifications: [], }); else if (section === "pages") await post("/pages/save", { siteCode: SITE, pageName: form.name, slug: form.slug, summary: form.summary, pageContent: form.body, systemPage: false, sortNo: 0, status: form.status, }); else await post("/contents/save", { siteCode: SITE, contentType: section, title: form.name, slug: form.slug, summary: form.summary, contentBody: form.body, featured: false, sortNo: 0, status: form.status, }); setOpen(false); setForm({ name: "", slug: "", summary: "", body: "", model: "", status: "DRAFT", }); await load(); } catch (e) { setError(e instanceof Error ? e.message : "保存失败"); } finally { setSaving(false); } }; return (
{section === "settings" ? ( ) : section === "about-us" ? ( ) : section === "seo-settings" ? ( ) : section === "agreements" ? ( ) : section === "navigation" ? ( ) : section === "decoration" ? ( navigateSection("pages")} /> ) : section === "custom-code" ? ( ) : section === "media" ? ( ) : editingProduct !== null ? ( ) : editingContent !== null ? ( ) : editingPage !== null ? ( ) : ( <> {section.endsWith("-categories") ? ( ) : ( <>

{title}管理

{!["inquiries", "inquiry-settings"].includes(section) && (
{["NEWS", "CASE", "HELP"].includes(section) && ( )}
)}
{section === "inquiry-settings" ? ( ) : ( <>
setQuery(e.target.value)} placeholder={`搜索${title}名称或路径`} />
{error && (
{error}。请先启动 Admin 后端,并初始化本地站点数据。
)}
{section === "inquiries" ? ( <> 公司名称 电子邮箱 国家 / IP地址 询盘时间 询盘来源 状态 操作 ) : ( <> 名称 英文路径 状态 发布时间 操作 )}
{filtered.length === 0 && !error ? (
暂无{title}数据

点击右上角新建按钮录入第一条内容。

) : ( filtered.map((item, index) => (
{section === "inquiries" ? ( <> {item.companyName || item.customerName || "--"} {item.customerName || "--"} {item.customerEmail || "--"} {item.countryName || item.countryCode || "未知"} ({item.clientIp || "--"}) {formatTime(item.createTime)} {item.productName || item.inquiryTitle || item.sourceType || "网站询盘"} {item.readStatus === 0 ? "未读" : item.statusLabel || statusLabel(item.status)}
) : ( <> {item.productName || item.title || item.pageName} {item.model || item.productCode || item.contentCode || item.pageCode} /{item.slug} {statusLabel(item.status)} {formatTime(item.publishTime)}
{[ "products", "NEWS", "CASE", "HELP", ].includes(section) && item.slug && ( 查看前台 )} {[ "products", "NEWS", "CASE", "HELP", ].includes(section) && ( <> )}
)}
)) )}
)} )} )}
{open && (

新建{title}

前台内容统一使用英文录入

{section === "products" && ( )}