Add user authentication with JWT login and registration
This commit is contained in:
+162
-53
@@ -1,61 +1,170 @@
|
||||
import React from 'react';
|
||||
import { BrowserRouter, Routes, Route, NavLink } from 'react-router-dom';
|
||||
import React from "react";
|
||||
import {
|
||||
LayoutDashboard, Upload, ListFilter, ClipboardCheck, Settings
|
||||
} from 'lucide-react';
|
||||
import Dashboard from './pages/Dashboard';
|
||||
import Import from './pages/Import';
|
||||
import Transactions from './pages/Transactions';
|
||||
import Review from './pages/Review';
|
||||
import SettingsPage from './pages/Settings';
|
||||
import './App.css';
|
||||
BrowserRouter,
|
||||
Routes,
|
||||
Route,
|
||||
NavLink,
|
||||
Navigate,
|
||||
Outlet,
|
||||
useNavigate,
|
||||
} from "react-router-dom";
|
||||
import {
|
||||
LayoutDashboard,
|
||||
Upload,
|
||||
ListFilter,
|
||||
ClipboardCheck,
|
||||
Settings,
|
||||
LogOut,
|
||||
} from "lucide-react";
|
||||
import { AuthProvider, useAuth } from "./context/AuthContext";
|
||||
import ProtectedRoute from "./components/ProtectedRoute";
|
||||
import Dashboard from "./pages/Dashboard";
|
||||
import Import from "./pages/Import";
|
||||
import Transactions from "./pages/Transactions";
|
||||
import Review from "./pages/Review";
|
||||
import SettingsPage from "./pages/Settings";
|
||||
import Login from "./pages/Login";
|
||||
import "./App.css";
|
||||
|
||||
function MainLayout() {
|
||||
const { user, logout } = useAuth();
|
||||
const navigate = useNavigate();
|
||||
|
||||
function handleLogout() {
|
||||
logout();
|
||||
navigate("/login", { replace: true });
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="app">
|
||||
<nav className="sidebar">
|
||||
<div className="sidebar-header">
|
||||
<h1 className="logo">💰 Reco</h1>
|
||||
<span className="logo-sub">银行流水对账分析</span>
|
||||
</div>
|
||||
<div className="nav-links">
|
||||
<NavLink
|
||||
to="/"
|
||||
end
|
||||
className={({ isActive }) => `nav-link ${isActive ? "active" : ""}`}
|
||||
>
|
||||
<LayoutDashboard size={20} />
|
||||
<span>仪表盘</span>
|
||||
</NavLink>
|
||||
<NavLink
|
||||
to="/import"
|
||||
className={({ isActive }) => `nav-link ${isActive ? "active" : ""}`}
|
||||
>
|
||||
<Upload size={20} />
|
||||
<span>导入账单</span>
|
||||
</NavLink>
|
||||
<NavLink
|
||||
to="/transactions"
|
||||
className={({ isActive }) => `nav-link ${isActive ? "active" : ""}`}
|
||||
>
|
||||
<ListFilter size={20} />
|
||||
<span>交易明细</span>
|
||||
</NavLink>
|
||||
<NavLink
|
||||
to="/review"
|
||||
className={({ isActive }) => `nav-link ${isActive ? "active" : ""}`}
|
||||
>
|
||||
<ClipboardCheck size={20} />
|
||||
<span>大额确认</span>
|
||||
<span className="badge" id="review-badge">
|
||||
0
|
||||
</span>
|
||||
</NavLink>
|
||||
<NavLink
|
||||
to="/settings"
|
||||
className={({ isActive }) => `nav-link ${isActive ? "active" : ""}`}
|
||||
>
|
||||
<Settings size={20} />
|
||||
<span>系统设置</span>
|
||||
</NavLink>
|
||||
</div>
|
||||
<div className="sidebar-footer">
|
||||
<div
|
||||
style={{
|
||||
fontSize: 12,
|
||||
color: "rgba(255,255,255,0.5)",
|
||||
marginBottom: 8,
|
||||
}}
|
||||
>
|
||||
👤 {user?.nickname || user?.username}
|
||||
</div>
|
||||
<div
|
||||
style={{
|
||||
display: "flex",
|
||||
alignItems: "center",
|
||||
justifyContent: "space-between",
|
||||
}}
|
||||
>
|
||||
<span className="version">v1.0.0</span>
|
||||
<button
|
||||
onClick={handleLogout}
|
||||
style={{
|
||||
background: "none",
|
||||
border: "none",
|
||||
color: "rgba(255,255,255,0.5)",
|
||||
cursor: "pointer",
|
||||
display: "flex",
|
||||
alignItems: "center",
|
||||
gap: 4,
|
||||
fontSize: 11,
|
||||
padding: "2px 6px",
|
||||
borderRadius: 4,
|
||||
}}
|
||||
onMouseEnter={(e) => {
|
||||
e.target.style.color = "white";
|
||||
e.target.style.background = "rgba(255,255,255,0.1)";
|
||||
}}
|
||||
onMouseLeave={(e) => {
|
||||
e.target.style.color = "rgba(255,255,255,0.5)";
|
||||
e.target.style.background = "none";
|
||||
}}
|
||||
>
|
||||
<LogOut size={12} /> 登出
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
<main className="main-content">
|
||||
<Outlet />
|
||||
</main>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function AppRoutes() {
|
||||
return (
|
||||
<Routes>
|
||||
{/* 公开路由 */}
|
||||
<Route path="/login" element={<Login />} />
|
||||
|
||||
{/* 受保护路由 */}
|
||||
<Route element={<ProtectedRoute />}>
|
||||
<Route element={<MainLayout />}>
|
||||
<Route path="/" element={<Dashboard />} />
|
||||
<Route path="/import" element={<Import />} />
|
||||
<Route path="/transactions" element={<Transactions />} />
|
||||
<Route path="/review" element={<Review />} />
|
||||
<Route path="/settings" element={<SettingsPage />} />
|
||||
</Route>
|
||||
</Route>
|
||||
|
||||
{/* 未匹配路由重定向到首页 */}
|
||||
<Route path="*" element={<Navigate to="/" replace />} />
|
||||
</Routes>
|
||||
);
|
||||
}
|
||||
|
||||
function App() {
|
||||
return (
|
||||
<BrowserRouter>
|
||||
<div className="app">
|
||||
<nav className="sidebar">
|
||||
<div className="sidebar-header">
|
||||
<h1 className="logo">💰 Reco</h1>
|
||||
<span className="logo-sub">银行流水对账分析</span>
|
||||
</div>
|
||||
<div className="nav-links">
|
||||
<NavLink to="/" end className={({ isActive }) => `nav-link ${isActive ? 'active' : ''}`}>
|
||||
<LayoutDashboard size={20} />
|
||||
<span>仪表盘</span>
|
||||
</NavLink>
|
||||
<NavLink to="/import" className={({ isActive }) => `nav-link ${isActive ? 'active' : ''}`}>
|
||||
<Upload size={20} />
|
||||
<span>导入账单</span>
|
||||
</NavLink>
|
||||
<NavLink to="/transactions" className={({ isActive }) => `nav-link ${isActive ? 'active' : ''}`}>
|
||||
<ListFilter size={20} />
|
||||
<span>交易明细</span>
|
||||
</NavLink>
|
||||
<NavLink to="/review" className={({ isActive }) => `nav-link ${isActive ? 'active' : ''}`}>
|
||||
<ClipboardCheck size={20} />
|
||||
<span>大额确认</span>
|
||||
<span className="badge" id="review-badge">0</span>
|
||||
</NavLink>
|
||||
<NavLink to="/settings" className={({ isActive }) => `nav-link ${isActive ? 'active' : ''}`}>
|
||||
<Settings size={20} />
|
||||
<span>系统设置</span>
|
||||
</NavLink>
|
||||
</div>
|
||||
<div className="sidebar-footer">
|
||||
<span className="version">v1.0.0</span>
|
||||
</div>
|
||||
</nav>
|
||||
<main className="main-content">
|
||||
<Routes>
|
||||
<Route path="/" element={<Dashboard />} />
|
||||
<Route path="/import" element={<Import />} />
|
||||
<Route path="/transactions" element={<Transactions />} />
|
||||
<Route path="/review" element={<Review />} />
|
||||
<Route path="/settings" element={<SettingsPage />} />
|
||||
</Routes>
|
||||
</main>
|
||||
</div>
|
||||
<AuthProvider>
|
||||
<AppRoutes />
|
||||
</AuthProvider>
|
||||
</BrowserRouter>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
import React from 'react';
|
||||
import { Navigate, Outlet } from 'react-router-dom';
|
||||
import { useAuth } from '../context/AuthContext';
|
||||
|
||||
export default function ProtectedRoute() {
|
||||
const { isAuthenticated } = useAuth();
|
||||
|
||||
if (!isAuthenticated) {
|
||||
return <Navigate to="/login" replace />;
|
||||
}
|
||||
|
||||
return <Outlet />;
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
import React, { createContext, useContext, useState, useEffect, useCallback } from 'react';
|
||||
import axios from 'axios';
|
||||
|
||||
const AuthContext = createContext(null);
|
||||
|
||||
const api = axios.create({ baseURL: '/api', timeout: 15000 });
|
||||
|
||||
const TOKEN_KEY = 'reco_token';
|
||||
const USER_KEY = 'reco_user';
|
||||
|
||||
export function AuthProvider({ children }) {
|
||||
const [user, setUser] = useState(() => {
|
||||
try {
|
||||
const stored = localStorage.getItem(USER_KEY);
|
||||
return stored ? JSON.parse(stored) : null;
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
const [token, setTokenRaw] = useState(() => localStorage.getItem(TOKEN_KEY));
|
||||
|
||||
const setToken = useCallback((t) => {
|
||||
if (t) {
|
||||
localStorage.setItem(TOKEN_KEY, t);
|
||||
} else {
|
||||
localStorage.removeItem(TOKEN_KEY);
|
||||
localStorage.removeItem(USER_KEY);
|
||||
}
|
||||
setTokenRaw(t);
|
||||
}, []);
|
||||
|
||||
// 同步 user 到 localStorage
|
||||
useEffect(() => {
|
||||
if (user) {
|
||||
localStorage.setItem(USER_KEY, JSON.stringify(user));
|
||||
} else {
|
||||
localStorage.removeItem(USER_KEY);
|
||||
}
|
||||
}, [user]);
|
||||
|
||||
const isAuthenticated = !!token;
|
||||
|
||||
async function login(username, password) {
|
||||
const res = await api.post('/auth/login', { username, password });
|
||||
setToken(res.data.token);
|
||||
setUser(res.data.user);
|
||||
return res.data;
|
||||
}
|
||||
|
||||
async function register(username, password, nickname) {
|
||||
const res = await api.post('/auth/register', { username, password, nickname });
|
||||
setToken(res.data.token);
|
||||
setUser(res.data.user);
|
||||
return res.data;
|
||||
}
|
||||
|
||||
function logout() {
|
||||
setToken(null);
|
||||
setUser(null);
|
||||
}
|
||||
|
||||
return (
|
||||
<AuthContext.Provider value={{ user, token, isAuthenticated, login, register, logout }}>
|
||||
{children}
|
||||
</AuthContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
export function useAuth() {
|
||||
const ctx = useContext(AuthContext);
|
||||
if (!ctx) throw new Error('useAuth 必须在 AuthProvider 内使用');
|
||||
return ctx;
|
||||
}
|
||||
|
||||
export default AuthContext;
|
||||
@@ -6,6 +6,28 @@ const api = axios.create({
|
||||
timeout: 60000,
|
||||
});
|
||||
|
||||
// 请求拦截器:自动携带 Token
|
||||
api.interceptors.request.use((config) => {
|
||||
const token = localStorage.getItem("reco_token");
|
||||
if (token) {
|
||||
config.headers.Authorization = `Bearer ${token}`;
|
||||
}
|
||||
return config;
|
||||
});
|
||||
|
||||
// 响应拦截器:401 自动跳转登录
|
||||
api.interceptors.response.use(
|
||||
(response) => response,
|
||||
(error) => {
|
||||
if (error.response?.status === 401) {
|
||||
localStorage.removeItem("reco_token");
|
||||
localStorage.removeItem("reco_user");
|
||||
window.location.href = "/login";
|
||||
}
|
||||
return Promise.reject(error);
|
||||
},
|
||||
);
|
||||
|
||||
// Global toast handler
|
||||
let toastHandler = null;
|
||||
export function setToastHandler(handler) {
|
||||
@@ -60,6 +82,16 @@ export async function deleteSource(id) {
|
||||
return res.data;
|
||||
}
|
||||
|
||||
export async function confirmUpload(tempId) {
|
||||
const res = await api.post("/upload/confirm", { tempId });
|
||||
return res.data;
|
||||
}
|
||||
|
||||
export async function cancelUpload(tempId) {
|
||||
const res = await api.post("/upload/cancel", { tempId });
|
||||
return res.data;
|
||||
}
|
||||
|
||||
// Deals
|
||||
export async function getDeals(params = {}) {
|
||||
const res = await api.get("/deals", { params });
|
||||
@@ -76,6 +108,16 @@ export async function batchConfirmDeals(ids) {
|
||||
return res.data;
|
||||
}
|
||||
|
||||
export async function clearDeals() {
|
||||
const res = await api.post("/deals/clear");
|
||||
return res.data;
|
||||
}
|
||||
|
||||
export async function getNeedsClassify() {
|
||||
const res = await api.get("/transactions/needs-classify");
|
||||
return res.data;
|
||||
}
|
||||
|
||||
// Transactions
|
||||
export async function getTransactions(params = {}) {
|
||||
const res = await api.get("/transactions", { params });
|
||||
|
||||
@@ -99,6 +99,10 @@ export default function Dashboard() {
|
||||
useEffect(() => {
|
||||
if (!raw) return;
|
||||
const cats = [...checked];
|
||||
if (cats.length === 0) {
|
||||
setMonthly([]);
|
||||
return;
|
||||
}
|
||||
getMonthly(12, cats).then(setMonthly).catch(console.error);
|
||||
}, [checked, raw]);
|
||||
|
||||
|
||||
+17
-25
@@ -9,7 +9,13 @@ import {
|
||||
X,
|
||||
Info,
|
||||
} from "lucide-react";
|
||||
import { getSources, deleteSource } from "../hooks/useApi";
|
||||
import {
|
||||
getSources,
|
||||
deleteSource,
|
||||
uploadFile,
|
||||
confirmUpload,
|
||||
cancelUpload,
|
||||
} from "../hooks/useApi";
|
||||
|
||||
const SOURCE_TYPES = [
|
||||
{
|
||||
@@ -40,7 +46,6 @@ const SOURCE_TYPES = [
|
||||
|
||||
const ALLOWED_EXTS = ["csv", "txt", "xlsx", "xls"];
|
||||
|
||||
// Toast 组件
|
||||
function Toast({ message, type, onClose }) {
|
||||
useEffect(() => {
|
||||
const t = setTimeout(onClose, 3000);
|
||||
@@ -137,15 +142,10 @@ export default function Import() {
|
||||
formData.append("file", file);
|
||||
formData.append("source_type", sourceType);
|
||||
formData.append("source_name", sourceType);
|
||||
const res = await fetch("/api/upload", {
|
||||
method: "POST",
|
||||
body: formData,
|
||||
});
|
||||
const data = await res.json();
|
||||
if (!res.ok) throw new Error(data.error || "解析失败");
|
||||
const data = await uploadFile(formData);
|
||||
setPreview(data);
|
||||
} catch (err) {
|
||||
setError(err.message || "上传失败");
|
||||
setError(err.response?.data?.error || err.message || "上传失败");
|
||||
}
|
||||
setUploading(false);
|
||||
}
|
||||
@@ -154,13 +154,7 @@ export default function Import() {
|
||||
if (!preview?.tempId) return;
|
||||
setConfirming(true);
|
||||
try {
|
||||
const res = await fetch("/api/upload/confirm", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ tempId: preview.tempId }),
|
||||
});
|
||||
const data = await res.json();
|
||||
if (!res.ok) throw new Error(data.error || "导入失败");
|
||||
const data = await confirmUpload(preview.tempId);
|
||||
setPreview(null);
|
||||
showToast(
|
||||
`导入成功!写入 ${data.inserted} 条${data.skipped > 0 ? ",跳过 " + data.skipped + " 条重复" : ""}`,
|
||||
@@ -168,18 +162,19 @@ export default function Import() {
|
||||
);
|
||||
loadSources();
|
||||
} catch (err) {
|
||||
showToast("导入失败: " + err.message, "error");
|
||||
showToast(
|
||||
"导入失败: " + (err.response?.data?.error || err.message),
|
||||
"error",
|
||||
);
|
||||
}
|
||||
setConfirming(false);
|
||||
}
|
||||
|
||||
async function handleCancel() {
|
||||
if (preview?.tempId) {
|
||||
await fetch("/api/upload/cancel", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ tempId: preview.tempId }),
|
||||
});
|
||||
try {
|
||||
await cancelUpload(preview.tempId);
|
||||
} catch (e) {}
|
||||
}
|
||||
setPreview(null);
|
||||
}
|
||||
@@ -215,7 +210,6 @@ export default function Import() {
|
||||
|
||||
return (
|
||||
<div>
|
||||
{/* Toast */}
|
||||
{toast && (
|
||||
<Toast
|
||||
key={toast.key}
|
||||
@@ -389,7 +383,6 @@ export default function Import() {
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* 确认导入弹框 */}
|
||||
{preview && (
|
||||
<div className="modal-overlay">
|
||||
<div className="modal" style={{ maxWidth: 480 }}>
|
||||
@@ -500,7 +493,6 @@ export default function Import() {
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* 删除确认弹框 */}
|
||||
{deleteTarget && (
|
||||
<div className="modal-overlay">
|
||||
<div className="modal" style={{ maxWidth: 400 }}>
|
||||
|
||||
@@ -0,0 +1,211 @@
|
||||
import React, { useState } from 'react';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { useAuth } from '../context/AuthContext';
|
||||
import { LogIn, UserPlus, AlertCircle } from 'lucide-react';
|
||||
|
||||
export default function Login() {
|
||||
const { login, register } = useAuth();
|
||||
const navigate = useNavigate();
|
||||
|
||||
const [mode, setMode] = useState('login'); // 'login' | 'register'
|
||||
const [username, setUsername] = useState('');
|
||||
const [password, setPassword] = useState('');
|
||||
const [nickname, setNickname] = useState('');
|
||||
const [error, setError] = useState('');
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
function switchMode() {
|
||||
setMode(mode === 'login' ? 'register' : 'login');
|
||||
setError('');
|
||||
}
|
||||
|
||||
async function handleSubmit(e) {
|
||||
e.preventDefault();
|
||||
setError('');
|
||||
|
||||
if (!username.trim() || !password.trim()) {
|
||||
setError('请填写用户名和密码');
|
||||
return;
|
||||
}
|
||||
|
||||
setLoading(true);
|
||||
try {
|
||||
if (mode === 'login') {
|
||||
await login(username.trim(), password);
|
||||
} else {
|
||||
await register(username.trim(), password, nickname.trim() || username.trim());
|
||||
}
|
||||
navigate('/', { replace: true });
|
||||
} catch (err) {
|
||||
const msg = err.response?.data?.error || err.message || '操作失败';
|
||||
setError(msg);
|
||||
}
|
||||
setLoading(false);
|
||||
}
|
||||
|
||||
return (
|
||||
<div style={{
|
||||
minHeight: '100vh',
|
||||
display: 'flex',
|
||||
alignItems: 'center',
|
||||
justifyContent: 'center',
|
||||
background: 'linear-gradient(135deg, #667eea 0%, #764ba2 100%)',
|
||||
padding: 20,
|
||||
}}>
|
||||
<div style={{
|
||||
background: 'white',
|
||||
borderRadius: 16,
|
||||
padding: 40,
|
||||
width: '100%',
|
||||
maxWidth: 400,
|
||||
boxShadow: '0 20px 60px rgba(0,0,0,0.15)',
|
||||
}}>
|
||||
{/* Logo */}
|
||||
<div style={{ textAlign: 'center', marginBottom: 32 }}>
|
||||
<div style={{ fontSize: 48, marginBottom: 8 }}>💰</div>
|
||||
<h1 style={{ fontSize: 24, fontWeight: 700, margin: 0 }}>Reco</h1>
|
||||
<p style={{ fontSize: 13, color: '#64748b', marginTop: 4 }}>
|
||||
银行流水对账分析系统
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{/* Error */}
|
||||
{error && (
|
||||
<div style={{
|
||||
marginBottom: 16,
|
||||
padding: '10px 14px',
|
||||
background: '#fef2f2',
|
||||
borderRadius: 8,
|
||||
color: '#ef4444',
|
||||
fontSize: 13,
|
||||
display: 'flex',
|
||||
alignItems: 'center',
|
||||
gap: 8,
|
||||
}}>
|
||||
<AlertCircle size={16} />
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Form */}
|
||||
<form onSubmit={handleSubmit}>
|
||||
<div style={{ marginBottom: 14 }}>
|
||||
<label style={{ display: 'block', fontSize: 13, fontWeight: 500, marginBottom: 6 }}>
|
||||
用户名
|
||||
</label>
|
||||
<input
|
||||
type="text"
|
||||
value={username}
|
||||
onChange={(e) => setUsername(e.target.value)}
|
||||
placeholder="请输入用户名(3-32位)"
|
||||
autoFocus
|
||||
style={{
|
||||
width: '100%',
|
||||
padding: '10px 14px',
|
||||
border: '1px solid #e2e8f0',
|
||||
borderRadius: 8,
|
||||
fontSize: 14,
|
||||
outline: 'none',
|
||||
boxSizing: 'border-box',
|
||||
}}
|
||||
onFocus={(e) => { e.target.style.borderColor = '#818cf8'; e.target.style.boxShadow = '0 0 0 3px #eef2ff'; }}
|
||||
onBlur={(e) => { e.target.style.borderColor = '#e2e8f0'; e.target.style.boxShadow = 'none'; }}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div style={{ marginBottom: 14 }}>
|
||||
<label style={{ display: 'block', fontSize: 13, fontWeight: 500, marginBottom: 6 }}>
|
||||
密码
|
||||
</label>
|
||||
<input
|
||||
type="password"
|
||||
value={password}
|
||||
onChange={(e) => setPassword(e.target.value)}
|
||||
placeholder={mode === 'register' ? '至少6个字符' : '请输入密码'}
|
||||
style={{
|
||||
width: '100%',
|
||||
padding: '10px 14px',
|
||||
border: '1px solid #e2e8f0',
|
||||
borderRadius: 8,
|
||||
fontSize: 14,
|
||||
outline: 'none',
|
||||
boxSizing: 'border-box',
|
||||
}}
|
||||
onFocus={(e) => { e.target.style.borderColor = '#818cf8'; e.target.style.boxShadow = '0 0 0 3px #eef2ff'; }}
|
||||
onBlur={(e) => { e.target.style.borderColor = '#e2e8f0'; e.target.style.boxShadow = 'none'; }}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{mode === 'register' && (
|
||||
<div style={{ marginBottom: 14 }}>
|
||||
<label style={{ display: 'block', fontSize: 13, fontWeight: 500, marginBottom: 6 }}>
|
||||
昵称 <span style={{ color: '#94a3b8', fontWeight: 400 }}>(可选)</span>
|
||||
</label>
|
||||
<input
|
||||
type="text"
|
||||
value={nickname}
|
||||
onChange={(e) => setNickname(e.target.value)}
|
||||
placeholder="显示名称,默认为用户名"
|
||||
style={{
|
||||
width: '100%',
|
||||
padding: '10px 14px',
|
||||
border: '1px solid #e2e8f0',
|
||||
borderRadius: 8,
|
||||
fontSize: 14,
|
||||
outline: 'none',
|
||||
boxSizing: 'border-box',
|
||||
}}
|
||||
onFocus={(e) => { e.target.style.borderColor = '#818cf8'; e.target.style.boxShadow = '0 0 0 3px #eef2ff'; }}
|
||||
onBlur={(e) => { e.target.style.borderColor = '#e2e8f0'; e.target.style.boxShadow = 'none'; }}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<button
|
||||
type="submit"
|
||||
disabled={loading}
|
||||
style={{
|
||||
width: '100%',
|
||||
padding: '12px',
|
||||
background: loading ? '#a5b4fc' : '#4f46e5',
|
||||
color: 'white',
|
||||
border: 'none',
|
||||
borderRadius: 8,
|
||||
fontSize: 15,
|
||||
fontWeight: 600,
|
||||
cursor: loading ? 'not-allowed' : 'pointer',
|
||||
display: 'flex',
|
||||
alignItems: 'center',
|
||||
justifyContent: 'center',
|
||||
gap: 8,
|
||||
marginTop: 6,
|
||||
}}
|
||||
>
|
||||
{mode === 'login' ? <LogIn size={18} /> : <UserPlus size={18} />}
|
||||
{loading ? '处理中...' : mode === 'login' ? '登录' : '注册'}
|
||||
</button>
|
||||
</form>
|
||||
|
||||
{/* Switch mode */}
|
||||
<div style={{ textAlign: 'center', marginTop: 20, fontSize: 13, color: '#64748b' }}>
|
||||
{mode === 'login' ? '还没有账号?' : '已有账号?'}
|
||||
<button
|
||||
onClick={switchMode}
|
||||
style={{
|
||||
background: 'none',
|
||||
border: 'none',
|
||||
color: '#4f46e5',
|
||||
cursor: 'pointer',
|
||||
fontSize: 13,
|
||||
fontWeight: 600,
|
||||
marginLeft: 4,
|
||||
padding: 0,
|
||||
}}
|
||||
>
|
||||
{mode === 'login' ? '注册新账号' : '去登录'}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -14,6 +14,10 @@ import {
|
||||
} from "lucide-react";
|
||||
import {
|
||||
getTransactions,
|
||||
getDeals,
|
||||
updateDeal,
|
||||
clearDeals,
|
||||
getNeedsClassify,
|
||||
runReconciliation,
|
||||
classifyTransactions,
|
||||
formatAmount,
|
||||
@@ -150,8 +154,7 @@ export default function Transactions() {
|
||||
setClassifyProgress(task.progress || 0);
|
||||
const poll = setInterval(async () => {
|
||||
try {
|
||||
const res = await fetch("/api/transactions/needs-classify");
|
||||
const { total } = await res.json();
|
||||
const { total } = await getNeedsClassify();
|
||||
const p =
|
||||
task.total > 0
|
||||
? Math.round(((task.total - total) / task.total) * 100)
|
||||
@@ -192,12 +195,10 @@ export default function Transactions() {
|
||||
async function loadDeals(f) {
|
||||
setDealLoading(true);
|
||||
try {
|
||||
const p = new URLSearchParams();
|
||||
Object.entries(f).forEach(([k, v]) => {
|
||||
if (v !== "" && v !== undefined) p.set(k, v);
|
||||
});
|
||||
const r = await fetch("/api/deals?" + p);
|
||||
setDeals(await r.json());
|
||||
const params = {};
|
||||
for (const [k, v] of Object.entries(f))
|
||||
if (v !== "" && v !== undefined) params[k] = v;
|
||||
setDeals(await getDeals(params));
|
||||
} catch (e) {}
|
||||
setDealLoading(false);
|
||||
}
|
||||
@@ -234,13 +235,9 @@ export default function Transactions() {
|
||||
async function handleUpdateDeal() {
|
||||
if (!editingDeal) return;
|
||||
try {
|
||||
await fetch("/api/deals/" + editingDeal.id, {
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
category: editingDeal.category,
|
||||
ai_description: editingDeal.ai_description,
|
||||
}),
|
||||
await updateDeal(editingDeal.id, {
|
||||
category: editingDeal.category,
|
||||
ai_description: editingDeal.ai_description,
|
||||
});
|
||||
setEditingDeal(null);
|
||||
loadDeals(dealFilters);
|
||||
@@ -252,8 +249,15 @@ export default function Transactions() {
|
||||
|
||||
async function handleClearDeals() {
|
||||
try {
|
||||
await fetch("/api/deals/clear", { method: "POST" });
|
||||
setDeals({ deals: [], total: 0, page: 1, totalPages: 0 });
|
||||
await clearDeals();
|
||||
setDeals({
|
||||
deals: [],
|
||||
total: 0,
|
||||
page: 1,
|
||||
totalPages: 0,
|
||||
totalIncome: 0,
|
||||
totalExpense: 0,
|
||||
});
|
||||
setClearConfirm(false);
|
||||
} catch (e) {}
|
||||
}
|
||||
@@ -280,8 +284,7 @@ export default function Transactions() {
|
||||
JSON.stringify({ total: 0, progress: 0, ts: Date.now() }),
|
||||
);
|
||||
try {
|
||||
const res = await fetch("/api/transactions/needs-classify");
|
||||
const { ids, total } = await res.json();
|
||||
const { ids, total } = await getNeedsClassify();
|
||||
if (!ids.length) {
|
||||
showToast("所有交易分类均有效!", "info");
|
||||
setClassifying(false);
|
||||
|
||||
Reference in New Issue
Block a user