login-test.html 5.27 KB
<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>登录API测试</title>
  <script src="config.js"></script>
  <style>
    body {
      font-family: 'PingFang SC', 'Microsoft YaHei', Arial, sans-serif;
      margin: 20px;
      background: #f5f5f5;
    }
    .container {
      max-width: 600px;
      margin: 0 auto;
      background: white;
      padding: 20px;
      border-radius: 10px;
      box-shadow: 0 2px 10px rgba(0,0,0,0.1);
    }
    .form-group {
      margin: 15px 0;
    }
    label {
      display: block;
      margin-bottom: 5px;
      font-weight: bold;
    }
    input {
      width: 100%;
      padding: 10px;
      border: 1px solid #ddd;
      border-radius: 5px;
      font-size: 16px;
    }
    .btn {
      background: #2196f3;
      color: white;
      border: none;
      padding: 12px 24px;
      border-radius: 5px;
      cursor: pointer;
      font-size: 16px;
      margin: 5px;
    }
    .btn:hover {
      background: #1976d2;
    }
    .result {
      margin-top: 20px;
      padding: 15px;
      border-radius: 5px;
      white-space: pre-wrap;
      font-family: monospace;
    }
    .result.success {
      background: #e8f5e9;
      border: 1px solid #4caf50;
      color: #2e7d32;
    }
    .result.error {
      background: #ffebee;
      border: 1px solid #f44336;
      color: #c62828;
    }
    .result.info {
      background: #e3f2fd;
      border: 1px solid #2196f3;
      color: #1565c0;
    }
  </style>
</head>
<body>
  <div class="container">
    <h1>🔐 登录API测试页面</h1>
    
    <div class="form-group">
      <label for="username">用户名:</label>
      <input type="text" id="username" value="admin" placeholder="请输入用户名">
    </div>
    
    <div class="form-group">
      <label for="password">密码:</label>
      <input type="password" id="password" value="123456" placeholder="请输入密码">
    </div>
    
    <div class="form-group">
      <button class="btn" onclick="testLogin()">测试登录</button>
      <button class="btn" onclick="testConnection()">测试连接</button>
      <button class="btn" onclick="clearResult()">清除结果</button>
    </div>
    
    <div id="result" class="result info" style="display: none;"></div>
  </div>

  <script>
    // 测试登录
    async function testLogin() {
      const username = document.getElementById('username').value.trim();
      const password = document.getElementById('password').value.trim();
      
      if (!username || !password) {
        showResult('请输入用户名和密码', 'error');
        return;
      }
      
      try {
        const apiBaseUrl = window.APP_CONFIG ? window.APP_CONFIG.getApiBaseUrl() : '';
        if (!apiBaseUrl) {
          throw new Error('API地址未配置');
        }
        
        showResult('正在测试登录...', 'info');
        
        const formData = new URLSearchParams();
        formData.append('account', username);
        formData.append('password', password);
        
        const response = await fetch(`${apiBaseUrl}/api/oauth/Login`, {
          method: 'POST',
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Accept': 'application/json'
          },
          body: formData
        });
        
        const result = await response.json();
        
        if (response.ok) {
          showResult(`✅ 登录成功!\n\n响应数据:\n${JSON.stringify(result, null, 2)}`, 'success');
        } else {
          showResult(`❌ 登录失败 (HTTP ${response.status})\n\n错误信息:\n${JSON.stringify(result, null, 2)}`, 'error');
        }
        
      } catch (error) {
        showResult(`❌ 请求失败:\n${error.message}`, 'error');
      }
    }
    
    // 测试连接
    async function testConnection() {
      try {
        const apiBaseUrl = window.APP_CONFIG ? window.APP_CONFIG.getApiBaseUrl() : '';
        if (!apiBaseUrl) {
          throw new Error('API地址未配置');
        }
        
        showResult('正在测试连接...', 'info');
        
        const response = await fetch(`${apiBaseUrl}/api/oauth/Login`, {
          method: 'GET'
        });
        
        if (response.status === 405) {
          showResult('✅ 连接成功!API端点存在,但GET方法不被允许(这是正常的,因为登录需要POST方法)', 'success');
        } else {
          showResult(`连接状态: HTTP ${response.status}`, 'info');
        }
        
      } catch (error) {
        showResult(`❌ 连接失败:\n${error.message}`, 'error');
      }
    }
    
    // 显示结果
    function showResult(message, type) {
      const resultEl = document.getElementById('result');
      resultEl.textContent = message;
      resultEl.className = `result ${type}`;
      resultEl.style.display = 'block';
    }
    
    // 清除结果
    function clearResult() {
      document.getElementById('result').style.display = 'none';
    }
    
    // 页面加载完成后显示配置信息
    document.addEventListener('DOMContentLoaded', function() {
      if (window.APP_CONFIG) {
        const info = `当前环境: ${window.APP_CONFIG.getEnvName()}\nAPI地址: ${window.APP_CONFIG.getApiBaseUrl()}`;
        showResult(info, 'info');
      }
    });
  </script>
</body>
</html>