login.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>登录 - 后台管理系统</title>
  7. <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
  8. <style>
  9. body {
  10. background-color: #f8f9fa;
  11. }
  12. .login-container {
  13. max-width: 400px;
  14. margin: 100px auto;
  15. padding: 20px;
  16. background-color: white;
  17. border-radius: 10px;
  18. box-shadow: 0 0 10px rgba(0,0,0,0.1);
  19. }
  20. .login-title {
  21. text-align: center;
  22. margin-bottom: 30px;
  23. color: #333;
  24. }
  25. .form-control:focus {
  26. border-color: #80bdff;
  27. box-shadow: 0 0 0 0.2rem rgba(0,123,255,.25);
  28. }
  29. .btn-login {
  30. width: 100%;
  31. padding: 10px;
  32. font-size: 16px;
  33. }
  34. .alert {
  35. display: none;
  36. margin-top: 20px;
  37. }
  38. </style>
  39. </head>
  40. <body>
  41. <div class="container">
  42. <div class="login-container">
  43. <h2 class="login-title">后台管理系统</h2>
  44. <form id="loginForm">
  45. <div class="mb-3">
  46. <label for="username" class="form-label">用户名</label>
  47. <input type="text" class="form-control" id="username" name="username" required>
  48. </div>
  49. <div class="mb-3">
  50. <label for="password" class="form-label">密码</label>
  51. <input type="password" class="form-control" id="password" name="password" required>
  52. </div>
  53. <button type="submit" class="btn btn-primary btn-login">登录</button>
  54. <div class="alert alert-danger" role="alert" id="errorAlert"></div>
  55. </form>
  56. </div>
  57. </div>
  58. <script>
  59. document.getElementById('loginForm').addEventListener('submit', async (e) => {
  60. e.preventDefault();
  61. const username = document.getElementById('username').value;
  62. const password = document.getElementById('password').value;
  63. const errorAlert = document.getElementById('errorAlert');
  64. try {
  65. console.log('发送登录请求...');
  66. const response = await fetch('/api/users/login', {
  67. method: 'POST',
  68. headers: {
  69. 'Content-Type': 'application/json'
  70. },
  71. body: JSON.stringify({ username, password })
  72. });
  73. const data = await response.json();
  74. console.log('登录响应:', data);
  75. if (response.ok) {
  76. console.log('登录成功,保存token');
  77. // 保存 token 到 localStorage
  78. localStorage.setItem('token', data.token);
  79. console.log('token已保存,准备跳转');
  80. // 跳转到仪表板
  81. window.location.href = '/admin/views/dashboard.html';
  82. } else {
  83. console.log('登录失败:', data.message);
  84. errorAlert.textContent = data.message || '登录失败';
  85. errorAlert.style.display = 'block';
  86. }
  87. } catch (error) {
  88. console.error('登录错误:', error);
  89. errorAlert.textContent = '服务器错误,请稍后重试';
  90. errorAlert.style.display = 'block';
  91. }
  92. });
  93. </script>
  94. </body>
  95. </html>