登录页面
This commit is contained in:
+246
@@ -0,0 +1,246 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>登录 - Lab</title>
|
||||
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.min.css">
|
||||
<style>
|
||||
body {
|
||||
background-color: #f8f9fa;
|
||||
color: #212529;
|
||||
line-height: 1.6;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
min-height: 100vh;
|
||||
margin: 0;
|
||||
padding: 20px;
|
||||
}
|
||||
|
||||
.card {
|
||||
border: none;
|
||||
border-radius: 12px;
|
||||
box-shadow: 0 2px 12px rgba(0,0,0,0.08);
|
||||
transition: transform 0.2s, box-shadow 0.2s;
|
||||
margin-bottom: 24px;
|
||||
width: 100%;
|
||||
max-width: 400px;
|
||||
}
|
||||
|
||||
.card-title {
|
||||
font-size: 1.25rem;
|
||||
font-weight: 600;
|
||||
margin-bottom: 0.75rem;
|
||||
color: #2c3e50;
|
||||
}
|
||||
|
||||
.card-body {
|
||||
padding: 24px;
|
||||
}
|
||||
|
||||
.btn {
|
||||
border-radius: 8px;
|
||||
padding: 8px 16px;
|
||||
font-weight: 500;
|
||||
transition: all 0.2s;
|
||||
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
||||
}
|
||||
|
||||
.btn-primary {
|
||||
background: linear-gradient(45deg, #6366F1, #818CF8);
|
||||
border: none;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.btn-primary:hover {
|
||||
background: linear-gradient(45deg, #818CF8, #6366F1);
|
||||
transform: translateY(-1px);
|
||||
box-shadow: 0 4px 12px rgba(99, 102, 241, 0.2);
|
||||
}
|
||||
|
||||
.btn-link {
|
||||
color: #6366F1;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.btn-link:hover {
|
||||
color: #818CF8;
|
||||
text-decoration: underline;
|
||||
}
|
||||
|
||||
.form-control {
|
||||
border-radius: 8px;
|
||||
border: 1px solid #dee2e6;
|
||||
padding: 10px 16px;
|
||||
transition: all 0.2s;
|
||||
}
|
||||
|
||||
.form-control:focus {
|
||||
border-color: #6366F1;
|
||||
box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.1);
|
||||
}
|
||||
|
||||
.hidden {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.form-label {
|
||||
color: #4a5568;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
h3 {
|
||||
color: #2d3748;
|
||||
margin-bottom: 1.5rem;
|
||||
}
|
||||
|
||||
.alert {
|
||||
border-radius: 8px;
|
||||
padding: 1rem;
|
||||
margin-bottom: 1rem;
|
||||
border: none;
|
||||
}
|
||||
|
||||
.alert-danger {
|
||||
background-color: #FEE2E2;
|
||||
color: #991B1B;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<!-- 登录表单 -->
|
||||
<div id="loginForm" class="card">
|
||||
<div class="card-body">
|
||||
<h3>登录</h3>
|
||||
<form onsubmit="login(event)">
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Username</label>
|
||||
<input type="text" class="form-control" id="loginUsername" required>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Password</label>
|
||||
<input type="password" class="form-control" id="loginPassword" required>
|
||||
</div>
|
||||
<button type="submit" class="btn btn-primary w-100 mb-3">Login</button>
|
||||
<button type="button" class="btn btn-link w-100" onclick="showRegisterForm()">Register</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 注册表单 -->
|
||||
<div id="registerForm" class="card hidden">
|
||||
<div class="card-body">
|
||||
<h3>Register</h3>
|
||||
<form onsubmit="register(event)">
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Username</label>
|
||||
<input type="text" class="form-control" id="regUsername" required>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Password</label>
|
||||
<input type="password" class="form-control" id="regPassword" required>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Email</label>
|
||||
<input type="email" class="form-control" id="regEmail" required>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Name</label>
|
||||
<input type="text" class="form-control" id="regName" required>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Institution</label>
|
||||
<input type="text" class="form-control" id="regInstitution" required>
|
||||
</div>
|
||||
<button type="submit" class="btn btn-primary w-100 mb-3">Register</button>
|
||||
<button type="button" class="btn btn-link w-100" onclick="showLoginForm()">Back to Login</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
|
||||
<script>
|
||||
// API base URL
|
||||
const API_BASE_URL = 'https://dev.obscura.work/lab';
|
||||
|
||||
// 显示/隐藏表单函数
|
||||
function showLoginForm() {
|
||||
document.getElementById('loginForm').classList.remove('hidden');
|
||||
document.getElementById('registerForm').classList.add('hidden');
|
||||
}
|
||||
|
||||
function showRegisterForm() {
|
||||
document.getElementById('loginForm').classList.add('hidden');
|
||||
document.getElementById('registerForm').classList.remove('hidden');
|
||||
}
|
||||
|
||||
// 用户注册
|
||||
async function register(event) {
|
||||
event.preventDefault();
|
||||
const userData = {
|
||||
username: document.getElementById('regUsername').value,
|
||||
password: document.getElementById('regPassword').value,
|
||||
email: document.getElementById('regEmail').value,
|
||||
name: document.getElementById('regName').value,
|
||||
institution: document.getElementById('regInstitution').value
|
||||
};
|
||||
|
||||
try {
|
||||
const response = await fetch(`${API_BASE_URL}/register`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(userData)
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
alert('Register success!');
|
||||
showLoginForm();
|
||||
} else {
|
||||
const error = await response.json();
|
||||
alert(`Register failed: ${error.detail}`);
|
||||
}
|
||||
} catch (error) {
|
||||
alert('Register failed: ' + error.message);
|
||||
}
|
||||
}
|
||||
|
||||
// 用户登录
|
||||
async function login(event) {
|
||||
event.preventDefault();
|
||||
const formData = new FormData();
|
||||
formData.append('username', document.getElementById('loginUsername').value);
|
||||
formData.append('password', document.getElementById('loginPassword').value);
|
||||
|
||||
try {
|
||||
const response = await fetch(`${API_BASE_URL}/token`, {
|
||||
method: 'POST',
|
||||
body: formData
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
const data = await response.json();
|
||||
localStorage.setItem('token', data.access_token);
|
||||
window.location.href = 'lab.html';
|
||||
} else {
|
||||
const error = await response.json();
|
||||
alert(`Login failed: ${error.detail}`);
|
||||
}
|
||||
} catch (error) {
|
||||
alert('Login failed: ' + error.message);
|
||||
}
|
||||
}
|
||||
|
||||
// 检查登录状态
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
const token = localStorage.getItem('token');
|
||||
if (token) {
|
||||
window.location.href = 'lab.html';
|
||||
}
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user