| <!DOCTYPE html> |
| <html lang="zh"> |
| <head> |
| <meta charset="UTF-8"> |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| <title>管理员面板</title> |
| <link rel="stylesheet" href="../css/main.css"> |
| <link rel="stylesheet" href="../css/loader.css"> |
| <link rel="stylesheet" href="../css/bg_override.css"> |
| <link rel="stylesheet" href="../css/fa.min.css"> |
| <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.css"> |
| <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.js"></script> |
| <style> |
| .admin-container { |
| max-width: 1200px; |
| margin: 20px auto; |
| padding: 30px; |
| background-color: var(--black80a); |
| border-radius: 15px; |
| box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2); |
| } |
| .users-grid { |
| display: grid; |
| grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); |
| gap: 25px; |
| margin-top: 25px; |
| } |
| .user-card { |
| background-color: var(--black60a); |
| border-radius: 15px; |
| padding: 25px; |
| transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1); |
| box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); |
| border: 1px solid rgba(255, 255, 255, 0.1); |
| backdrop-filter: blur(10px); |
| } |
| .user-card:hover { |
| background-color: var(--black90a); |
| transform: translateY(-5px); |
| box-shadow: 0 8px 15px rgba(0, 0, 0, 0.2); |
| border-color: rgba(255, 255, 255, 0.2); |
| } |
| .user-header { |
| display: flex; |
| align-items: center; |
| gap: 20px; |
| margin-bottom: 20px; |
| } |
| .user-avatar { |
| width: 70px; |
| height: 70px; |
| border-radius: 50%; |
| object-fit: cover; |
| border: 3px solid var(--accent); |
| box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2); |
| transition: all 0.3s ease; |
| } |
| .user-card:hover .user-avatar { |
| transform: scale(1.05); |
| border-color: #e74c3c; |
| } |
| .user-info { |
| flex-grow: 1; |
| } |
| .user-name { |
| font-size: 1.3em; |
| font-weight: bold; |
| margin: 0 0 8px 0; |
| color: var(--white95); |
| } |
| .user-handle { |
| color: var(--grey70); |
| font-size: 0.95em; |
| margin-bottom: 12px; |
| opacity: 0.8; |
| } |
| .badge { |
| display: inline-block; |
| padding: 6px 12px; |
| border-radius: 20px; |
| font-size: 0.85em; |
| margin-right: 8px; |
| font-weight: 500; |
| letter-spacing: 0.3px; |
| text-transform: uppercase; |
| box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); |
| } |
| .badge.admin { |
| background: linear-gradient(135deg, #e74c3c 0%, #c0392b 100%); |
| color: white; |
| min-width: 70px; |
| text-align: center; |
| } |
| .badge.user { |
| background: linear-gradient(135deg, var(--black60a) 0%, var(--black90a) 100%); |
| color: var(--grey70); |
| min-width: 70px; |
| text-align: center; |
| border: 1px solid rgba(255, 255, 255, 0.1); |
| } |
| .badge.enabled { |
| background: linear-gradient(135deg, #2ecc71 0%, #27ae60 100%); |
| color: white; |
| } |
| .badge.disabled { |
| background: linear-gradient(135deg, #e74c3c 0%, #c0392b 100%); |
| color: white; |
| } |
| .user-actions { |
| display: flex; |
| gap: 10px; |
| flex-wrap: wrap; |
| margin-top: 15px; |
| padding-top: 15px; |
| border-top: 1px solid rgba(255, 255, 255, 0.1); |
| } |
| .action-btn { |
| padding: 10px 15px; |
| border-radius: 8px; |
| border: none; |
| cursor: pointer; |
| font-size: 0.9em; |
| display: flex; |
| align-items: center; |
| gap: 8px; |
| background-color: var(--black60a); |
| color: var(--white90); |
| transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1); |
| border: 1px solid rgba(255, 255, 255, 0.1); |
| flex: 1; |
| justify-content: center; |
| min-width: 100px; |
| } |
| .action-btn:hover { |
| background-color: var(--black90a); |
| transform: translateY(-2px); |
| box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15); |
| border-color: rgba(255, 255, 255, 0.2); |
| } |
| .action-btn.danger { |
| background: linear-gradient(135deg, var(--warning50a) 0%, var(--warning70a) 100%); |
| } |
| .action-btn.danger:hover { |
| background: linear-gradient(135deg, var(--warning70a) 0%, var(--warning90a) 100%); |
| } |
| .pagination { |
| display: flex; |
| justify-content: center; |
| align-items: center; |
| gap: 12px; |
| margin-top: 40px; |
| flex-wrap: wrap; |
| } |
| .page-btn { |
| padding: 10px 18px; |
| border-radius: 8px; |
| border: none; |
| cursor: pointer; |
| background-color: var(--black60a); |
| color: var(--white90); |
| transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1); |
| border: 1px solid rgba(255, 255, 255, 0.1); |
| font-weight: 500; |
| } |
| .page-btn:hover:not(.active):not(:disabled) { |
| background-color: var(--black90a); |
| transform: translateY(-2px); |
| box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15); |
| border-color: rgba(255, 255, 255, 0.2); |
| } |
| .page-btn.active { |
| background: linear-gradient(135deg, var(--accent) 0%, var(--accent-dark) 100%); |
| font-weight: bold; |
| box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); |
| } |
| .page-btn:disabled { |
| opacity: 0.5; |
| cursor: not-allowed; |
| } |
| .filters { |
| display: flex; |
| gap: 20px; |
| margin-bottom: 30px; |
| flex-wrap: wrap; |
| background-color: var(--black60a); |
| padding: 20px; |
| border-radius: 12px; |
| box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); |
| border: 1px solid rgba(255, 255, 255, 0.1); |
| } |
| .search-box { |
| flex-grow: 1; |
| padding: 12px 20px; |
| border-radius: 8px; |
| border: 1px solid rgba(255, 255, 255, 0.1); |
| background-color: var(--black60a); |
| color: var(--white90); |
| font-size: 1em; |
| transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1); |
| } |
| .search-box:focus { |
| outline: none; |
| border-color: var(--accent); |
| background-color: var(--black90a); |
| box-shadow: 0 0 0 2px rgba(231, 76, 60, 0.2); |
| } |
| .filter-select { |
| padding: 12px 20px; |
| border-radius: 8px; |
| border: 1px solid rgba(255, 255, 255, 0.1); |
| background-color: var(--black60a); |
| color: var(--white90); |
| font-size: 1em; |
| cursor: pointer; |
| transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1); |
| min-width: 150px; |
| } |
| .filter-select:focus { |
| outline: none; |
| border-color: var(--accent); |
| background-color: var(--black90a); |
| box-shadow: 0 0 0 2px rgba(231, 76, 60, 0.2); |
| } |
| #loading { |
| display: none; |
| text-align: center; |
| margin: 40px 0; |
| font-size: 1.2em; |
| color: var(--grey70); |
| } |
| .page-title { |
| color: var(--white95); |
| margin-bottom: 40px; |
| font-size: 2.2em; |
| text-align: center; |
| text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2); |
| font-weight: 700; |
| letter-spacing: 0.5px; |
| } |
| .page-jump { |
| display: flex; |
| align-items: center; |
| gap: 12px; |
| margin-left: 15px; |
| background-color: var(--black60a); |
| padding: 8px 15px; |
| border-radius: 8px; |
| border: 1px solid rgba(255, 255, 255, 0.1); |
| } |
| .page-jump input { |
| width: 70px; |
| padding: 8px 12px; |
| border-radius: 6px; |
| border: 1px solid rgba(255, 255, 255, 0.1); |
| background-color: var(--black60a); |
| color: var(--white90); |
| text-align: center; |
| font-size: 0.95em; |
| } |
| .page-jump input:focus { |
| outline: none; |
| border-color: var(--accent); |
| box-shadow: 0 0 0 2px rgba(231, 76, 60, 0.2); |
| } |
| .page-jump button { |
| padding: 8px 15px; |
| border-radius: 6px; |
| border: none; |
| background: linear-gradient(135deg, var(--accent) 0%, var(--accent-dark) 100%); |
| color: white; |
| cursor: pointer; |
| transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1); |
| font-weight: 500; |
| } |
| .page-jump button:hover { |
| transform: translateY(-2px); |
| box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15); |
| } |
| </style> |
| </head> |
| <body> |
| <div class="admin-container"> |
| <h1 class="page-title">管理员面板</h1> |
| <div class="filters"> |
| <input type="text" class="search-box" id="searchInput" placeholder="搜索用户..."> |
| <select class="filter-select" id="roleFilter"> |
| <option value="">所有角色</option> |
| <option value="admin">管理员</option> |
| <option value="user">普通用户</option> |
| </select> |
| <select class="filter-select" id="statusFilter"> |
| <option value="">所有状态</option> |
| <option value="enabled">已启用</option> |
| <option value="disabled">已禁用</option> |
| </select> |
| </div> |
| <div class="users-grid" id="usersGrid"></div> |
| <div id="loading">加载中...</div> |
| <div class="pagination" id="pagination"></div> |
| </div> |
|
|
| <script> |
| |
| toastr.options = { |
| "closeButton": true, |
| "debug": false, |
| "newestOnTop": true, |
| "progressBar": true, |
| "positionClass": "toast-top-right", |
| "preventDuplicates": false, |
| "onclick": null, |
| "showDuration": "300", |
| "hideDuration": "1000", |
| "timeOut": "5000", |
| "extendedTimeOut": "1000", |
| "showEasing": "swing", |
| "hideEasing": "linear", |
| "showMethod": "fadeIn", |
| "hideMethod": "fadeOut" |
| }; |
| |
| const PAGE_SIZE = 12; |
| let currentPage = 1; |
| let totalPages = 1; |
| let allUsers = []; |
| let filteredUsers = []; |
| let csrfToken = ''; |
| |
| async function getCsrfToken() { |
| try { |
| const response = await fetch('/csrf-token'); |
| if (!response.ok) throw new Error('获取CSRF令牌失败'); |
| const data = await response.json(); |
| csrfToken = data.token; |
| } catch (error) { |
| console.error('获取CSRF令牌错误:', error); |
| toastr.error('获取CSRF令牌失败。请刷新页面重试。'); |
| } |
| } |
| |
| async function fetchUsers() { |
| try { |
| document.getElementById('loading').style.display = 'block'; |
| const response = await fetch('/api/users-internal/list', { |
| method: 'GET', |
| headers: { |
| 'Content-Type': 'application/json', |
| 'X-CSRF-Token': csrfToken |
| } |
| }); |
| |
| if (!response.ok) { |
| throw new Error('获取用户列表失败'); |
| } |
| |
| allUsers = await response.json(); |
| applyFilters(); |
| } catch (error) { |
| console.error('获取用户列表错误:', error); |
| toastr.error('获取用户列表失败。请重试。'); |
| } finally { |
| document.getElementById('loading').style.display = 'none'; |
| } |
| } |
| |
| function applyFilters() { |
| const searchTerm = document.getElementById('searchInput').value.toLowerCase(); |
| const roleFilter = document.getElementById('roleFilter').value; |
| const statusFilter = document.getElementById('statusFilter').value; |
| |
| filteredUsers = allUsers.filter(user => { |
| const matchesSearch = user.name.toLowerCase().includes(searchTerm) || |
| user.handle.toLowerCase().includes(searchTerm); |
| const matchesRole = !roleFilter || |
| (roleFilter === 'admin' && user.admin) || |
| (roleFilter === 'user' && !user.admin); |
| const matchesStatus = !statusFilter || |
| (statusFilter === 'enabled' && user.enabled) || |
| (statusFilter === 'disabled' && !user.enabled); |
| |
| return matchesSearch && matchesRole && matchesStatus; |
| }); |
| |
| totalPages = Math.ceil(filteredUsers.length / PAGE_SIZE); |
| currentPage = Math.min(currentPage, totalPages); |
| if (currentPage < 1) currentPage = 1; |
| |
| renderUsers(); |
| renderPagination(); |
| } |
| |
| function renderUsers() { |
| const grid = document.getElementById('usersGrid'); |
| grid.innerHTML = ''; |
| |
| const start = (currentPage - 1) * PAGE_SIZE; |
| const end = start + PAGE_SIZE; |
| const pageUsers = filteredUsers.slice(start, end); |
| |
| pageUsers.forEach(user => { |
| const card = document.createElement('div'); |
| card.className = 'user-card'; |
| card.innerHTML = ` |
| <div class="user-header"> |
| <img src="${user.avatar || '../img/ai4.png'}" alt="头像" class="user-avatar"> |
| <div class="user-info"> |
| <h3 class="user-name">${user.name}</h3> |
| <div class="user-handle">${user.handle}</div> |
| <div> |
| <span class="badge ${user.admin ? 'admin' : 'user'}">${user.admin ? '管理员' : '普通用户'}</span> |
| <span class="badge ${user.enabled ? 'enabled' : 'disabled'}">${user.enabled ? '已启用' : '已禁用'}</span> |
| </div> |
| </div> |
| </div> |
| <div class="user-actions"> |
| ${user.enabled ? |
| `<button class="action-btn" onclick="disableUser('${user.handle}')"> |
| <i class="fa-solid fa-ban"></i> 禁用 |
| </button>` : |
| `<button class="action-btn" onclick="enableUser('${user.handle}')"> |
| <i class="fa-solid fa-check"></i> 启用 |
| </button>` |
| } |
| ${user.admin ? |
| `<button class="action-btn" onclick="demoteUser('${user.handle}')"> |
| <i class="fa-solid fa-arrow-down"></i> 降级 |
| </button>` : |
| `<button class="action-btn" onclick="promoteUser('${user.handle}')"> |
| <i class="fa-solid fa-arrow-up"></i> 提升 |
| </button>` |
| } |
| <button class="action-btn" onclick="changePassword('${user.handle}')"> |
| <i class="fa-solid fa-key"></i> 密码 |
| </button> |
| <button class="action-btn danger" onclick="deleteUser('${user.handle}')"> |
| <i class="fa-solid fa-trash"></i> 删除 |
| </button> |
| </div> |
| `; |
| grid.appendChild(card); |
| }); |
| |
| if (pageUsers.length === 0) { |
| grid.innerHTML = '<div style="text-align: center; color: var(--grey70); padding: 40px;">没有找到匹配的用户</div>'; |
| } |
| } |
| |
| function renderPagination() { |
| const pagination = document.getElementById('pagination'); |
| pagination.innerHTML = ''; |
| |
| if (totalPages <= 1) return; |
| |
| |
| const prevBtn = document.createElement('button'); |
| prevBtn.className = 'page-btn'; |
| prevBtn.innerHTML = '<i class="fa-solid fa-chevron-left"></i>'; |
| prevBtn.disabled = currentPage === 1; |
| prevBtn.onclick = () => goToPage(currentPage - 1); |
| pagination.appendChild(prevBtn); |
| |
| |
| addPageButton(1); |
| |
| |
| if (currentPage > 3) { |
| addEllipsis(); |
| } |
| |
| |
| for (let i = Math.max(2, currentPage - 1); i <= Math.min(totalPages - 1, currentPage + 1); i++) { |
| addPageButton(i); |
| } |
| |
| |
| if (currentPage < totalPages - 2) { |
| addEllipsis(); |
| } |
| |
| |
| if (totalPages > 1) { |
| addPageButton(totalPages); |
| } |
| |
| |
| const nextBtn = document.createElement('button'); |
| nextBtn.className = 'page-btn'; |
| nextBtn.innerHTML = '<i class="fa-solid fa-chevron-right"></i>'; |
| nextBtn.disabled = currentPage === totalPages; |
| nextBtn.onclick = () => goToPage(currentPage + 1); |
| pagination.appendChild(nextBtn); |
| |
| |
| const pageJump = document.createElement('div'); |
| pageJump.className = 'page-jump'; |
| pageJump.innerHTML = ` |
| <span>跳转到</span> |
| <input type="number" min="1" max="${totalPages}" value="${currentPage}"> |
| <button>确定</button> |
| `; |
| pagination.appendChild(pageJump); |
| |
| |
| const jumpInput = pageJump.querySelector('input'); |
| const jumpButton = pageJump.querySelector('button'); |
| |
| jumpInput.addEventListener('keypress', (e) => { |
| if (e.key === 'Enter') { |
| const page = parseInt(jumpInput.value); |
| goToPage(page); |
| } |
| }); |
| |
| jumpButton.addEventListener('click', () => { |
| const page = parseInt(jumpInput.value); |
| goToPage(page); |
| }); |
| |
| function addPageButton(pageNum) { |
| const pageBtn = document.createElement('button'); |
| pageBtn.className = `page-btn ${pageNum === currentPage ? 'active' : ''}`; |
| pageBtn.textContent = pageNum; |
| pageBtn.onclick = () => goToPage(pageNum); |
| pagination.appendChild(pageBtn); |
| } |
| |
| function addEllipsis() { |
| const dots = document.createElement('span'); |
| dots.textContent = '...'; |
| dots.style.margin = '0 5px'; |
| pagination.appendChild(dots); |
| } |
| } |
| |
| function goToPage(pageNum) { |
| pageNum = Math.max(1, Math.min(pageNum, totalPages)); |
| if (pageNum >= 1 && pageNum <= totalPages) { |
| currentPage = pageNum; |
| renderUsers(); |
| renderPagination(); |
| window.scrollTo(0, 0); |
| } |
| } |
| |
| async function enableUser(handle) { |
| try { |
| const response = await fetch('/api/users-internal/enable', { |
| method: 'POST', |
| headers: { |
| 'Content-Type': 'application/json', |
| 'X-CSRF-Token': csrfToken |
| }, |
| body: JSON.stringify({ handle }) |
| }); |
| |
| if (!response.ok) throw new Error('启用用户失败'); |
| await fetchUsers(); |
| toastr.success('用户已成功启用'); |
| } catch (error) { |
| console.error('启用用户错误:', error); |
| toastr.error('启用用户失败。请重试。'); |
| } |
| } |
| |
| async function disableUser(handle) { |
| try { |
| const response = await fetch('/api/users-internal/disable', { |
| method: 'POST', |
| headers: { |
| 'Content-Type': 'application/json', |
| 'X-CSRF-Token': csrfToken |
| }, |
| body: JSON.stringify({ handle }) |
| }); |
| |
| if (!response.ok) throw new Error('禁用用户失败'); |
| await fetchUsers(); |
| toastr.success('用户已成功禁用'); |
| } catch (error) { |
| console.error('禁用用户错误:', error); |
| toastr.error('禁用用户失败。请重试。'); |
| } |
| } |
| |
| async function promoteUser(handle) { |
| try { |
| const response = await fetch('/api/users-internal/promote', { |
| method: 'POST', |
| headers: { |
| 'Content-Type': 'application/json', |
| 'X-CSRF-Token': csrfToken |
| }, |
| body: JSON.stringify({ handle }) |
| }); |
| |
| if (!response.ok) throw new Error('提升用户失败'); |
| await fetchUsers(); |
| toastr.success('用户已成功提升为管理员'); |
| } catch (error) { |
| console.error('提升用户错误:', error); |
| toastr.error('提升用户失败。请重试。'); |
| } |
| } |
| |
| async function demoteUser(handle) { |
| try { |
| const response = await fetch('/api/users-internal/demote', { |
| method: 'POST', |
| headers: { |
| 'Content-Type': 'application/json', |
| 'X-CSRF-Token': csrfToken |
| }, |
| body: JSON.stringify({ handle }) |
| }); |
| |
| if (!response.ok) throw new Error('降级用户失败'); |
| await fetchUsers(); |
| toastr.success('用户已成功降级为普通用户'); |
| } catch (error) { |
| console.error('降级用户错误:', error); |
| toastr.error('降级用户失败。请重试。'); |
| } |
| } |
| |
| async function deleteUser(handle) { |
| if (!confirm(`确定要删除用户 ${handle} 吗?`)) { |
| return; |
| } |
| |
| try { |
| const response = await fetch('/api/users-internal/delete', { |
| method: 'POST', |
| headers: { |
| 'Content-Type': 'application/json', |
| 'X-CSRF-Token': csrfToken |
| }, |
| body: JSON.stringify({ handle }) |
| }); |
| |
| if (!response.ok) throw new Error('删除用户失败'); |
| await fetchUsers(); |
| toastr.success('用户已成功删除'); |
| } catch (error) { |
| console.error('删除用户错误:', error); |
| toastr.error('删除用户失败。请重试。'); |
| } |
| } |
| |
| async function changePassword(handle) { |
| const password = prompt('请输入新密码(留空则删除密码):'); |
| if (password === null) return; |
| |
| try { |
| const response = await fetch('/api/users-internal/password', { |
| method: 'POST', |
| headers: { |
| 'Content-Type': 'application/json', |
| 'X-CSRF-Token': csrfToken |
| }, |
| body: JSON.stringify({ handle, password }) |
| }); |
| |
| if (!response.ok) throw new Error('修改密码失败'); |
| toastr.success('密码已成功更新'); |
| await fetchUsers(); |
| } catch (error) { |
| console.error('修改密码错误:', error); |
| toastr.error('修改密码失败。请重试。'); |
| } |
| } |
| |
| |
| document.getElementById('searchInput').addEventListener('input', applyFilters); |
| document.getElementById('roleFilter').addEventListener('change', applyFilters); |
| document.getElementById('statusFilter').addEventListener('change', applyFilters); |
| |
| |
| getCsrfToken().then(() => fetchUsers()); |
| </script> |
| </body> |
| </html> |
|
|