Adds /api/health, ntfy notify on config save, /admin/analytics charts, and deploy smoke for miniprogram config/API. Co-authored-by: Cursor <cursoragent@cursor.com>
150 lines
4.8 KiB
TypeScript
150 lines
4.8 KiB
TypeScript
"use client";
|
|
|
|
import Link from "next/link";
|
|
import { usePathname, useRouter } from "next/navigation";
|
|
import { useCallback, useEffect, useState, type ReactNode } from "react";
|
|
import { ThemeToggle } from "~/components/theme-toggle";
|
|
import styles from "./admin.module.css";
|
|
|
|
const NAV = [
|
|
{ href: "/admin", label: "总览", emoji: "📡" },
|
|
{ href: "/admin/config", label: "远程配置", emoji: "⚙️" },
|
|
{ href: "/admin/users", label: "用户", emoji: "👥" },
|
|
{ href: "/admin/orders", label: "订单/支付", emoji: "💳" },
|
|
{ href: "/admin/verticals", label: "垂直业务", emoji: "🧩" },
|
|
{ href: "/admin/miniprogram", label: "小程序", emoji: "📱" },
|
|
{ href: "/admin/analytics", label: "统计", emoji: "📈" },
|
|
{ href: "/admin/ops", label: "运维", emoji: "🛠️" },
|
|
] as const;
|
|
|
|
export function AdminShell({ children }: { children: ReactNode }) {
|
|
const pathname = usePathname();
|
|
const router = useRouter();
|
|
const [ready, setReady] = useState(false);
|
|
const [authed, setAuthed] = useState(false);
|
|
const [configured, setConfigured] = useState(true);
|
|
const [password, setPassword] = useState("");
|
|
const [error, setError] = useState("");
|
|
const [busy, setBusy] = useState(false);
|
|
|
|
const check = useCallback(async () => {
|
|
const res = await fetch("/api/admin/session", { cache: "no-store" });
|
|
const data = await res.json().catch(() => ({}));
|
|
setConfigured(Boolean(data.configured));
|
|
setAuthed(Boolean(data.ok));
|
|
setReady(true);
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
void check();
|
|
}, [check]);
|
|
|
|
async function onLogin(e: React.FormEvent) {
|
|
e.preventDefault();
|
|
setBusy(true);
|
|
setError("");
|
|
try {
|
|
const res = await fetch("/api/admin/login", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ password }),
|
|
});
|
|
const data = await res.json().catch(() => ({}));
|
|
if (!res.ok || !data.ok) {
|
|
setError(data.error || "登录失败");
|
|
return;
|
|
}
|
|
setAuthed(true);
|
|
setPassword("");
|
|
router.refresh();
|
|
} finally {
|
|
setBusy(false);
|
|
}
|
|
}
|
|
|
|
async function onLogout() {
|
|
await fetch("/api/admin/logout", { method: "POST" });
|
|
setAuthed(false);
|
|
}
|
|
|
|
if (!ready) {
|
|
return (
|
|
<div className={styles.shell}>
|
|
<p className={styles.muted}>加载会话…</p>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
if (!authed) {
|
|
return (
|
|
<div className={styles.shell}>
|
|
<div className={styles.loginCard}>
|
|
<p className={styles.eyebrow}>🔐 Nomadro Admin</p>
|
|
<h1 className={styles.loginTitle}>统一管理后台</h1>
|
|
<p className={styles.muted}>
|
|
{configured
|
|
? "输入管理员密码进入编排面板"
|
|
: "请先在 .env.local 配置 NOMADRO_ADMIN_PASSWORD"}
|
|
</p>
|
|
<form className={styles.loginForm} onSubmit={onLogin}>
|
|
<input
|
|
type="password"
|
|
className={styles.input}
|
|
value={password}
|
|
onChange={(e) => setPassword(e.target.value)}
|
|
placeholder="管理员密码"
|
|
autoComplete="current-password"
|
|
disabled={!configured || busy}
|
|
/>
|
|
{error ? <p className={styles.error}>{error}</p> : null}
|
|
<button type="submit" className={styles.primaryBtn} disabled={!configured || busy}>
|
|
{busy ? "登录中…" : "进入后台"}
|
|
</button>
|
|
</form>
|
|
<Link href="/" className={styles.backLink}>
|
|
← 返回门户
|
|
</Link>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<div className={styles.shell}>
|
|
<div className={styles.frame}>
|
|
<aside className={styles.sidebar}>
|
|
<Link href="/admin" className={styles.brand}>
|
|
nomadro
|
|
</Link>
|
|
<p className={styles.sideHint}>统一编排 · 深色科技风</p>
|
|
<nav className={styles.sideNav}>
|
|
{NAV.map((item) => {
|
|
const active =
|
|
item.href === "/admin"
|
|
? pathname === "/admin"
|
|
: pathname.startsWith(item.href);
|
|
return (
|
|
<Link
|
|
key={item.href}
|
|
href={item.href}
|
|
className={`${styles.sideLink} ${active ? styles.sideLinkActive : ""}`}
|
|
>
|
|
<span aria-hidden>{item.emoji}</span>
|
|
{item.label}
|
|
</Link>
|
|
);
|
|
})}
|
|
</nav>
|
|
<div className={styles.sideFoot}>
|
|
<ThemeToggle />
|
|
<button type="button" className={styles.ghostBtn} onClick={onLogout}>
|
|
退出
|
|
</button>
|
|
</div>
|
|
</aside>
|
|
<div className={styles.main}>{children}</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|