File size: 3,347 Bytes
59bd45e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | <!DOCTYPE html>
<html>
<head>
<title>API 测试</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 50px auto;
padding: 20px;
}
.test-section {
margin: 20px 0;
padding: 15px;
border: 1px solid #ddd;
border-radius: 5px;
}
button {
padding: 10px 20px;
margin: 5px;
cursor: pointer;
}
.result {
margin-top: 10px;
padding: 10px;
background: #f5f5f5;
border-radius: 3px;
white-space: pre-wrap;
}
.success { color: green; }
.error { color: red; }
</style>
</head>
<body>
<h1>🧪 API 测试工具</h1>
<div class="test-section">
<h3>1. 健康检查</h3>
<button onclick="testHealth()">测试 /health</button>
<div id="health-result" class="result"></div>
</div>
<div class="test-section">
<h3>2. API 状态</h3>
<button onclick="testStatus()">测试 /api/status</button>
<div id="status-result" class="result"></div>
</div>
<div class="test-section">
<h3>3. 获取心情数据</h3>
<button onclick="testMoods()">测试 /api/moods</button>
<div id="moods-result" class="result"></div>
</div>
<div class="test-section">
<h3>4. 获取灵感数据</h3>
<button onclick="testInspirations()">测试 /api/inspirations</button>
<div id="inspirations-result" class="result"></div>
</div>
<div class="test-section">
<h3>5. 获取待办数据</h3>
<button onclick="testTodos()">测试 /api/todos</button>
<div id="todos-result" class="result"></div>
</div>
<script>
const baseUrl = window.location.origin;
async function testEndpoint(endpoint, resultId) {
const resultDiv = document.getElementById(resultId);
resultDiv.innerHTML = '测试中...';
try {
const response = await fetch(`${baseUrl}${endpoint}`);
const data = await response.json();
if (response.ok) {
resultDiv.innerHTML = `<span class="success">✅ 成功 (${response.status})</span>\n${JSON.stringify(data, null, 2)}`;
} else {
resultDiv.innerHTML = `<span class="error">❌ 失败 (${response.status})</span>\n${JSON.stringify(data, null, 2)}`;
}
} catch (error) {
resultDiv.innerHTML = `<span class="error">❌ 错误</span>\n${error.message}`;
}
}
function testHealth() {
testEndpoint('/health', 'health-result');
}
function testStatus() {
testEndpoint('/api/status', 'status-result');
}
function testMoods() {
testEndpoint('/api/moods', 'moods-result');
}
function testInspirations() {
testEndpoint('/api/inspirations', 'inspirations-result');
}
function testTodos() {
testEndpoint('/api/todos', 'todos-result');
}
</script>
</body>
</html>
|