feat: 完成多用户系统(登录/注册界面 + 用户隔离)
This commit is contained in:
@@ -4,6 +4,8 @@ import StatsPanel from './components/StatsPanel';
|
||||
import Calendar from './components/Calendar';
|
||||
import DiaryDetail from './components/DiaryDetail';
|
||||
import ExperienceList from './components/ExperienceList';
|
||||
import Login from './components/Login';
|
||||
import Register from './components/Register';
|
||||
|
||||
const API_BASE = '/api';
|
||||
|
||||
@@ -11,6 +13,7 @@ const API_BASE = '/api';
|
||||
* ⚠️ 主应用组件
|
||||
*
|
||||
* 核心功能:
|
||||
* - 用户认证
|
||||
* - 统计面板
|
||||
* - 日历组件 ⭐
|
||||
* - 日记详情
|
||||
@@ -19,6 +22,8 @@ const API_BASE = '/api';
|
||||
* ⚠️ 修改前阅读 docs/README.md
|
||||
*/
|
||||
function App() {
|
||||
const [user, setUser] = useState(null);
|
||||
const [authView, setAuthView] = useState('login'); // login | register
|
||||
const [selectedDate, setSelectedDate] = useState(new Date().toISOString().split('T')[0]);
|
||||
const [diaryDates, setDiaryDates] = useState([]);
|
||||
const [entries, setEntries] = useState([]);
|
||||
@@ -28,9 +33,50 @@ function App() {
|
||||
const [error, setError] = useState(null);
|
||||
|
||||
useEffect(() => {
|
||||
loadData();
|
||||
// 检查是否已登录
|
||||
checkAuth();
|
||||
}, []);
|
||||
|
||||
const checkAuth = async () => {
|
||||
try {
|
||||
const res = await axios.get(`${API_BASE}/auth/me/`);
|
||||
setUser(res.data);
|
||||
loadData();
|
||||
} catch (err) {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleLogin = (userData, view) => {
|
||||
if (view) {
|
||||
setAuthView(view);
|
||||
} else if (userData) {
|
||||
setUser(userData);
|
||||
loadData();
|
||||
}
|
||||
};
|
||||
|
||||
const handleRegister = (userData, view) => {
|
||||
if (view) {
|
||||
setAuthView(view);
|
||||
} else if (userData) {
|
||||
setUser(userData);
|
||||
loadData();
|
||||
}
|
||||
};
|
||||
|
||||
const handleLogout = async () => {
|
||||
try {
|
||||
await axios.post(`${API_BASE}/auth/logout/`);
|
||||
setUser(null);
|
||||
setEntries([]);
|
||||
setExperiences([]);
|
||||
setStats({});
|
||||
} catch (err) {
|
||||
console.error('登出失败:', err);
|
||||
}
|
||||
};
|
||||
|
||||
const loadData = async () => {
|
||||
try {
|
||||
const [statsRes, entriesRes, expRes] = await Promise.all([
|
||||
@@ -50,6 +96,14 @@ function App() {
|
||||
}
|
||||
};
|
||||
|
||||
// 未登录显示登录/注册界面
|
||||
if (!user) {
|
||||
if (authView === 'register') {
|
||||
return <Register onRegister={handleRegister} />;
|
||||
}
|
||||
return <Login onLogin={handleLogin} />;
|
||||
}
|
||||
|
||||
const handleDateSelect = (date) => {
|
||||
setSelectedDate(date);
|
||||
};
|
||||
@@ -67,8 +121,16 @@ function App() {
|
||||
return (
|
||||
<div className="container">
|
||||
<header>
|
||||
<h1>⚡ 码神的日记系统</h1>
|
||||
<p>记录每天的进步与成长</p>
|
||||
<div className="header-content">
|
||||
<div>
|
||||
<h1>⚡ 码神的日记系统</h1>
|
||||
<p>记录每天的进步与成长</p>
|
||||
</div>
|
||||
<div className="user-info">
|
||||
<span>👤 {user.username}</span>
|
||||
<button onClick={handleLogout} className="btn-logout">登出</button>
|
||||
</div>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<StatsPanel stats={stats} />
|
||||
|
||||
77
frontend-react/src/components/Login.js
Normal file
77
frontend-react/src/components/Login.js
Normal file
@@ -0,0 +1,77 @@
|
||||
import React, { useState } from 'react';
|
||||
import axios from 'axios';
|
||||
|
||||
const API_BASE = '/api';
|
||||
|
||||
/**
|
||||
* 登录组件
|
||||
*/
|
||||
function Login({ onLogin }) {
|
||||
const [username, setUsername] = useState('');
|
||||
const [password, setPassword] = useState('');
|
||||
const [error, setError] = useState('');
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
const handleSubmit = async (e) => {
|
||||
e.preventDefault();
|
||||
setLoading(true);
|
||||
setError('');
|
||||
|
||||
try {
|
||||
const res = await axios.post(`${API_BASE}/auth/login/`, {
|
||||
username,
|
||||
password
|
||||
});
|
||||
onLogin(res.data.user);
|
||||
} catch (err) {
|
||||
setError(err.response?.data?.message || '登录失败');
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="auth-container">
|
||||
<div className="auth-card">
|
||||
<h1>⚡ 码神日记系统</h1>
|
||||
<h2>登录</h2>
|
||||
|
||||
{error && <div className="error">{error}</div>}
|
||||
|
||||
<form onSubmit={handleSubmit}>
|
||||
<div className="form-group">
|
||||
<label>用户名</label>
|
||||
<input
|
||||
type="text"
|
||||
value={username}
|
||||
onChange={(e) => setUsername(e.target.value)}
|
||||
required
|
||||
placeholder="请输入用户名"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="form-group">
|
||||
<label>密码</label>
|
||||
<input
|
||||
type="password"
|
||||
value={password}
|
||||
onChange={(e) => setPassword(e.target.value)}
|
||||
required
|
||||
placeholder="请输入密码"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<button type="submit" className="btn-primary" disabled={loading}>
|
||||
{loading ? '登录中...' : '登录'}
|
||||
</button>
|
||||
</form>
|
||||
|
||||
<p className="auth-tip">
|
||||
还没有账号?<a href="#register" onClick={(e) => { e.preventDefault(); onLogin(null, 'register'); }}>立即注册</a>
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default Login;
|
||||
112
frontend-react/src/components/Register.js
Normal file
112
frontend-react/src/components/Register.js
Normal file
@@ -0,0 +1,112 @@
|
||||
import React, { useState } from 'react';
|
||||
import axios from 'axios';
|
||||
|
||||
const API_BASE = '/api';
|
||||
|
||||
/**
|
||||
* 注册组件
|
||||
*/
|
||||
function Register({ onRegister }) {
|
||||
const [username, setUsername] = useState('');
|
||||
const [email, setEmail] = useState('');
|
||||
const [password, setPassword] = useState('');
|
||||
const [confirmPassword, setConfirmPassword] = useState('');
|
||||
const [error, setError] = useState('');
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
const handleSubmit = async (e) => {
|
||||
e.preventDefault();
|
||||
|
||||
if (password !== confirmPassword) {
|
||||
setError('两次输入的密码不一致');
|
||||
return;
|
||||
}
|
||||
|
||||
if (password.length < 6) {
|
||||
setError('密码至少 6 位');
|
||||
return;
|
||||
}
|
||||
|
||||
setLoading(true);
|
||||
setError('');
|
||||
|
||||
try {
|
||||
const res = await axios.post(`${API_BASE}/auth/register/`, {
|
||||
username,
|
||||
email,
|
||||
password
|
||||
});
|
||||
onRegister(res.data.user);
|
||||
} catch (err) {
|
||||
setError(err.response?.data?.username?.[0] || err.response?.data?.message || '注册失败');
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="auth-container">
|
||||
<div className="auth-card">
|
||||
<h1>⚡ 码神日记系统</h1>
|
||||
<h2>注册</h2>
|
||||
|
||||
{error && <div className="error">{error}</div>}
|
||||
|
||||
<form onSubmit={handleSubmit}>
|
||||
<div className="form-group">
|
||||
<label>用户名</label>
|
||||
<input
|
||||
type="text"
|
||||
value={username}
|
||||
onChange={(e) => setUsername(e.target.value)}
|
||||
required
|
||||
placeholder="请输入用户名"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="form-group">
|
||||
<label>邮箱(可选)</label>
|
||||
<input
|
||||
type="email"
|
||||
value={email}
|
||||
onChange={(e) => setEmail(e.target.value)}
|
||||
placeholder="请输入邮箱"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="form-group">
|
||||
<label>密码</label>
|
||||
<input
|
||||
type="password"
|
||||
value={password}
|
||||
onChange={(e) => setPassword(e.target.value)}
|
||||
required
|
||||
placeholder="至少 6 位"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="form-group">
|
||||
<label>确认密码</label>
|
||||
<input
|
||||
type="password"
|
||||
value={confirmPassword}
|
||||
onChange={(e) => setConfirmPassword(e.target.value)}
|
||||
required
|
||||
placeholder="再次输入密码"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<button type="submit" className="btn-primary" disabled={loading}>
|
||||
{loading ? '注册中...' : '注册'}
|
||||
</button>
|
||||
</form>
|
||||
|
||||
<p className="auth-tip">
|
||||
已有账号?<a href="#login" onClick={(e) => { e.preventDefault(); onRegister(null, 'login'); }}>立即登录</a>
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default Register;
|
||||
@@ -262,6 +262,128 @@ header p {
|
||||
color: #999;
|
||||
}
|
||||
|
||||
/* 认证界面 */
|
||||
.auth-container {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
min-height: 100vh;
|
||||
padding: 20px;
|
||||
}
|
||||
|
||||
.auth-card {
|
||||
background: white;
|
||||
padding: 40px;
|
||||
border-radius: 10px;
|
||||
box-shadow: 0 4px 20px rgba(0,0,0,0.2);
|
||||
width: 100%;
|
||||
max-width: 400px;
|
||||
}
|
||||
|
||||
.auth-card h1 {
|
||||
color: #667eea;
|
||||
font-size: 2em;
|
||||
text-align: center;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
|
||||
.auth-card h2 {
|
||||
color: #333;
|
||||
font-size: 1.5em;
|
||||
text-align: center;
|
||||
margin-bottom: 30px;
|
||||
}
|
||||
|
||||
.form-group {
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
|
||||
.form-group label {
|
||||
display: block;
|
||||
color: #555;
|
||||
font-weight: 500;
|
||||
margin-bottom: 8px;
|
||||
}
|
||||
|
||||
.form-group input {
|
||||
width: 100%;
|
||||
padding: 12px;
|
||||
border: 1px solid #ddd;
|
||||
border-radius: 5px;
|
||||
font-size: 1em;
|
||||
transition: border-color 0.2s;
|
||||
}
|
||||
|
||||
.form-group input:focus {
|
||||
outline: none;
|
||||
border-color: #667eea;
|
||||
}
|
||||
|
||||
.btn-primary {
|
||||
width: 100%;
|
||||
padding: 12px;
|
||||
background: linear-gradient(135deg, #667eea, #764ba2);
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: 5px;
|
||||
font-size: 1.1em;
|
||||
font-weight: 600;
|
||||
cursor: pointer;
|
||||
transition: transform 0.2s;
|
||||
}
|
||||
|
||||
.btn-primary:hover {
|
||||
transform: translateY(-2px);
|
||||
}
|
||||
|
||||
.btn-primary:disabled {
|
||||
opacity: 0.6;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.auth-tip {
|
||||
text-align: center;
|
||||
margin-top: 20px;
|
||||
color: #666;
|
||||
}
|
||||
|
||||
.auth-tip a {
|
||||
color: #667eea;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.auth-tip a:hover {
|
||||
text-decoration: underline;
|
||||
}
|
||||
|
||||
/* 用户信息 */
|
||||
.header-content {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.user-info {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 15px;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.btn-logout {
|
||||
padding: 8px 16px;
|
||||
background: rgba(255,255,255,0.2);
|
||||
color: white;
|
||||
border: 1px solid rgba(255,255,255,0.5);
|
||||
border-radius: 5px;
|
||||
cursor: pointer;
|
||||
transition: background 0.2s;
|
||||
}
|
||||
|
||||
.btn-logout:hover {
|
||||
background: rgba(255,255,255,0.3);
|
||||
}
|
||||
|
||||
/* 移动端 */
|
||||
@media (max-width: 768px) {
|
||||
.grid-2 {
|
||||
|
||||
Reference in New Issue
Block a user