账户登录
This commit is contained in:
1 parent
a0c723cdcc
commit
3b555ae815
37 files changed
+728
-26
No files matched your search
+36
-14
@@ -27,11 +27,13 @@ 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 { apiPost } from "./api";
|
||||
|
||||
const API = "http://localhost:8081/api/admin/v1";
|
||||
const SITE_WEB = "http://localhost:5174";
|
||||
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" | "decoration" | "custom-code" | "media" | "inquiries" | "inquiry-settings";
|
||||
type Section = "products" | "product-categories" | "NEWS" | "NEWS-categories" | "CASE" | "CASE-categories" | "HELP" | "HELP-categories" | "pages" | "decoration" | "custom-code" | "media" | "inquiries" | "inquiry-settings" | "settings";
|
||||
type EditorialSection = "NEWS" | "CASE" | "HELP";
|
||||
type ContentEditorState = { contentType: EditorialSection; contentCode: string };
|
||||
type Item = {
|
||||
@@ -62,14 +64,7 @@ type Item = {
|
||||
};
|
||||
|
||||
async function post(path: string, body: unknown) {
|
||||
const response = await fetch(API + path, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
if (!response.ok)
|
||||
throw new Error((await response.json()).message || "请求失败");
|
||||
return response.json();
|
||||
return apiPost(path, body);
|
||||
}
|
||||
|
||||
const menus = [
|
||||
@@ -83,10 +78,11 @@ const sectionLabels: Record<Section, string> = {
|
||||
products: "产品", "product-categories": "产品分类", NEWS: "新闻", "NEWS-categories": "新闻分类",
|
||||
CASE: "案例", "CASE-categories": "案例分类", HELP: "帮助", "HELP-categories": "帮助分类",
|
||||
pages: "页面", decoration: "店铺装修", "custom-code": "自定义代码", media: "素材中心", inquiries: "询盘", "inquiry-settings": "询盘设置",
|
||||
settings: "系统设置",
|
||||
};
|
||||
const statusLabels: Record<string, string> = { DRAFT: "草稿", PUBLISHED: "已上架", OFFLINE: "已下架" };
|
||||
|
||||
function App() {
|
||||
function App({ account, onAccount, onLogout }: { account: Account; onAccount: (value: Account) => void; onLogout: () => void }) {
|
||||
const contentRoutes: Record<EditorialSection, string> = { NEWS: "news", CASE: "cases", HELP: "help" };
|
||||
const resolveContentRoute = (): ContentEditorState | null => {
|
||||
const match = window.location.pathname.match(/^\/(news|cases|help)\/(new|([^/]+)\/edit)$/);
|
||||
@@ -136,6 +132,8 @@ function App() {
|
||||
setError("");
|
||||
const data =
|
||||
section.endsWith("-categories")
|
||||
? { items: [] }
|
||||
: section === "settings"
|
||||
? { items: [] }
|
||||
: section === "products"
|
||||
? await post("/products/list", { siteCode: SITE })
|
||||
@@ -322,10 +320,14 @@ function App() {
|
||||
<Settings size={18} />
|
||||
询盘设置
|
||||
</button>
|
||||
<button className={section === "settings" ? "active" : ""} onClick={() => navigateSection("settings")}>
|
||||
<Settings size={18} />
|
||||
系统设置
|
||||
</button>
|
||||
</div>
|
||||
</aside>
|
||||
<main>
|
||||
{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 === "settings" ? <SettingsPage account={account} onAccount={onAccount} onLogout={onLogout}/> : 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}/> : <>
|
||||
<header>
|
||||
<div>
|
||||
@@ -481,8 +483,28 @@ function App() {
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
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));
|
||||
return () => window.removeEventListener("shoplix:unauthorized", unauthorized);
|
||||
}, []);
|
||||
|
||||
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)}/>;
|
||||
}
|
||||
|
||||
createRoot(document.getElementById("root")!).render(
|
||||
<React.StrictMode>
|
||||
<App />
|
||||
<RootApp />
|
||||
</React.StrictMode>,
|
||||
);
|
||||
Reference in new issue
Block a user