Qilan2 commited on
Commit
128909e
·
verified ·
1 Parent(s): fa604e7

Create admin.html

Browse files
Files changed (1) hide show
  1. st-free/admin.html +682 -0
st-free/admin.html ADDED
@@ -0,0 +1,682 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="zh">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>管理员面板</title>
7
+ <link rel="stylesheet" href="../css/main.css">
8
+ <link rel="stylesheet" href="../css/loader.css">
9
+ <link rel="stylesheet" href="../css/bg_override.css">
10
+ <link rel="stylesheet" href="../css/fa.min.css">
11
+ <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.css">
12
+ <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
13
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.js"></script>
14
+ <style>
15
+ .admin-container {
16
+ max-width: 1200px;
17
+ margin: 20px auto;
18
+ padding: 30px;
19
+ background-color: var(--black80a);
20
+ border-radius: 15px;
21
+ box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
22
+ }
23
+ .users-grid {
24
+ display: grid;
25
+ grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
26
+ gap: 25px;
27
+ margin-top: 25px;
28
+ }
29
+ .user-card {
30
+ background-color: var(--black60a);
31
+ border-radius: 15px;
32
+ padding: 25px;
33
+ transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
34
+ box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
35
+ border: 1px solid rgba(255, 255, 255, 0.1);
36
+ backdrop-filter: blur(10px);
37
+ }
38
+ .user-card:hover {
39
+ background-color: var(--black90a);
40
+ transform: translateY(-5px);
41
+ box-shadow: 0 8px 15px rgba(0, 0, 0, 0.2);
42
+ border-color: rgba(255, 255, 255, 0.2);
43
+ }
44
+ .user-header {
45
+ display: flex;
46
+ align-items: center;
47
+ gap: 20px;
48
+ margin-bottom: 20px;
49
+ }
50
+ .user-avatar {
51
+ width: 70px;
52
+ height: 70px;
53
+ border-radius: 50%;
54
+ object-fit: cover;
55
+ border: 3px solid var(--accent);
56
+ box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
57
+ transition: all 0.3s ease;
58
+ }
59
+ .user-card:hover .user-avatar {
60
+ transform: scale(1.05);
61
+ border-color: #e74c3c;
62
+ }
63
+ .user-info {
64
+ flex-grow: 1;
65
+ }
66
+ .user-name {
67
+ font-size: 1.3em;
68
+ font-weight: bold;
69
+ margin: 0 0 8px 0;
70
+ color: var(--white95);
71
+ }
72
+ .user-handle {
73
+ color: var(--grey70);
74
+ font-size: 0.95em;
75
+ margin-bottom: 12px;
76
+ opacity: 0.8;
77
+ }
78
+ .badge {
79
+ display: inline-block;
80
+ padding: 6px 12px;
81
+ border-radius: 20px;
82
+ font-size: 0.85em;
83
+ margin-right: 8px;
84
+ font-weight: 500;
85
+ letter-spacing: 0.3px;
86
+ text-transform: uppercase;
87
+ box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
88
+ }
89
+ .badge.admin {
90
+ background: linear-gradient(135deg, #e74c3c 0%, #c0392b 100%);
91
+ color: white;
92
+ min-width: 70px;
93
+ text-align: center;
94
+ }
95
+ .badge.user {
96
+ background: linear-gradient(135deg, var(--black60a) 0%, var(--black90a) 100%);
97
+ color: var(--grey70);
98
+ min-width: 70px;
99
+ text-align: center;
100
+ border: 1px solid rgba(255, 255, 255, 0.1);
101
+ }
102
+ .badge.enabled {
103
+ background: linear-gradient(135deg, #2ecc71 0%, #27ae60 100%);
104
+ color: white;
105
+ }
106
+ .badge.disabled {
107
+ background: linear-gradient(135deg, #e74c3c 0%, #c0392b 100%);
108
+ color: white;
109
+ }
110
+ .user-actions {
111
+ display: flex;
112
+ gap: 10px;
113
+ flex-wrap: wrap;
114
+ margin-top: 15px;
115
+ padding-top: 15px;
116
+ border-top: 1px solid rgba(255, 255, 255, 0.1);
117
+ }
118
+ .action-btn {
119
+ padding: 10px 15px;
120
+ border-radius: 8px;
121
+ border: none;
122
+ cursor: pointer;
123
+ font-size: 0.9em;
124
+ display: flex;
125
+ align-items: center;
126
+ gap: 8px;
127
+ background-color: var(--black60a);
128
+ color: var(--white90);
129
+ transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
130
+ border: 1px solid rgba(255, 255, 255, 0.1);
131
+ flex: 1;
132
+ justify-content: center;
133
+ min-width: 100px;
134
+ }
135
+ .action-btn:hover {
136
+ background-color: var(--black90a);
137
+ transform: translateY(-2px);
138
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
139
+ border-color: rgba(255, 255, 255, 0.2);
140
+ }
141
+ .action-btn.danger {
142
+ background: linear-gradient(135deg, var(--warning50a) 0%, var(--warning70a) 100%);
143
+ }
144
+ .action-btn.danger:hover {
145
+ background: linear-gradient(135deg, var(--warning70a) 0%, var(--warning90a) 100%);
146
+ }
147
+ .pagination {
148
+ display: flex;
149
+ justify-content: center;
150
+ align-items: center;
151
+ gap: 12px;
152
+ margin-top: 40px;
153
+ flex-wrap: wrap;
154
+ }
155
+ .page-btn {
156
+ padding: 10px 18px;
157
+ border-radius: 8px;
158
+ border: none;
159
+ cursor: pointer;
160
+ background-color: var(--black60a);
161
+ color: var(--white90);
162
+ transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
163
+ border: 1px solid rgba(255, 255, 255, 0.1);
164
+ font-weight: 500;
165
+ }
166
+ .page-btn:hover:not(.active):not(:disabled) {
167
+ background-color: var(--black90a);
168
+ transform: translateY(-2px);
169
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
170
+ border-color: rgba(255, 255, 255, 0.2);
171
+ }
172
+ .page-btn.active {
173
+ background: linear-gradient(135deg, var(--accent) 0%, var(--accent-dark) 100%);
174
+ font-weight: bold;
175
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
176
+ }
177
+ .page-btn:disabled {
178
+ opacity: 0.5;
179
+ cursor: not-allowed;
180
+ }
181
+ .filters {
182
+ display: flex;
183
+ gap: 20px;
184
+ margin-bottom: 30px;
185
+ flex-wrap: wrap;
186
+ background-color: var(--black60a);
187
+ padding: 20px;
188
+ border-radius: 12px;
189
+ box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
190
+ border: 1px solid rgba(255, 255, 255, 0.1);
191
+ }
192
+ .search-box {
193
+ flex-grow: 1;
194
+ padding: 12px 20px;
195
+ border-radius: 8px;
196
+ border: 1px solid rgba(255, 255, 255, 0.1);
197
+ background-color: var(--black60a);
198
+ color: var(--white90);
199
+ font-size: 1em;
200
+ transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
201
+ }
202
+ .search-box:focus {
203
+ outline: none;
204
+ border-color: var(--accent);
205
+ background-color: var(--black90a);
206
+ box-shadow: 0 0 0 2px rgba(231, 76, 60, 0.2);
207
+ }
208
+ .filter-select {
209
+ padding: 12px 20px;
210
+ border-radius: 8px;
211
+ border: 1px solid rgba(255, 255, 255, 0.1);
212
+ background-color: var(--black60a);
213
+ color: var(--white90);
214
+ font-size: 1em;
215
+ cursor: pointer;
216
+ transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
217
+ min-width: 150px;
218
+ }
219
+ .filter-select:focus {
220
+ outline: none;
221
+ border-color: var(--accent);
222
+ background-color: var(--black90a);
223
+ box-shadow: 0 0 0 2px rgba(231, 76, 60, 0.2);
224
+ }
225
+ #loading {
226
+ display: none;
227
+ text-align: center;
228
+ margin: 40px 0;
229
+ font-size: 1.2em;
230
+ color: var(--grey70);
231
+ }
232
+ .page-title {
233
+ color: var(--white95);
234
+ margin-bottom: 40px;
235
+ font-size: 2.2em;
236
+ text-align: center;
237
+ text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
238
+ font-weight: 700;
239
+ letter-spacing: 0.5px;
240
+ }
241
+ .page-jump {
242
+ display: flex;
243
+ align-items: center;
244
+ gap: 12px;
245
+ margin-left: 15px;
246
+ background-color: var(--black60a);
247
+ padding: 8px 15px;
248
+ border-radius: 8px;
249
+ border: 1px solid rgba(255, 255, 255, 0.1);
250
+ }
251
+ .page-jump input {
252
+ width: 70px;
253
+ padding: 8px 12px;
254
+ border-radius: 6px;
255
+ border: 1px solid rgba(255, 255, 255, 0.1);
256
+ background-color: var(--black60a);
257
+ color: var(--white90);
258
+ text-align: center;
259
+ font-size: 0.95em;
260
+ }
261
+ .page-jump input:focus {
262
+ outline: none;
263
+ border-color: var(--accent);
264
+ box-shadow: 0 0 0 2px rgba(231, 76, 60, 0.2);
265
+ }
266
+ .page-jump button {
267
+ padding: 8px 15px;
268
+ border-radius: 6px;
269
+ border: none;
270
+ background: linear-gradient(135deg, var(--accent) 0%, var(--accent-dark) 100%);
271
+ color: white;
272
+ cursor: pointer;
273
+ transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
274
+ font-weight: 500;
275
+ }
276
+ .page-jump button:hover {
277
+ transform: translateY(-2px);
278
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
279
+ }
280
+ </style>
281
+ </head>
282
+ <body>
283
+ <div class="admin-container">
284
+ <h1 class="page-title">管理员面板</h1>
285
+ <div class="filters">
286
+ <input type="text" class="search-box" id="searchInput" placeholder="搜索用户...">
287
+ <select class="filter-select" id="roleFilter">
288
+ <option value="">所有角色</option>
289
+ <option value="admin">管理员</option>
290
+ <option value="user">普��用户</option>
291
+ </select>
292
+ <select class="filter-select" id="statusFilter">
293
+ <option value="">所有状态</option>
294
+ <option value="enabled">已启用</option>
295
+ <option value="disabled">已禁用</option>
296
+ </select>
297
+ </div>
298
+ <div class="users-grid" id="usersGrid"></div>
299
+ <div id="loading">加载中...</div>
300
+ <div class="pagination" id="pagination"></div>
301
+ </div>
302
+
303
+ <script>
304
+ // 配置toastr
305
+ toastr.options = {
306
+ "closeButton": true,
307
+ "debug": false,
308
+ "newestOnTop": true,
309
+ "progressBar": true,
310
+ "positionClass": "toast-top-right",
311
+ "preventDuplicates": false,
312
+ "onclick": null,
313
+ "showDuration": "300",
314
+ "hideDuration": "1000",
315
+ "timeOut": "5000",
316
+ "extendedTimeOut": "1000",
317
+ "showEasing": "swing",
318
+ "hideEasing": "linear",
319
+ "showMethod": "fadeIn",
320
+ "hideMethod": "fadeOut"
321
+ };
322
+
323
+ const PAGE_SIZE = 12;
324
+ let currentPage = 1;
325
+ let totalPages = 1;
326
+ let allUsers = [];
327
+ let filteredUsers = [];
328
+ let csrfToken = '';
329
+
330
+ async function getCsrfToken() {
331
+ try {
332
+ const response = await fetch('/csrf-token');
333
+ if (!response.ok) throw new Error('获取CSRF令牌失败');
334
+ const data = await response.json();
335
+ csrfToken = data.token;
336
+ } catch (error) {
337
+ console.error('获取CSRF令牌错误:', error);
338
+ toastr.error('获取CSRF令牌失败。请刷新页面重试。');
339
+ }
340
+ }
341
+
342
+ async function fetchUsers() {
343
+ try {
344
+ document.getElementById('loading').style.display = 'block';
345
+ const response = await fetch('/api/users-internal/list', {
346
+ method: 'GET',
347
+ headers: {
348
+ 'Content-Type': 'application/json',
349
+ 'X-CSRF-Token': csrfToken
350
+ }
351
+ });
352
+
353
+ if (!response.ok) {
354
+ throw new Error('获取用户列表失败');
355
+ }
356
+
357
+ allUsers = await response.json();
358
+ applyFilters();
359
+ } catch (error) {
360
+ console.error('获取用户列表错误:', error);
361
+ toastr.error('获取用户列表失败。请重试。');
362
+ } finally {
363
+ document.getElementById('loading').style.display = 'none';
364
+ }
365
+ }
366
+
367
+ function applyFilters() {
368
+ const searchTerm = document.getElementById('searchInput').value.toLowerCase();
369
+ const roleFilter = document.getElementById('roleFilter').value;
370
+ const statusFilter = document.getElementById('statusFilter').value;
371
+
372
+ filteredUsers = allUsers.filter(user => {
373
+ const matchesSearch = user.name.toLowerCase().includes(searchTerm) ||
374
+ user.handle.toLowerCase().includes(searchTerm);
375
+ const matchesRole = !roleFilter ||
376
+ (roleFilter === 'admin' && user.admin) ||
377
+ (roleFilter === 'user' && !user.admin);
378
+ const matchesStatus = !statusFilter ||
379
+ (statusFilter === 'enabled' && user.enabled) ||
380
+ (statusFilter === 'disabled' && !user.enabled);
381
+
382
+ return matchesSearch && matchesRole && matchesStatus;
383
+ });
384
+
385
+ totalPages = Math.ceil(filteredUsers.length / PAGE_SIZE);
386
+ currentPage = Math.min(currentPage, totalPages);
387
+ if (currentPage < 1) currentPage = 1;
388
+
389
+ renderUsers();
390
+ renderPagination();
391
+ }
392
+
393
+ function renderUsers() {
394
+ const grid = document.getElementById('usersGrid');
395
+ grid.innerHTML = '';
396
+
397
+ const start = (currentPage - 1) * PAGE_SIZE;
398
+ const end = start + PAGE_SIZE;
399
+ const pageUsers = filteredUsers.slice(start, end);
400
+
401
+ pageUsers.forEach(user => {
402
+ const card = document.createElement('div');
403
+ card.className = 'user-card';
404
+ card.innerHTML = `
405
+ <div class="user-header">
406
+ <img src="${user.avatar || '../img/ai4.png'}" alt="头像" class="user-avatar">
407
+ <div class="user-info">
408
+ <h3 class="user-name">${user.name}</h3>
409
+ <div class="user-handle">${user.handle}</div>
410
+ <div>
411
+ <span class="badge ${user.admin ? 'admin' : 'user'}">${user.admin ? '管理员' : '普通用户'}</span>
412
+ <span class="badge ${user.enabled ? 'enabled' : 'disabled'}">${user.enabled ? '已启用' : '已禁用'}</span>
413
+ </div>
414
+ </div>
415
+ </div>
416
+ <div class="user-actions">
417
+ ${user.enabled ?
418
+ `<button class="action-btn" onclick="disableUser('${user.handle}')">
419
+ <i class="fa-solid fa-ban"></i> 禁用
420
+ </button>` :
421
+ `<button class="action-btn" onclick="enableUser('${user.handle}')">
422
+ <i class="fa-solid fa-check"></i> 启用
423
+ </button>`
424
+ }
425
+ ${user.admin ?
426
+ `<button class="action-btn" onclick="demoteUser('${user.handle}')">
427
+ <i class="fa-solid fa-arrow-down"></i> 降级
428
+ </button>` :
429
+ `<button class="action-btn" onclick="promoteUser('${user.handle}')">
430
+ <i class="fa-solid fa-arrow-up"></i> 提升
431
+ </button>`
432
+ }
433
+ <button class="action-btn" onclick="changePassword('${user.handle}')">
434
+ <i class="fa-solid fa-key"></i> 密码
435
+ </button>
436
+ <button class="action-btn danger" onclick="deleteUser('${user.handle}')">
437
+ <i class="fa-solid fa-trash"></i> 删除
438
+ </button>
439
+ </div>
440
+ `;
441
+ grid.appendChild(card);
442
+ });
443
+
444
+ if (pageUsers.length === 0) {
445
+ grid.innerHTML = '<div style="text-align: center; color: var(--grey70); padding: 40px;">没有找到匹配的用户</div>';
446
+ }
447
+ }
448
+
449
+ function renderPagination() {
450
+ const pagination = document.getElementById('pagination');
451
+ pagination.innerHTML = '';
452
+
453
+ if (totalPages <= 1) return;
454
+
455
+ // Previous button
456
+ const prevBtn = document.createElement('button');
457
+ prevBtn.className = 'page-btn';
458
+ prevBtn.innerHTML = '<i class="fa-solid fa-chevron-left"></i>';
459
+ prevBtn.disabled = currentPage === 1;
460
+ prevBtn.onclick = () => goToPage(currentPage - 1);
461
+ pagination.appendChild(prevBtn);
462
+
463
+ // First page
464
+ addPageButton(1);
465
+
466
+ // Dots after first page
467
+ if (currentPage > 3) {
468
+ addEllipsis();
469
+ }
470
+
471
+ // Pages around current page
472
+ for (let i = Math.max(2, currentPage - 1); i <= Math.min(totalPages - 1, currentPage + 1); i++) {
473
+ addPageButton(i);
474
+ }
475
+
476
+ // Dots before last page
477
+ if (currentPage < totalPages - 2) {
478
+ addEllipsis();
479
+ }
480
+
481
+ // Last page
482
+ if (totalPages > 1) {
483
+ addPageButton(totalPages);
484
+ }
485
+
486
+ // Next button
487
+ const nextBtn = document.createElement('button');
488
+ nextBtn.className = 'page-btn';
489
+ nextBtn.innerHTML = '<i class="fa-solid fa-chevron-right"></i>';
490
+ nextBtn.disabled = currentPage === totalPages;
491
+ nextBtn.onclick = () => goToPage(currentPage + 1);
492
+ pagination.appendChild(nextBtn);
493
+
494
+ // Page jump
495
+ const pageJump = document.createElement('div');
496
+ pageJump.className = 'page-jump';
497
+ pageJump.innerHTML = `
498
+ <span>跳转到</span>
499
+ <input type="number" min="1" max="${totalPages}" value="${currentPage}">
500
+ <button>确定</button>
501
+ `;
502
+ pagination.appendChild(pageJump);
503
+
504
+ // 添加跳转功能
505
+ const jumpInput = pageJump.querySelector('input');
506
+ const jumpButton = pageJump.querySelector('button');
507
+
508
+ jumpInput.addEventListener('keypress', (e) => {
509
+ if (e.key === 'Enter') {
510
+ const page = parseInt(jumpInput.value);
511
+ goToPage(page);
512
+ }
513
+ });
514
+
515
+ jumpButton.addEventListener('click', () => {
516
+ const page = parseInt(jumpInput.value);
517
+ goToPage(page);
518
+ });
519
+
520
+ function addPageButton(pageNum) {
521
+ const pageBtn = document.createElement('button');
522
+ pageBtn.className = `page-btn ${pageNum === currentPage ? 'active' : ''}`;
523
+ pageBtn.textContent = pageNum;
524
+ pageBtn.onclick = () => goToPage(pageNum);
525
+ pagination.appendChild(pageBtn);
526
+ }
527
+
528
+ function addEllipsis() {
529
+ const dots = document.createElement('span');
530
+ dots.textContent = '...';
531
+ dots.style.margin = '0 5px';
532
+ pagination.appendChild(dots);
533
+ }
534
+ }
535
+
536
+ function goToPage(pageNum) {
537
+ pageNum = Math.max(1, Math.min(pageNum, totalPages));
538
+ if (pageNum >= 1 && pageNum <= totalPages) {
539
+ currentPage = pageNum;
540
+ renderUsers();
541
+ renderPagination();
542
+ window.scrollTo(0, 0);
543
+ }
544
+ }
545
+
546
+ async function enableUser(handle) {
547
+ try {
548
+ const response = await fetch('/api/users-internal/enable', {
549
+ method: 'POST',
550
+ headers: {
551
+ 'Content-Type': 'application/json',
552
+ 'X-CSRF-Token': csrfToken
553
+ },
554
+ body: JSON.stringify({ handle })
555
+ });
556
+
557
+ if (!response.ok) throw new Error('启用用户失败');
558
+ await fetchUsers();
559
+ toastr.success('用户已成功启用');
560
+ } catch (error) {
561
+ console.error('启用用户错误:', error);
562
+ toastr.error('启用用户失败。请重试。');
563
+ }
564
+ }
565
+
566
+ async function disableUser(handle) {
567
+ try {
568
+ const response = await fetch('/api/users-internal/disable', {
569
+ method: 'POST',
570
+ headers: {
571
+ 'Content-Type': 'application/json',
572
+ 'X-CSRF-Token': csrfToken
573
+ },
574
+ body: JSON.stringify({ handle })
575
+ });
576
+
577
+ if (!response.ok) throw new Error('禁用用户失败');
578
+ await fetchUsers();
579
+ toastr.success('用户已成功禁用');
580
+ } catch (error) {
581
+ console.error('禁用用户错误:', error);
582
+ toastr.error('禁用用户失败。请重试。');
583
+ }
584
+ }
585
+
586
+ async function promoteUser(handle) {
587
+ try {
588
+ const response = await fetch('/api/users-internal/promote', {
589
+ method: 'POST',
590
+ headers: {
591
+ 'Content-Type': 'application/json',
592
+ 'X-CSRF-Token': csrfToken
593
+ },
594
+ body: JSON.stringify({ handle })
595
+ });
596
+
597
+ if (!response.ok) throw new Error('提升用户失败');
598
+ await fetchUsers();
599
+ toastr.success('用户已成功提升为管理员');
600
+ } catch (error) {
601
+ console.error('提升用户错误:', error);
602
+ toastr.error('提升用户失败。请重试。');
603
+ }
604
+ }
605
+
606
+ async function demoteUser(handle) {
607
+ try {
608
+ const response = await fetch('/api/users-internal/demote', {
609
+ method: 'POST',
610
+ headers: {
611
+ 'Content-Type': 'application/json',
612
+ 'X-CSRF-Token': csrfToken
613
+ },
614
+ body: JSON.stringify({ handle })
615
+ });
616
+
617
+ if (!response.ok) throw new Error('降级用户失败');
618
+ await fetchUsers();
619
+ toastr.success('用户已成功降级为普通用户');
620
+ } catch (error) {
621
+ console.error('降级用户错误:', error);
622
+ toastr.error('降级用户失败。请重试。');
623
+ }
624
+ }
625
+
626
+ async function deleteUser(handle) {
627
+ if (!confirm(`确定要删除用户 ${handle} 吗?`)) {
628
+ return;
629
+ }
630
+
631
+ try {
632
+ const response = await fetch('/api/users-internal/delete', {
633
+ method: 'POST',
634
+ headers: {
635
+ 'Content-Type': 'application/json',
636
+ 'X-CSRF-Token': csrfToken
637
+ },
638
+ body: JSON.stringify({ handle })
639
+ });
640
+
641
+ if (!response.ok) throw new Error('删除用户失败');
642
+ await fetchUsers();
643
+ toastr.success('用户已成功删除');
644
+ } catch (error) {
645
+ console.error('删除用户错误:', error);
646
+ toastr.error('删除用户失败。请重试。');
647
+ }
648
+ }
649
+
650
+ async function changePassword(handle) {
651
+ const password = prompt('请输入新密码(留空则删除密码):');
652
+ if (password === null) return; // 用户取消
653
+
654
+ try {
655
+ const response = await fetch('/api/users-internal/password', {
656
+ method: 'POST',
657
+ headers: {
658
+ 'Content-Type': 'application/json',
659
+ 'X-CSRF-Token': csrfToken
660
+ },
661
+ body: JSON.stringify({ handle, password })
662
+ });
663
+
664
+ if (!response.ok) throw new Error('修改密码失败');
665
+ toastr.success('密码已成功更新');
666
+ await fetchUsers();
667
+ } catch (error) {
668
+ console.error('修改密码错误:', error);
669
+ toastr.error('修改密码失败。请重试。');
670
+ }
671
+ }
672
+
673
+ // 事件监听器
674
+ document.getElementById('searchInput').addEventListener('input', applyFilters);
675
+ document.getElementById('roleFilter').addEventListener('change', applyFilters);
676
+ document.getElementById('statusFilter').addEventListener('change', applyFilters);
677
+
678
+ // 初始加载
679
+ getCsrfToken().then(() => fetchUsers());
680
+ </script>
681
+ </body>
682
+ </html>