- 完全重构前端管理界面,采用模块化架构 - 新增独立的 CSS 和 JS 模块(utils, auth, channels, tokens, pricing, api-test, database) - 实现统一的 loading 遮罩效果,所有 API 请求自动显示加载状态 - 添加现代化多选下拉框组件用于频道选择 - 实现模型部署映射的可视化表格编辑 - 修复页面高度自适应问题,管理页面占满全屏 - 优化侧边栏布局,添加 GitHub 链接和版本号 - 令牌管理页面隐藏敏感 key,添加复制功能 - 管理页面切换时自动重置为列表视图并重新加载数据 - 新增 Claude Proxy 提供商支持 - 统一所有 API 调用的错误处理和 loading 状态管理 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
81 lines
2.4 KiB
JavaScript
81 lines
2.4 KiB
JavaScript
// ===== Authentication Module =====
|
|
import { toggleModal, showNotification, showLoading, hideLoading } from './utils.js';
|
|
|
|
export function toggleAuthModal() {
|
|
const adminToken = localStorage.getItem('adminToken');
|
|
|
|
if (adminToken) {
|
|
// Logout
|
|
if (confirm('确定要退出登录吗?')) {
|
|
localStorage.removeItem('adminToken');
|
|
onLogout();
|
|
}
|
|
} else {
|
|
toggleModal('authModal');
|
|
}
|
|
}
|
|
|
|
export async function authenticate(event) {
|
|
event.preventDefault();
|
|
const token = document.getElementById('adminToken').value;
|
|
|
|
showLoading('验证令牌...');
|
|
try {
|
|
const response = await fetch('/api/admin/channel', {
|
|
headers: { 'x-admin-token': token }
|
|
});
|
|
|
|
if (response.ok) {
|
|
localStorage.setItem('adminToken', token);
|
|
toggleModal('authModal');
|
|
onAuthenticated();
|
|
showNotification('登录成功', 'success');
|
|
} else {
|
|
showAuthError('管理员令牌无效');
|
|
}
|
|
} catch (error) {
|
|
showAuthError('认证失败:' + error.message);
|
|
} finally {
|
|
hideLoading();
|
|
}
|
|
}
|
|
|
|
function showAuthError(message) {
|
|
document.getElementById('authError').classList.remove('hidden');
|
|
document.getElementById('authErrorMessage').textContent = message;
|
|
}
|
|
|
|
function onAuthenticated() {
|
|
document.querySelectorAll('.admin-only').forEach(el => {
|
|
el.classList.remove('hidden');
|
|
});
|
|
document.getElementById('loginPrompt').classList.add('hidden');
|
|
const authButton = document.getElementById('authButton');
|
|
authButton.textContent = '🚪 退出登录';
|
|
authButton.style.color = 'hsl(var(--destructive))';
|
|
authButton.style.borderColor = 'hsl(var(--destructive))';
|
|
}
|
|
|
|
function onLogout() {
|
|
document.querySelectorAll('.admin-only').forEach(el => {
|
|
el.classList.add('hidden');
|
|
});
|
|
document.getElementById('loginPrompt').classList.remove('hidden');
|
|
const authButton = document.getElementById('authButton');
|
|
authButton.textContent = '🔑 管理员登录';
|
|
authButton.style.color = '';
|
|
authButton.style.borderColor = '';
|
|
|
|
if (window.switchTab) {
|
|
window.switchTab('dashboard');
|
|
}
|
|
}
|
|
|
|
// Initialize on page load
|
|
export function initAuth() {
|
|
const adminToken = localStorage.getItem('adminToken');
|
|
if (adminToken) {
|
|
onAuthenticated();
|
|
}
|
|
}
|