test-api.html 5.95 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);
    }
    .test-item {
      margin: 15px 0;
      padding: 15px;
      border: 1px solid #ddd;
      border-radius: 8px;
    }
    .test-item.success {
      border-color: #4caf50;
      background: #f1f8e9;
    }
    .test-item.error {
      border-color: #f44336;
      background: #ffebee;
    }
    .test-item.loading {
      border-color: #2196f3;
      background: #e3f2fd;
    }
    .btn {
      background: #2196f3;
      color: white;
      border: none;
      padding: 10px 20px;
      border-radius: 5px;
      cursor: pointer;
      margin: 5px;
    }
    .btn:hover {
      background: #1976d2;
    }
    .status {
      margin-top: 10px;
      font-weight: bold;
    }
  </style>
</head>
<body>
  <div class="container">
    <h1>🌱 绿纤协同平台 - API连接测试</h1>
    
    <div class="test-item">
      <h3>当前配置信息</h3>
      <p><strong>当前环境:</strong> <span id="currentEnv">-</span></p>
      <p><strong>API地址:</strong> <span id="currentApiUrl">-</span></p>
      <p><strong>环境描述:</strong> <span id="currentDesc">-</span></p>
    </div>
    
    <div class="test-item">
      <h3>环境切换</h3>
      <button class="btn" onclick="switchEnv('development')">切换到开发环境</button>
      <button class="btn" onclick="switchEnv('production')">切换到正式环境</button>
    </div>
    
    <div class="test-item">
      <h3>登录API测试</h3>
      <button class="btn" onclick="testLoginAPI()">测试登录API</button>
      <div class="status" id="loginStatus">-</div>
    </div>
    
    <div class="test-item">
      <h3>拓客API测试</h3>
      <button class="btn" onclick="testExpansionAPI()">测试拓客API</button>
      <div class="status" id="expansionStatus">-</div>
    </div>
    
    <div class="test-item">
      <h3>连接历史</h3>
      <div id="connectionHistory"></div>
    </div>
  </div>

  <script>
    // 页面加载完成后初始化
    document.addEventListener('DOMContentLoaded', function() {
      updateConfigInfo();
    });
    
    // 更新配置信息显示
    function updateConfigInfo() {
      if (window.APP_CONFIG) {
        document.getElementById('currentEnv').textContent = window.APP_CONFIG.getEnvName();
        document.getElementById('currentApiUrl').textContent = window.APP_CONFIG.getApiBaseUrl();
        document.getElementById('currentDesc').textContent = window.APP_CONFIG.getEnvDescription();
      }
    }
    
    // 切换环境
    function switchEnv(env) {
      if (window.APP_CONFIG && window.APP_CONFIG.switchEnvironment(env)) {
        updateConfigInfo();
        addHistory(`✅ 已切换到${window.APP_CONFIG.getEnvName()}`);
      }
    }
    
    // 测试登录API
    async function testLoginAPI() {
      const statusEl = document.getElementById('loginStatus');
      statusEl.textContent = '🔄 正在测试...';
      
      try {
        const apiBaseUrl = window.APP_CONFIG ? window.APP_CONFIG.getApiBaseUrl() : '';
        if (!apiBaseUrl) {
          throw new Error('API地址未配置');
        }
        
        const response = await fetch(`${apiBaseUrl}/api/oauth/Login`, {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json'
          },
          body: JSON.stringify({
            username: 'test',
            password: 'test'
          })
        });
        
        if (response.ok) {
          statusEl.textContent = '✅ 登录API连接成功';
          addHistory(`✅ 登录API测试成功: ${apiBaseUrl}/api/oauth/Login`);
        } else {
          statusEl.textContent = `❌ 登录API响应错误: HTTP ${response.status}`;
          addHistory(`❌ 登录API测试失败: HTTP ${response.status}`);
        }
      } catch (error) {
        statusEl.textContent = `❌ 登录API连接失败: ${error.message}`;
        addHistory(`❌ 登录API测试失败: ${error.message}`);
      }
    }
    
    // 测试拓客API
    async function testExpansionAPI() {
      const statusEl = document.getElementById('expansionStatus');
      statusEl.textContent = '🔄 正在测试...';
      
      try {
        const apiBaseUrl = window.APP_CONFIG ? window.APP_CONFIG.getApiBaseUrl() : '';
        if (!apiBaseUrl) {
          throw new Error('API地址未配置');
        }
        
        const response = await fetch(`${apiBaseUrl}/api/Extend/LqTkjlb`, {
          method: 'GET'
        });
        
        if (response.ok) {
          statusEl.textContent = '✅ 拓客API连接成功';
          addHistory(`✅ 拓客API测试成功: ${apiBaseUrl}/api/Extend/LqTkjlb`);
        } else {
          statusEl.textContent = `❌ 拓客API响应错误: HTTP ${response.status}`;
          addHistory(`❌ 拓客API测试失败: HTTP ${response.status}`);
        }
      } catch (error) {
        statusEl.textContent = `❌ 拓客API连接失败: ${error.message}`;
        addHistory(`❌ 拓客API测试失败: ${error.message}`);
      }
    }
    
    // 添加连接历史
    function addHistory(message) {
      const historyEl = document.getElementById('connectionHistory');
      const timestamp = new Date().toLocaleTimeString();
      const historyItem = document.createElement('div');
      historyItem.innerHTML = `<small>[${timestamp}]</small> ${message}`;
      historyItem.style.marginBottom = '5px';
      historyEl.appendChild(historyItem);
      
      // 保持最多10条记录
      if (historyEl.children.length > 10) {
        historyEl.removeChild(historyEl.firstChild);
      }
    }
  </script>
</body>
</html>