import React, { useState, useEffect } from 'react'; import Sidebar from './components/Sidebar'; import Dashboard from './components/Dashboard'; import AccountList from './components/AccountList'; import OrderList from './components/OrderList'; import CardList from './components/CardList'; import ItemList from './components/ItemList'; import Settings from './components/Settings'; import Rules from './components/Rules'; import Notifications from './components/Notifications'; import { YdisksBrandIcon } from './components/YdisksLogo'; import { login, logout, verifySession } from './services/api'; import { ShieldCheck, ArrowRight, Loader2, User, Lock } from 'lucide-react'; interface DeliveryRuleTarget { cookieId: string; itemId: string; requestId: number; } // 路由:URL path ↔ tab id。所有 SPA 路由统一挂 /app/ 前缀,避免和后端 API // 路径(/orders、/cards、/items 等)冲突——后者在 chi 里先注册,刷新会直接 // 返回 JSON 而不是 SPA 页面。 const ROUTES: Record = { '/app/dashboard': 'dashboard', '/app/accounts': 'accounts', '/app/orders': 'orders', '/app/cards': 'cards', '/app/items': 'items', '/app/rules': 'rules', '/app/notifications': 'notifications', '/app/settings': 'settings', }; const TAB_TO_PATH: Record = Object.fromEntries( Object.entries(ROUTES).map(([path, tab]) => [tab, path]) ); const tabFromPath = (): string => ROUTES[window.location.pathname] || 'dashboard'; // 检测 Windows 以显示对应的 CLI 命令(路径分隔符、引号、.exe 后缀)。 const isWindows = /Win/i.test(navigator.platform || navigator.userAgent); const App: React.FC = () => { const [isLoggedIn, setIsLoggedIn] = useState(false); const [isAdmin, setIsAdmin] = useState(false); const [activeTab, setActiveTab] = useState(tabFromPath); const [checkingAuth, setCheckingAuth] = useState(true); const [needsInit, setNeedsInit] = useState(false); const [username, setUsername] = useState(''); const [password, setPassword] = useState(''); const [loginLoading, setLoginLoading] = useState(false); const [loginError, setLoginError] = useState(''); const [deliveryRuleTarget, setDeliveryRuleTarget] = useState(); // 切换 tab 并同步 URL。若 tab 没有对应 path(不应发生)则只切 tab。 const navigate = (tab: string) => { const nextTab = tab === 'settings' && !isAdmin ? 'dashboard' : tab; const path = TAB_TO_PATH[nextTab]; if (path && path !== window.location.pathname) { window.history.pushState({}, '', path); } setActiveTab(nextTab); }; // 浏览器后退/前进同步 tab。 useEffect(() => { const onPopState = () => setActiveTab(tabFromPath()); window.addEventListener('popstate', onPopState); return () => window.removeEventListener('popstate', onPopState); }, []); // Check auth on mount useEffect(() => { verifySession() .then((res) => { if (res?.initialized === false) { setNeedsInit(true); setIsLoggedIn(false); setIsAdmin(false); return; } setNeedsInit(false); if (res?.authenticated) { setIsLoggedIn(true); setIsAdmin(res.is_admin === true); } else { setIsAdmin(false); } }) .catch(() => { setIsLoggedIn(false); setIsAdmin(false); }) .finally(() => setCheckingAuth(false)); const handleAuthLogoutEvent = () => { setIsLoggedIn(false); setIsAdmin(false); }; window.addEventListener('auth:logout', handleAuthLogoutEvent); return () => window.removeEventListener('auth:logout', handleAuthLogoutEvent); }, []); useEffect(() => { if (!checkingAuth && isLoggedIn && !isAdmin && activeTab === 'settings') { window.history.replaceState({}, '', TAB_TO_PATH.dashboard); setActiveTab('dashboard'); } }, [checkingAuth, isLoggedIn, isAdmin, activeTab]); const handleLogin = async (e: React.FormEvent) => { e.preventDefault(); setLoginLoading(true); setLoginError(''); try { const res = await login({ username, password }); if (res.success) { setIsLoggedIn(true); setIsAdmin(res.is_admin === true); } else { setLoginError(res.message || '登录失败'); } } catch (err) { const msg = err instanceof Error ? err.message : String(err); setLoginError(msg || '登录失败'); } finally { setLoginLoading(false); } }; const handleLogout = async () => { try { await logout(); } catch (err) { console.error('退出登录失败', err); } finally { setIsLoggedIn(false); setIsAdmin(false); } }; if (checkingAuth) { return (
); } // Init Screen (system not initialized) if (needsInit) { return (

系统尚未初始化

为避免默认口令风险,管理员必须通过服务器本机 CLI 初始化。

请在服务器上执行:
{isWindows
                ? '.\\xianyu-server.exe -init-admin -db data\\xianyu_data.db -admin-password "请设置密码"'
                : './xianyu-server -init-admin -db data/xianyu_data.db -admin-password \'请设置密码\''}
完成后刷新页面即可进入登录。
Secure Bootstrap
); } // Login Screen Component if (!isLoggedIn) { return (
{/* Animated Background Blobs */}
{/* Header with Logo */}

欢迎回来

Ydisks闲鱼助手 · 自动发货与管家系统

setUsername(e.target.value)} className="w-full ios-input pl-14 pr-6 py-4.5 rounded-2xl text-base h-14" />
setPassword(e.target.value)} className="w-full ios-input pl-14 pr-6 py-4.5 rounded-2xl text-base h-14" />
{loginError && (
{loginError}
)}
Ydisks闲鱼助手 v1.0
); } // Main App Layout const renderContent = () => { switch (activeTab) { case 'dashboard': return ; case 'accounts': return ; case 'orders': return ; case 'cards': return ; case 'items': return { setDeliveryRuleTarget({ cookieId: item.cookie_id, itemId: item.item_id, requestId: Date.now() }); navigate('rules'); }} />; case 'rules': return setDeliveryRuleTarget(undefined)} />; case 'notifications': return ; case 'settings': return isAdmin ? : ; default: return ; } }; return (
{/* Subtle background decoration */}
{renderContent()}
); }; export default App;