blob: 400884b080c730a88f4077c1c5bea1bae8500de5 [file] [log] [blame]
TRM-coding130f05c2025-06-15 16:05:28 +08001import React, { useState, useEffect } from 'react';
TRM-coding78aa9662025-06-17 23:40:10 +08002import '../style/Admin.css';
TRM-coding130f05c2025-06-15 16:05:28 +08003
4function UserManagement() {
5 const [users, setUsers] = useState([]);
6
7 useEffect(() => {
8 fetch('/api/users')
9 .then(res => res.json())
10 .then(data => setUsers(data))
11 .catch(console.error);
12 }, []);
13
14 const handleUserAction = (id, action) => {
15 fetch(`/api/users/${id}/${action}`, { method: 'POST' })
16 .then(res => res.ok && setUsers(us => us.filter(u => u.id !== id)))
17 .catch(console.error);
18 };
19
20 return (
21 <div className="admin-container">
22 <h2>用户管理</h2>
23 <table className="admin-table">
24 <thead>
25 <tr><th>用户名</th><th>角色</th><th>操作</th></tr>
26 </thead>
27 <tbody>
28 {users.map(u => (
29 <tr key={u.id}>
30 <td>{u.username}</td>
31 <td>{u.role}</td>
32 <td>
33 <button onClick={() => handleUserAction(u.id, 'ban')}>封禁</button>
34 <button onClick={() => handleUserAction(u.id, 'promote')}>提升权限</button>
35 </td>
36 </tr>
37 ))}
38 </tbody>
39 </table>
40 </div>
41 );
42}
43
44export default UserManagement;