Files
shoplix/web-admin/src/main.tsx
T

999 lines
34 KiB
TypeScript
Raw Normal View History

2026-11-21 14:09:59 +08:00
import React, { useEffect, useState } from "react";
import { createRoot } from "react-dom/client";
import {
Boxes,
Code2,
ChevronDown,
FileText,
FolderTree,
2026-11-23 11:09:03 +08:00
Globe2,
2026-11-21 14:09:59 +08:00
HelpCircle,
Image,
2026-11-21 14:09:59 +08:00
Mail,
2026-11-23 11:09:03 +08:00
Menu,
2026-11-21 14:09:59 +08:00
Newspaper,
PanelsTopLeft,
Paintbrush,
Plus,
Search,
Settings,
2026-11-23 14:45:29 +08:00
ScrollText,
2026-11-21 14:09:59 +08:00
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";
2026-11-22 10:14:44 +08:00
import LoginPage, { type Account } from "./LoginPage";
import SettingsPage from "./SettingsPage";
2026-11-23 09:34:28 +08:00
import SeoSettingsPage from "./SeoSettingsPage";
2026-11-23 11:09:03 +08:00
import NavigationManager from "./NavigationManager";
2026-11-23 14:45:29 +08:00
import AgreementSettingsPage from "./AgreementSettingsPage";
2026-11-22 10:14:44 +08:00
import { apiPost } from "./api";
2026-11-24 14:49:58 +08:00
import { statusLabel } from "./statusLabels";
2026-11-21 14:09:59 +08:00
2026-11-24 14:49:58 +08:00
const SITE_WEB =
window.location.hostname === "admin.steliro.com"
? "https://site.steliro.com"
: "http://localhost:5174";
2026-11-21 14:09:59 +08:00
const SITE = "SIT0000000000001";
2026-11-24 14:49:58 +08:00
type Section =
| "products"
| "product-categories"
| "NEWS"
| "NEWS-categories"
| "CASE"
| "CASE-categories"
| "HELP"
| "HELP-categories"
| "pages"
| "navigation"
| "seo-settings"
| "decoration"
| "custom-code"
| "agreements"
| "media"
| "inquiries"
| "inquiry-settings"
| "settings";
2026-11-21 14:09:59 +08:00
type EditorialSection = "NEWS" | "CASE" | "HELP";
2026-11-24 14:49:58 +08:00
type ContentEditorState = {
contentType: EditorialSection;
contentCode: string;
};
2026-11-21 14:09:59 +08:00
type Item = {
productCode?: string;
contentCode?: string;
pageCode?: string;
productName?: string;
title?: string;
pageName?: string;
slug: string;
status: string;
model?: string;
2026-11-22 12:49:09 +08:00
publishTime?: number;
2026-11-21 14:09:59 +08:00
inquiryCode?: string;
customerName?: string;
customerEmail?: string;
companyName?: string;
2026-11-21 18:29:38 +08:00
countryCode?: string;
2026-11-21 18:46:19 +08:00
countryName?: string;
provinceName?: string;
cityName?: string;
2026-11-21 18:29:38 +08:00
clientIp?: string;
2026-11-21 18:46:19 +08:00
readStatus?: number;
inquiryTitle?: string;
2026-11-21 18:29:38 +08:00
sourceType?: string;
2026-11-21 14:09:59 +08:00
statusLabel?: string;
2026-11-22 12:49:09 +08:00
createTime?: number;
2026-11-21 14:09:59 +08:00
};
2026-11-22 12:49:09 +08:00
/** 将毫秒时间戳格式化为本地列表时间。 */
function formatTime(value?: number): string {
if (!value) return "--";
return new Intl.DateTimeFormat("zh-CN", {
2026-11-24 14:49:58 +08:00
year: "numeric",
month: "2-digit",
day: "2-digit",
hour: "2-digit",
minute: "2-digit",
hour12: false,
2026-11-22 12:49:09 +08:00
}).format(new Date(value));
}
2026-11-21 14:09:59 +08:00
async function post(path: string, body: unknown) {
2026-11-22 10:14:44 +08:00
return apiPost(path, body);
2026-11-21 14:09:59 +08:00
}
2026-11-23 11:09:03 +08:00
const frontSiteMenus = [
2026-11-21 14:09:59 +08:00
{ key: "pages" as Section, label: "页面", icon: FileText },
2026-11-23 11:09:03 +08:00
{ key: "navigation" as Section, label: "菜单导航", icon: Menu },
2026-11-21 14:09:59 +08:00
{ key: "decoration" as Section, label: "店铺装修", icon: Paintbrush },
{ key: "custom-code" as Section, label: "自定义代码", icon: Code2 },
2026-11-23 11:09:03 +08:00
{ key: "seo-settings" as Section, label: "页面 SEO", icon: Search },
2026-11-23 14:45:29 +08:00
{ key: "agreements" as Section, label: "协议设置", icon: ScrollText },
2026-11-23 11:09:03 +08:00
];
const menus = [
{ key: "media" as Section, label: "素材中心", icon: Image },
2026-11-21 14:09:59 +08:00
{ key: "inquiries" as Section, label: "询盘", icon: Mail },
];
const sectionLabels: Record<Section, string> = {
2026-11-24 14:49:58 +08:00
products: "产品",
"product-categories": "产品分类",
NEWS: "新闻",
"NEWS-categories": "新闻分类",
CASE: "案例",
"CASE-categories": "案例分类",
HELP: "帮助",
"HELP-categories": "帮助分类",
pages: "页面",
navigation: "菜单导航",
decoration: "店铺装修",
"custom-code": "自定义代码",
media: "素材中心",
inquiries: "询盘",
"inquiry-settings": "询盘设置",
2026-11-23 09:34:28 +08:00
"seo-settings": "页面 SEO",
2026-11-23 14:45:29 +08:00
agreements: "协议设置",
2026-11-22 10:14:44 +08:00
settings: "系统设置",
2026-11-21 14:09:59 +08:00
};
2026-11-24 14:49:58 +08:00
function App({
account,
onAccount,
onLogout,
}: {
account: Account;
onAccount: (value: Account) => void;
onLogout: () => void;
}) {
const contentRoutes: Record<EditorialSection, string> = {
NEWS: "news",
CASE: "cases",
HELP: "help",
};
2026-11-21 14:09:59 +08:00
const resolveContentRoute = (): ContentEditorState | null => {
2026-11-24 14:49:58 +08:00
const match = window.location.pathname.match(
/^\/(news|cases|help)\/(new|([^/]+)\/edit)$/,
);
2026-11-21 14:09:59 +08:00
if (!match) return null;
2026-11-24 14:49:58 +08:00
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]),
};
2026-11-21 14:09:59 +08:00
};
const initialContentEditor = resolveContentRoute();
2026-11-24 14:49:58 +08:00
const [section, setSection] = useState<Section>(
initialContentEditor?.contentType ||
(window.location.pathname.startsWith("/pages/") ? "pages" : "products"),
);
2026-11-21 14:09:59 +08:00
const [items, setItems] = useState<Item[]>([]);
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;
};
2026-11-24 14:49:58 +08:00
const [editingProduct, setEditingProduct] = useState<string | null>(
resolveProductRoute,
);
const [editingContent, setEditingContent] =
useState<ContentEditorState | null>(initialContentEditor);
const [editingPage, setEditingPage] = useState<string | null>(
resolvePageRoute,
);
2026-11-21 14:09:59 +08:00
const [productMenuOpen, setProductMenuOpen] = useState(true);
const [contentMenuOpen, setContentMenuOpen] = useState(true);
2026-11-23 11:09:03 +08:00
const [frontSiteMenuOpen, setFrontSiteMenuOpen] = useState(true);
2026-11-21 14:09:59 +08:00
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("");
2026-11-24 14:49:58 +08:00
const data = section.endsWith("-categories")
? { items: [] }
: section === "settings"
2026-11-21 14:09:59 +08:00
? { items: [] }
2026-11-23 11:09:03 +08:00
: section === "navigation"
2026-11-24 14:49:58 +08:00
? { items: [] }
: section === "agreements"
2026-11-21 14:09:59 +08:00
? { items: [] }
2026-11-24 14:49:58 +08:00
: 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,
});
2026-11-21 14:09:59 +08:00
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();
};
2026-11-24 14:49:58 +08:00
const openContentEditor = (
contentType: EditorialSection,
contentCode?: string,
) => {
2026-11-21 14:09:59 +08:00
const route = contentRoutes[contentType];
2026-11-24 14:49:58 +08:00
const path = contentCode
? `/${route}/${encodeURIComponent(contentCode)}/edit`
: `/${route}/new`;
2026-11-21 14:09:59 +08:00
window.history.pushState({}, "", path);
setEditingContent({ contentType, contentCode: contentCode || "" });
};
const closeContentEditor = () => {
window.history.pushState({}, "", "/");
setEditingContent(null);
};
const openPageEditor = (pageCode?: string) => {
2026-11-24 14:49:58 +08:00
window.history.pushState(
{},
"",
pageCode ? `/pages/${encodeURIComponent(pageCode)}/edit` : "/pages/new",
);
2026-11-21 14:09:59 +08:00
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)
2026-11-24 14:49:58 +08:00
await post("/products/status", {
siteCode: SITE,
productCode: item.productCode,
status,
});
2026-11-21 14:09:59 +08:00
else if (["NEWS", "CASE", "HELP"].includes(section) && item.contentCode)
2026-11-24 14:49:58 +08:00
await post("/contents/status", {
siteCode: SITE,
contentCode: item.contentCode,
contentType: section,
status,
});
2026-11-21 14:09:59 +08:00
await load();
2026-11-24 14:49:58 +08:00
} catch (operationError) {
setError(
operationError instanceof Error
? operationError.message
: "状态更新失败",
);
}
2026-11-21 14:09:59 +08:00
};
const deleteItem = async (item: Item) => {
if (!window.confirm("删除后该内容将不再展示,确认删除吗?")) return;
try {
setError("");
if (section === "products" && item.productCode)
2026-11-24 14:49:58 +08:00
await post("/products/delete", {
siteCode: SITE,
productCode: item.productCode,
});
2026-11-21 14:09:59 +08:00
else if (["NEWS", "CASE", "HELP"].includes(section) && item.contentCode)
2026-11-24 14:49:58 +08:00
await post("/contents/delete", {
siteCode: SITE,
contentCode: item.contentCode,
contentType: section,
});
2026-11-21 14:09:59 +08:00
await load();
2026-11-24 14:49:58 +08:00
} catch (operationError) {
setError(
operationError instanceof Error ? operationError.message : "删除失败",
);
}
2026-11-21 14:09:59 +08:00
};
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 (
<div className="shell">
<aside>
<div className="brand">
<span>S</span>
<div>
Shoplix<small>运营中心</small>
</div>
</div>
<nav>
<div className="nav-group">
2026-11-24 14:49:58 +08:00
<button
className={
["products", "product-categories"].includes(section)
? "group-active"
: ""
}
onClick={() => setProductMenuOpen(!productMenuOpen)}
>
<Boxes size={18} />
产品
<ChevronDown
className={productMenuOpen ? "expanded" : ""}
size={16}
/>
</button>
{productMenuOpen && (
<div className="nav-children">
<button
className={section === "products" ? "active" : ""}
onClick={() => navigateSection("products")}
>
产品管理
</button>
<button
className={section === "product-categories" ? "active" : ""}
onClick={() => navigateSection("product-categories")}
>
分类管理
</button>
</div>
)}
2026-11-21 14:09:59 +08:00
</div>
<div className="nav-group">
2026-11-24 14:49:58 +08:00
<button
className={
[
"NEWS",
"NEWS-categories",
"CASE",
"CASE-categories",
"HELP",
"HELP-categories",
].includes(section)
? "group-active"
: ""
}
onClick={() => setContentMenuOpen(!contentMenuOpen)}
>
<FolderTree size={18} />
内容
<ChevronDown
className={contentMenuOpen ? "expanded" : ""}
size={16}
/>
</button>
{contentMenuOpen && (
<div className="nav-children content-children">
<button
className={
["NEWS", "NEWS-categories"].includes(section)
? "active"
: ""
}
onClick={() => navigateSection("NEWS")}
>
<Newspaper size={15} />
新闻
</button>
<button
className={
["CASE", "CASE-categories"].includes(section)
? "active"
: ""
}
onClick={() => navigateSection("CASE")}
>
<PanelsTopLeft size={15} />
案例
</button>
<button
className={
["HELP", "HELP-categories"].includes(section)
? "active"
: ""
}
onClick={() => navigateSection("HELP")}
>
<HelpCircle size={15} />
帮助
</button>
</div>
)}
2026-11-21 14:09:59 +08:00
</div>
2026-11-23 11:09:03 +08:00
<div className="nav-group">
2026-11-24 14:49:58 +08:00
<button
className={
frontSiteMenus.some((item) => item.key === section)
? "group-active"
: ""
}
onClick={() => setFrontSiteMenuOpen(!frontSiteMenuOpen)}
>
<Globe2 size={18} />
前台站
<ChevronDown
className={frontSiteMenuOpen ? "expanded" : ""}
size={16}
/>
</button>
{frontSiteMenuOpen && (
<div className="nav-children front-site-children">
{frontSiteMenus.map((item) => (
<button
key={item.key}
className={section === item.key ? "active" : ""}
onClick={() => navigateSection(item.key)}
>
<item.icon size={15} />
{item.label}
</button>
))}
</div>
)}
2026-11-23 11:09:03 +08:00
</div>
2026-11-21 14:09:59 +08:00
{menus.map((m) => (
<button
key={m.key}
className={section === m.key ? "active" : ""}
onClick={() => navigateSection(m.key)}
>
<m.icon size={18} />
{m.label}
</button>
))}
</nav>
<div className="aside-bottom">
<button>
<Users size={18} />
CRM 客户
</button>
2026-11-24 14:49:58 +08:00
<button
className={section === "inquiry-settings" ? "active" : ""}
onClick={() => navigateSection("inquiry-settings")}
>
2026-11-21 14:09:59 +08:00
<Settings size={18} />
询盘设置
</button>
2026-11-24 14:49:58 +08:00
<button
className={section === "settings" ? "active" : ""}
onClick={() => navigateSection("settings")}
>
2026-11-22 10:14:44 +08:00
<Settings size={18} />
系统设置
</button>
2026-11-21 14:09:59 +08:00
</div>
</aside>
<main>
2026-11-24 14:49:58 +08:00
{section === "settings" ? (
<SettingsPage
account={account}
onAccount={onAccount}
onLogout={onLogout}
/>
) : section === "seo-settings" ? (
<SeoSettingsPage />
) : section === "agreements" ? (
<AgreementSettingsPage />
) : section === "navigation" ? (
<NavigationManager />
) : section === "decoration" ? (
<DecorationEditor onBack={() => navigateSection("pages")} />
) : section === "custom-code" ? (
<CustomCodeManager />
) : section === "media" ? (
<MediaCenter />
) : editingProduct !== null ? (
<ProductEditor
productCode={editingProduct || undefined}
onBack={closeProductEditor}
/>
) : editingContent !== null ? (
<ContentEditor
contentType={editingContent.contentType}
contentCode={editingContent.contentCode || undefined}
onBack={closeContentEditor}
/>
) : editingPage !== null ? (
<PageEditor
pageCode={editingPage || undefined}
onBack={closePageEditor}
/>
) : (
<>
{section.endsWith("-categories") ? (
<CategoryManager
contentType={
section === "NEWS-categories"
? "NEWS"
: section === "CASE-categories"
? "CASE"
: section === "HELP-categories"
? "HELP"
: undefined
2026-11-21 14:09:59 +08:00
}
2026-11-24 14:49:58 +08:00
/>
) : (
<>
<header>
<div>
<h1>{title}管理</h1>
<p>Local Development Site · 英文站内容</p>
</div>
{!["inquiries", "inquiry-settings"].includes(section) && (
<div className="header-actions">
{["NEWS", "CASE", "HELP"].includes(section) && (
<button
className="secondary"
onClick={() =>
setSection(`${section}-categories` as Section)
}
>
<FolderTree size={17} />
管理分类
</button>
)}
<button
className="primary"
onClick={() =>
section === "products"
? openProductEditor()
: ["NEWS", "CASE", "HELP"].includes(section)
? openContentEditor(section as EditorialSection)
: section === "pages"
? openPageEditor()
: setOpen(true)
}
>
<Plus size={17} />
新建{title}
</button>
</div>
)}
</header>
{section === "inquiry-settings" ? (
<InquirySettingsPanel />
) : (
<>
<section className="toolbar">
<div className="search">
<Search size={17} />
<input
value={query}
onChange={(e) => setQuery(e.target.value)}
placeholder={`搜索${title}名称或路径`}
/>
</div>
</section>
{error && (
<div className="notice">
{error}。请先启动 Admin 后端,并初始化本地站点数据。
</div>
)}
<section
className={`table ${section === "inquiries" ? "inquiry-table" : ""}`}
>
<div className="thead">
{section === "inquiries" ? (
<>
<span>公司名称</span>
<span>电子邮箱</span>
<span>国家 / IP地址</span>
<span>询盘时间</span>
<span>询盘来源</span>
<span>状态</span>
<span>操作</span>
</>
) : (
<>
<span>名称</span>
<span>英文路径</span>
<span>状态</span>
<span>发布时间</span>
<span>操作</span>
</>
)}
</div>
{filtered.length === 0 && !error ? (
<div className="empty">
<strong>暂无{title}数据</strong>
<p>点击右上角新建按钮录入第一条内容。</p>
</div>
) : (
filtered.map((item, index) => (
<div
className="row"
key={
item.productCode ||
item.contentCode ||
item.pageCode ||
item.inquiryCode ||
index
}
>
{section === "inquiries" ? (
<>
<span>
<strong>
{item.companyName ||
item.customerName ||
"--"}
</strong>
<small>{item.customerName || "--"}</small>
</span>
<span>{item.customerEmail || "--"}</span>
<span>
{item.countryName ||
item.countryCode ||
"未知"}
({item.clientIp || "--"})
</span>
<span>{formatTime(item.createTime)}</span>
<span>
{item.productName ||
item.inquiryTitle ||
item.sourceType ||
"网站询盘"}
</span>
<span
className={`status ${item.status.toLowerCase()}`}
>
{item.readStatus === 0
? "未读"
: item.statusLabel || statusLabel(item.status)}
</span>
<div className="row-actions">
<button className="link">查看</button>
</div>
</>
) : (
<>
<span>
<strong>
{item.productName ||
item.title ||
item.pageName}
</strong>
<small>
{item.model ||
item.productCode ||
item.contentCode ||
item.pageCode}
</small>
</span>
<code>/{item.slug}</code>
<span
className={`status ${item.status.toLowerCase()}`}
>
{statusLabel(item.status)}
</span>
<span>{formatTime(item.publishTime)}</span>
<div className="row-actions">
{[
"products",
"NEWS",
"CASE",
"HELP",
].includes(section) &&
item.slug && (
<a
className="link"
href={`${SITE_WEB}/${section === "products" ? "products" : section === "CASE" ? "cases" : section === "NEWS" ? "news" : "help"}/${encodeURIComponent(item.slug)}`}
target="_blank"
rel="noreferrer"
>
查看前台
</a>
)}
<button
className="link"
onClick={() =>
section === "products" && item.productCode
? openProductEditor(item.productCode)
: ["NEWS", "CASE", "HELP"].includes(
section,
) && item.contentCode
? openContentEditor(
section as EditorialSection,
item.contentCode,
)
: section === "pages" && item.pageCode
? openPageEditor(item.pageCode)
: undefined
}
>
编辑
</button>
{[
"products",
"NEWS",
"CASE",
"HELP",
].includes(section) && (
<>
<button
className="link"
onClick={() =>
void changeItemStatus(item)
}
>
{item.status === "PUBLISHED"
? "下架"
: "上架"}
</button>
<button
className="link danger"
onClick={() => void deleteItem(item)}
>
删除
</button>
</>
)}
</div>
</>
)}
</div>
))
)}
</section>
</>
)}
</>
)}
</>
)}
2026-11-21 14:09:59 +08:00
</main>
{open && (
<div className="mask">
<section className="dialog">
<div className="dialog-head">
<div>
<h2>新建{title}</h2>
<p>前台内容统一使用英文录入</p>
</div>
<button
className="icon"
onClick={() => setOpen(false)}
aria-label="关闭"
>
<X size={19} />
</button>
</div>
<div className="form-grid">
<label>
<span>
{section === "products"
? "产品名称"
: section === "pages"
? "页面名称"
: "标题"}
</span>
<input
value={form.name}
onChange={(e) => setForm({ ...form, name: e.target.value })}
placeholder="English name"
/>
</label>
<label>
<span>英文路径</span>
<input
value={form.slug}
onChange={(e) =>
setForm({
...form,
slug: e.target.value
.toLowerCase()
.replace(/[^a-z0-9-]/g, "-"),
})
}
placeholder="industrial-pumps"
/>
</label>
{section === "products" && (
<label>
<span>产品型号</span>
<input
value={form.model}
onChange={(e) =>
setForm({ ...form, model: e.target.value })
}
placeholder="Model / Series"
/>
</label>
)}
<label>
<span>发布状态</span>
<select
value={form.status}
onChange={(e) => setForm({ ...form, status: e.target.value })}
>
<option value="DRAFT">草稿</option>
<option value="PUBLISHED">立即发布</option>
<option value="OFFLINE">下线</option>
</select>
</label>
<label className="wide">
<span>英文摘要</span>
<textarea
rows={3}
value={form.summary}
onChange={(e) =>
setForm({ ...form, summary: e.target.value })
}
/>
</label>
<label className="wide">
<span>英文正文</span>
<textarea
rows={9}
value={form.body}
onChange={(e) => setForm({ ...form, body: e.target.value })}
/>
</label>
</div>
<div className="dialog-actions">
<button onClick={() => setOpen(false)}>取消</button>
<button
className="primary"
disabled={saving || !form.name || !form.slug || !form.body}
onClick={save}
>
{saving ? "保存中..." : "保存"}
</button>
</div>
</section>
</div>
)}
</div>
);
}
2026-11-22 10:14:44 +08:00
function RootApp() {
const [account, setAccount] = useState<Account | null>(null);
const [checking, setChecking] = useState(true);
useEffect(() => {
const unauthorized = () => setAccount(null);
window.addEventListener("shoplix:unauthorized", unauthorized);
apiPost<Account>("/auth/me")
.then(setAccount)
.catch(() => setAccount(null))
.finally(() => setChecking(false));
2026-11-24 14:49:58 +08:00
return () =>
window.removeEventListener("shoplix:unauthorized", unauthorized);
2026-11-22 10:14:44 +08:00
}, []);
2026-11-24 14:49:58 +08:00
if (checking)
return (
<div className="auth-loading">
<div className="auth-spinner" />
<span>正在验证登录状态</span>
</div>
);
if (!account) return <LoginPage onLogin={setAccount} />;
return (
<App
account={account}
onAccount={setAccount}
onLogout={() => setAccount(null)}
/>
);
2026-11-22 10:14:44 +08:00
}
2026-11-21 14:09:59 +08:00
createRoot(document.getElementById("root")!).render(
<React.StrictMode>
2026-11-22 10:14:44 +08:00
<RootApp />
2026-11-21 14:09:59 +08:00
</React.StrictMode>,
);