Files
hospital_performance/frontend/public/test-api.html
2026-02-28 15:05:30 +08:00

133 lines
4.0 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>API 测试</title>
<style>
body { font-family: Arial, sans-serif; padding: 20px; }
.result { background: #f5f5f5; padding: 10px; margin: 10px 0; border-radius: 4px; }
.success { background: #d4edda; }
.error { background: #f8d7da; }
button { padding: 10px 20px; margin: 5px; cursor: pointer; }
</style>
</head>
<body>
<h1>前端 API 测试</h1>
<div>
<button onclick="testHealth()">测试健康检查</button>
<button onclick="testLogin()">测试登录</button>
<button onclick="testDepartments()">测试科室列表</button>
<button onclick="testIndicators()">测试指标列表</button>
<button onclick="testStats()">测试统计</button>
<button onclick="clearResults()">清空结果</button>
</div>
<div id="results"></div>
<script>
const API_BASE = 'http://localhost:8000/api/v1';
let token = '';
function log(message, type = 'info') {
const div = document.createElement('div');
div.className = `result ${type}`;
div.textContent = message;
document.getElementById('results').appendChild(div);
}
function clearResults() {
document.getElementById('results').innerHTML = '';
}
async function testHealth() {
try {
const res = await fetch('http://localhost:8000/health');
const data = await res.json();
log(`健康检查:${res.status} - ${JSON.stringify(data)}`, 'success');
} catch (error) {
log(`健康检查失败:${error.message}`, 'error');
}
}
async function testLogin() {
try {
const params = new URLSearchParams();
params.append('username', 'admin');
params.append('password', 'admin123');
const res = await fetch(`${API_BASE}/auth/login`, {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: params
});
const data = await res.json();
if (res.status === 200) {
token = data.access_token;
log(`登录成功Token=${token.substring(0, 30)}...`, 'success');
} else {
log(`登录失败:${res.status} - ${JSON.stringify(data)}`, 'error');
}
} catch (error) {
log(`登录异常:${error.message}`, 'error');
}
}
async function testDepartments() {
if (!token) {
log('请先登录', 'error');
return;
}
try {
const res = await fetch(`${API_BASE}/departments`, {
headers: { 'Authorization': `Bearer ${token}` }
});
const data = await res.json();
log(`科室列表:${res.status} - ${data.message || ''} (共${data.data?.length || 0}个)`, 'success');
} catch (error) {
log(`科室列表失败:${error.message}`, 'error');
}
}
async function testIndicators() {
if (!token) {
log('请先登录', 'error');
return;
}
try {
const res = await fetch(`${API_BASE}/indicators?page=1&page_size=20`, {
headers: { 'Authorization': `Bearer ${token}` }
});
const data = await res.json();
log(`指标列表:${res.status} - ${data.message || ''} (共${data.data?.length || 0}个)`, 'success');
} catch (error) {
log(`指标列表失败:${error.message}`, 'error');
}
}
async function testStats() {
if (!token) {
log('请先登录', 'error');
return;
}
try {
const res = await fetch(`${API_BASE}/stats/department?period_year=2026&period_month=2`, {
headers: { 'Authorization': `Bearer ${token}` }
});
const data = await res.json();
log(`科室统计:${res.status} - ${data.message || ''} (共${data.data?.length || 0}个科室)`, 'success');
} catch (error) {
log(`科室统计失败:${error.message}`, 'error');
}
}
</script>
</body>
</html>