123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- <!DOCTYPE html>
- <html lang="zh-CN">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>登录 - 后台管理系统</title>
- <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
- <style>
- body {
- background-color: #f8f9fa;
- }
- .login-container {
- max-width: 400px;
- margin: 100px auto;
- padding: 20px;
- background-color: white;
- border-radius: 10px;
- box-shadow: 0 0 10px rgba(0,0,0,0.1);
- }
- .login-title {
- text-align: center;
- margin-bottom: 30px;
- color: #333;
- }
- .form-control:focus {
- border-color: #80bdff;
- box-shadow: 0 0 0 0.2rem rgba(0,123,255,.25);
- }
- .btn-login {
- width: 100%;
- padding: 10px;
- font-size: 16px;
- }
- .alert {
- display: none;
- margin-top: 20px;
- }
- </style>
- </head>
- <body>
- <div class="container">
- <div class="login-container">
- <h2 class="login-title">后台管理系统</h2>
- <form id="loginForm">
- <div class="mb-3">
- <label for="username" class="form-label">用户名</label>
- <input type="text" class="form-control" id="username" name="username" required>
- </div>
- <div class="mb-3">
- <label for="password" class="form-label">密码</label>
- <input type="password" class="form-control" id="password" name="password" required>
- </div>
- <button type="submit" class="btn btn-primary btn-login">登录</button>
- <div class="alert alert-danger" role="alert" id="errorAlert"></div>
- </form>
- </div>
- </div>
- <script>
- document.getElementById('loginForm').addEventListener('submit', async (e) => {
- e.preventDefault();
-
- const username = document.getElementById('username').value;
- const password = document.getElementById('password').value;
- const errorAlert = document.getElementById('errorAlert');
-
- try {
- console.log('发送登录请求...');
- const response = await fetch('/api/users/login', {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json'
- },
- body: JSON.stringify({ username, password })
- });
- const data = await response.json();
- console.log('登录响应:', data);
- if (response.ok) {
- console.log('登录成功,保存token');
- // 保存 token 到 localStorage
- localStorage.setItem('token', data.token);
- console.log('token已保存,准备跳转');
- // 跳转到仪表板
- window.location.href = '/admin/views/dashboard.html';
- } else {
- console.log('登录失败:', data.message);
- errorAlert.textContent = data.message || '登录失败';
- errorAlert.style.display = 'block';
- }
- } catch (error) {
- console.error('登录错误:', error);
- errorAlert.textContent = '服务器错误,请稍后重试';
- errorAlert.style.display = 'block';
- }
- });
- </script>
- </body>
- </html>
|