实现登录功能

This commit is contained in:
Alex Yang
2025-11-30 11:44:26 +08:00
parent 4f0815a5f9
commit 72aa2846e5
7 changed files with 441 additions and 38 deletions

186
static/login.html Normal file
View File

@@ -0,0 +1,186 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>DNS服务器控制台 - 登录</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
background-color: #f5f7fa;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
color: #333;
}
.login-container {
background-color: white;
border-radius: 8px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
padding: 40px;
width: 100%;
max-width: 400px;
}
.login-header {
text-align: center;
margin-bottom: 30px;
}
.login-header h1 {
font-size: 24px;
color: #2c3e50;
margin-bottom: 8px;
}
.login-header p {
color: #7f8c8d;
font-size: 14px;
}
.form-group {
margin-bottom: 20px;
}
.form-group label {
display: block;
margin-bottom: 8px;
font-weight: 500;
color: #555;
}
.form-group input {
width: 100%;
padding: 12px;
border: 1px solid #e1e5e9;
border-radius: 4px;
font-size: 16px;
transition: border-color 0.3s ease;
}
.form-group input:focus {
outline: none;
border-color: #3498db;
box-shadow: 0 0 0 2px rgba(52, 152, 219, 0.1);
}
.btn {
width: 100%;
padding: 12px;
background-color: #3498db;
color: white;
border: none;
border-radius: 4px;
font-size: 16px;
font-weight: 500;
cursor: pointer;
transition: background-color 0.3s ease;
}
.btn:hover {
background-color: #2980b9;
}
.btn:active {
transform: translateY(1px);
}
.error-message {
background-color: #fee;
color: #c00;
padding: 12px;
border-radius: 4px;
margin-bottom: 20px;
text-align: center;
font-size: 14px;
display: none;
}
.loading {
opacity: 0.7;
cursor: not-allowed;
}
</style>
</head>
<body>
<div class="login-container">
<div class="login-header">
<h1>DNS服务器控制台</h1>
<p>请输入您的登录凭据</p>
</div>
<div class="error-message" id="errorMessage"></div>
<form id="loginForm">
<div class="form-group">
<label for="username">用户名</label>
<input type="text" id="username" name="username" placeholder="请输入用户名" required>
</div>
<div class="form-group">
<label for="password">密码</label>
<input type="password" id="password" name="password" placeholder="请输入密码" required>
</div>
<button type="submit" class="btn" id="loginBtn">登录</button>
</form>
</div>
<script>
document.getElementById('loginForm').addEventListener('submit', function(e) {
e.preventDefault();
const username = document.getElementById('username').value;
const password = document.getElementById('password').value;
const loginBtn = document.getElementById('loginBtn');
const errorMessage = document.getElementById('errorMessage');
// 显示加载状态
loginBtn.textContent = '登录中...';
loginBtn.classList.add('loading');
errorMessage.style.display = 'none';
// 发送登录请求
fetch('/api/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
username: username,
password: password
})
})
.then(response => {
if (!response.ok) {
throw new Error('登录失败');
}
return response.json();
})
.then(data => {
if (data.status === 'success') {
// 登录成功,重定向到主页
window.location.href = '/';
} else {
throw new Error(data.error || '登录失败');
}
})
.catch(error => {
// 显示错误信息
errorMessage.textContent = error.message;
errorMessage.style.display = 'block';
loginBtn.textContent = '登录';
loginBtn.classList.remove('loading');
});
});
</script>
</body>
</html>