Skip to content

Commit 76b05c2

Browse files
author
CodeJudge
committed
feat: 提交记录查看题目 + 删除账户功能
1 parent 06d4ee9 commit 76b05c2

4 files changed

Lines changed: 41 additions & 3 deletions

File tree

backend/src/routes/auth.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ router.post('/register', register);
77
router.post('/login', login);
88
router.get('/profile', authenticate, getProfile);
99
router.put('/password', authenticate, changePassword);
10+
router.delete('/profile', authenticate, (req, res) => {
11+
run('DELETE FROM submissions WHERE user_id = ?', [req.user.id]);
12+
run('DELETE FROM users WHERE id = ?', [req.user.id]);
13+
res.json({ message: '账户已删除' });
14+
});
1015

1116
router.get('/solved-calendar', authenticate, (req, res) => {
1217
const rows = queryAll(

frontend/src/pages/Profile.tsx

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { useState, useEffect } from 'react';
2-
import { Link } from 'react-router-dom';
3-
import { User, Mail, Shield, Award, TrendingUp, Loader2, AlertTriangle, CheckCircle2, XCircle, Clock, Zap, Lock, Key, CalendarDays, Code, PieChart } from 'lucide-react';
2+
import { Link, useNavigate } from 'react-router-dom';
3+
import { User, Mail, Shield, Award, TrendingUp, Loader2, AlertTriangle, CheckCircle2, XCircle, Clock, Zap, Lock, Key, CalendarDays, Code, PieChart, Trash2 } from 'lucide-react';
44
import { useAuth } from '../context/AuthContext';
55
import api from '../services/api';
6+
import toast from 'react-hot-toast';
67
import type { Submission } from '../types';
78
import { useDocumentTitle } from '../hooks/useDocumentTitle';
89
import GamificationSection from '../components/GamificationSection';
@@ -38,7 +39,8 @@ const STATUS_MAP: Record<string, { label: string; icon: React.ReactNode; classNa
3839

3940
export default function Profile() {
4041
useDocumentTitle('个人中心');
41-
const { user } = useAuth();
42+
const { user, logout } = useAuth();
43+
const navigate = useNavigate();
4244
const [profile, setProfile] = useState<any>(null);
4345
const [submissions, setSubmissions] = useState<Submission[]>([]);
4446
const [loading, setLoading] = useState(true);
@@ -424,6 +426,29 @@ export default function Profile() {
424426
)}
425427
</div>
426428

429+
{/* Delete Account */}
430+
<div className="card p-6 border-red-600/20">
431+
<button
432+
onClick={async () => {
433+
if (window.confirm('确定要删除账户吗?所有提交记录也将被永久删除。此操作不可撤销。')) {
434+
try {
435+
await api.auth.deleteAccount();
436+
toast.success('账户已删除');
437+
logout();
438+
navigate('/');
439+
} catch (err: any) {
440+
toast.error(err.message || '删除失败');
441+
}
442+
}
443+
}}
444+
className="flex items-center gap-2 text-sm font-medium text-red-400 hover:text-red-300 transition-colors"
445+
>
446+
<Trash2 className="w-4 h-4" />
447+
删除账户
448+
</button>
449+
<p className="text-xs text-dark-500 mt-2">删除后不可恢复,所有数据将被清除</p>
450+
</div>
451+
427452
{/* Recent submissions */}
428453
<div className="card p-6">
429454
<h2 className="text-lg font-semibold text-white mb-4">最近提交</h2>

frontend/src/pages/Submissions.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useState, useEffect, useCallback } from 'react';
2+
import { Link } from 'react-router-dom';
23
import {
34
ChevronLeft,
45
ChevronRight,
@@ -259,6 +260,12 @@ export default function Submissions() {
259260

260261
{expandedId === Number(submission.id) && (
261262
<div className="px-4 pb-4 mt-2 border-t border-dark-700 pt-3">
263+
<Link
264+
to={`/problems/${submission.problem_id}`}
265+
className="inline-flex items-center gap-1 text-sm text-primary-400 hover:text-primary-300 mb-3 transition-colors"
266+
>
267+
<Send size={14} /> 查看题目 #{submission.problem_id}
268+
</Link>
262269
{submission.code && (
263270
<div className="mb-3">
264271
<div className="flex items-center justify-between mb-1">

frontend/src/services/api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const api = {
3838
changePassword: (body: { currentPassword: string; newPassword: string }) =>
3939
request<{ message: string }>('/auth/password', { method: 'PUT', body: JSON.stringify(body) }),
4040
recentSubmissions: () => request<{ submissions: any[] }>('/auth/recent-submissions'),
41+
deleteAccount: () => request<{ message: string }>('/auth/profile', { method: 'DELETE' }),
4142
},
4243

4344
problems: {

0 commit comments

Comments
 (0)