diff --git a/js/admin.js b/js/admin.js
index db80afd..76ddf8f 100644
--- a/js/admin.js
+++ b/js/admin.js
@@ -60,13 +60,31 @@ function renderAdminReports() {
});
}
-function renderAdminUsers() {
+let currentUsersPage = 1;
+let usersTotalPages = 1;
+let usersSearchKeyword = '';
+
+function renderAdminUsers(page = 1, search = '') {
const container = document.getElementById('adminContent');
container.innerHTML = '
加载中...
';
- API.getUsers().then(users => {
+
+ currentUsersPage = page;
+ usersSearchKeyword = search;
+
+ API.getUsers(page, 20, search).then(result => {
+ const users = result.data || [];
+ const pagination = result.pagination || { total: 0, totalPages: 1, page: 1 };
+ usersTotalPages = pagination.totalPages || 1;
+
let html = `
-
- 共
${users.length} 位用户
+
+
共 ${pagination.total} 位用户
+
+
+
+
+
@@ -80,40 +98,106 @@ function renderAdminUsers() {
`;
- users.forEach(u => {
- const isAdmin = u.role === 'admin';
- const isCurrentUser = currentUser && currentUser.id === u.id;
- html += `
-
-
-
-  + '&background=6366f1&color=fff&size=64'})
- ${u.username}
- ${isCurrentUser ? ' 你' : ''}
-
- |
-
-
- ${isAdmin ? '管理员' : '普通用户'}
-
- |
- ${u.created_at ? new Date(u.created_at).toLocaleDateString('zh-CN') : '—'} |
-
- ${isCurrentUser ?
- '不可操作自己' :
- (isAdmin ?
- `` :
- ``
- )
- }
- |
-
- `;
- });
+
+ if (users.length === 0) {
+ html += `| 暂无用户 |
`;
+ } else {
+ users.forEach(u => {
+ const isAdmin = u.role === 'admin';
+ const isCurrentUser = currentUser && currentUser.id === u.id;
+ html += `
+
+
+
+  + '&background=6366f1&color=fff&size=64'})
+ ${u.username}
+ ${isCurrentUser ? ' 你' : ''}
+
+ |
+
+
+ ${isAdmin ? '管理员' : '普通用户'}
+
+ |
+ ${u.created_at ? new Date(u.created_at).toLocaleDateString('zh-CN') : '—'} |
+
+ ${isCurrentUser ?
+ '不可操作自己' :
+ (isAdmin ?
+ `` :
+ ``
+ )
+ }
+ |
+
+ `;
+ });
+ }
+
html += '
';
+
+ // 分页控件
+ if (usersTotalPages > 1) {
+ html += `
+
+
+ `;
+
+ let startPage = Math.max(1, page - 4);
+ let endPage = Math.min(usersTotalPages, page + 4);
+
+ if (page <= 4) endPage = Math.min(usersTotalPages, 9);
+ if (page > usersTotalPages - 4) startPage = Math.max(1, usersTotalPages - 8);
+
+ if (startPage > 1) {
+ html += ``;
+ if (startPage > 2) html += `…`;
+ }
+
+ for (let i = startPage; i <= endPage; i++) {
+ const isActive = i === page;
+ html += `
+
+ `;
+ }
+
+ if (endPage < usersTotalPages) {
+ if (endPage < usersTotalPages - 1) html += `…`;
+ html += ``;
+ }
+
+ html += `
+
+
+ ${pagination.total} 位用户
+
+
+ `;
+ }
+
container.innerHTML = html;
+ // 绑定分页按钮事件
+ container.querySelectorAll('.users-page-btn:not([disabled])').forEach(btn => {
+ btn.addEventListener('click', function() {
+ const newPage = parseInt(this.dataset.page);
+ if (newPage >= 1 && newPage <= usersTotalPages) {
+ renderAdminUsers(newPage, usersSearchKeyword);
+ }
+ });
+ });
+
+ // 绑定角色切换事件
container.querySelectorAll('[data-role]').forEach(btn => {
btn.addEventListener('click', function() {
const userId = this.dataset.userid;
@@ -121,32 +205,53 @@ function renderAdminUsers() {
const roleName = role === 'admin' ? '管理员' : '普通用户';
if (!confirm(`确定要将该用户设为「${roleName}」吗?`)) return;
API.updateUserRole(userId, role).then(() => {
- renderAdminUsers();
+ renderAdminUsers(currentUsersPage, usersSearchKeyword);
}).catch(err => alert('操作失败:' + err.message));
});
});
+
+ // 搜索按钮
+ const searchBtn = document.getElementById('userSearchBtn');
+ if (searchBtn) {
+ searchBtn.addEventListener('click', function() {
+ const input = document.getElementById('userSearchInput');
+ renderAdminUsers(1, input.value.trim());
+ });
+ }
+
+ // 清空搜索
+ const clearBtn = document.getElementById('userClearSearchBtn');
+ if (clearBtn) {
+ clearBtn.addEventListener('click', function() {
+ const input = document.getElementById('userSearchInput');
+ input.value = '';
+ renderAdminUsers(1, '');
+ });
+ }
+
+ // 回车搜索
+ const searchInput = document.getElementById('userSearchInput');
+ if (searchInput) {
+ searchInput.addEventListener('keydown', function(e) {
+ if (e.key === 'Enter') {
+ renderAdminUsers(1, this.value.trim());
+ }
+ });
+ }
+
}).catch(() => {
container.innerHTML = '
加载失败
';
});
}
-// ============================================================
-// 事件日志 —— 支持分页
-// ============================================================
-
-let currentLogsPage = 1;
-let logsTotalPages = 1;
-
function renderAdminLogs(page = 1) {
const container = document.getElementById('adminContent');
container.innerHTML = '
加载中...
';
- currentLogsPage = page;
-
API.getEventLogs(page, 20).then(result => {
const logs = result.data || [];
const pagination = result.pagination || { total: 0, totalPages: 1, page: 1 };
- logsTotalPages = pagination.totalPages || 1;
+ const totalPages = pagination.totalPages || 1;
if (!logs || logs.length === 0) {
container.innerHTML = '
暂无日志
';
@@ -176,7 +281,7 @@ function renderAdminLogs(page = 1) {
html += '';
// 分页控件
- if (logsTotalPages > 1) {
+ if (totalPages > 1) {
html += `