提交文件
This commit is contained in:
156
test_frontend_connection.py
Normal file
156
test_frontend_connection.py
Normal file
@@ -0,0 +1,156 @@
|
||||
"""
|
||||
前端连接测试脚本 - 诊断前端到后端的所有连接问题
|
||||
"""
|
||||
import requests
|
||||
import sys
|
||||
|
||||
BASE_URL = "http://localhost:8000"
|
||||
API_URL = "http://localhost:8000/api/v1"
|
||||
FRONTEND_URL = "http://localhost:5173"
|
||||
|
||||
print("="*60)
|
||||
print("医院绩效管理系统 - 前端连接诊断")
|
||||
print("="*60)
|
||||
|
||||
# 1. 检查后端服务
|
||||
print("\n[1] 检查后端服务...")
|
||||
try:
|
||||
resp = requests.get(f"{BASE_URL}/health")
|
||||
if resp.status_code == 200:
|
||||
print(f" ✅ 后端服务正常:{resp.json()}")
|
||||
else:
|
||||
print(f" ❌ 后端服务异常:状态码 {resp.status_code}")
|
||||
except Exception as e:
|
||||
print(f" ❌ 后端服务未运行:{e}")
|
||||
sys.exit(1)
|
||||
|
||||
# 2. 检查前端服务
|
||||
print("\n[2] 检查前端服务...")
|
||||
try:
|
||||
resp = requests.get(FRONTEND_URL)
|
||||
if resp.status_code == 200:
|
||||
print(f" ✅ 前端服务正常")
|
||||
else:
|
||||
print(f" ❌ 前端服务异常:状态码 {resp.status_code}")
|
||||
except Exception as e:
|
||||
print(f" ❌ 前端服务未运行:{e}")
|
||||
|
||||
# 3. 检查 CORS 配置
|
||||
print("\n[3] 检查 CORS 配置...")
|
||||
try:
|
||||
resp = requests.options(
|
||||
f"{API_URL}/auth/login",
|
||||
headers={
|
||||
"Origin": "http://localhost:5173",
|
||||
"Access-Control-Request-Method": "POST"
|
||||
}
|
||||
)
|
||||
cors_headers = resp.headers.get("Access-Control-Allow-Origin", "")
|
||||
if "localhost:5173" in cors_headers or "*" in cors_headers:
|
||||
print(f" ✅ CORS 配置正确")
|
||||
else:
|
||||
print(f" ⚠️ CORS 配置可能有问题:{cors_headers}")
|
||||
except Exception as e:
|
||||
print(f" ❌ CORS 检查失败:{e}")
|
||||
|
||||
# 4. 测试登录接口
|
||||
print("\n[4] 测试登录接口...")
|
||||
try:
|
||||
resp = requests.post(
|
||||
f"{API_URL}/auth/login",
|
||||
data={"username": "admin", "password": "admin123"},
|
||||
headers={"Origin": "http://localhost:5173"}
|
||||
)
|
||||
if resp.status_code == 200:
|
||||
token = resp.json().get("access_token", "")
|
||||
print(f" ✅ 登录接口正常,Token: {token[:30]}...")
|
||||
elif resp.status_code == 401:
|
||||
print(f" ❌ 登录失败:账号或密码错误")
|
||||
else:
|
||||
print(f" ❌ 登录接口异常:状态码 {resp.status_code}")
|
||||
print(f" 响应:{resp.text[:100]}")
|
||||
except Exception as e:
|
||||
print(f" ❌ 登录接口异常:{e}")
|
||||
token = ""
|
||||
|
||||
# 5. 测试需要认证的接口
|
||||
if token:
|
||||
headers = {"Authorization": f"Bearer {token}"}
|
||||
|
||||
print("\n[5] 测试科室列表接口...")
|
||||
try:
|
||||
resp = requests.get(f"{API_URL}/departments", headers=headers)
|
||||
if resp.status_code == 200:
|
||||
data = resp.json()
|
||||
print(f" ✅ 科室列表正常:共 {len(data.get('data', []))} 个科室")
|
||||
else:
|
||||
print(f" ❌ 科室列表异常:状态码 {resp.status_code}")
|
||||
except Exception as e:
|
||||
print(f" ❌ 科室列表异常:{e}")
|
||||
|
||||
print("\n[6] 测试指标列表接口...")
|
||||
try:
|
||||
resp = requests.get(f"{API_URL}/indicators?page=1&page_size=20", headers=headers)
|
||||
if resp.status_code == 200:
|
||||
data = resp.json()
|
||||
print(f" ✅ 指标列表正常:共 {len(data.get('data', []))} 个指标")
|
||||
else:
|
||||
print(f" ❌ 指标列表异常:状态码 {resp.status_code}")
|
||||
except Exception as e:
|
||||
print(f" ❌ 指标列表异常:{e}")
|
||||
|
||||
print("\n[7] 测试 BSC 维度统计接口...")
|
||||
try:
|
||||
resp = requests.get(
|
||||
f"{API_URL}/stats/bsc-dimension?period_year=2026&period_month=2",
|
||||
headers=headers
|
||||
)
|
||||
if resp.status_code == 200:
|
||||
data = resp.json()
|
||||
dimensions = data.get("data", {}).get("dimensions", {})
|
||||
print(f" ✅ BSC 统计正常:共 {len(dimensions)} 个维度")
|
||||
else:
|
||||
print(f" ❌ BSC 统计异常:状态码 {resp.status_code}")
|
||||
print(f" 响应:{resp.text[:100]}")
|
||||
except Exception as e:
|
||||
print(f" ❌ BSC 统计异常:{e}")
|
||||
|
||||
print("\n[8] 测试科室统计接口...")
|
||||
try:
|
||||
resp = requests.get(
|
||||
f"{API_URL}/stats/department?period_year=2026&period_month=2",
|
||||
headers=headers
|
||||
)
|
||||
if resp.status_code == 200:
|
||||
data = resp.json()
|
||||
print(f" ✅ 科室统计正常:共 {len(data.get('data', []))} 个科室")
|
||||
else:
|
||||
print(f" ❌ 科室统计异常:状态码 {resp.status_code}")
|
||||
print(f" 响应:{resp.text[:100]}")
|
||||
except Exception as e:
|
||||
print(f" ❌ 科室统计异常:{e}")
|
||||
else:
|
||||
print("\n[5-8] 跳过(需要登录)")
|
||||
|
||||
# 9. 检查 API 文档
|
||||
print("\n[9] 检查 API 文档...")
|
||||
try:
|
||||
resp = requests.get(f"{API_URL}/docs")
|
||||
if resp.status_code == 200:
|
||||
print(f" ✅ API 文档可访问:{API_URL}/docs")
|
||||
else:
|
||||
print(f" ⚠️ API 文档访问异常:状态码 {resp.status_code}")
|
||||
except Exception as e:
|
||||
print(f" ❌ API 文档访问异常:{e}")
|
||||
|
||||
print("\n" + "="*60)
|
||||
print("诊断完成")
|
||||
print("="*60)
|
||||
|
||||
print("\n💡 常见问题解决:")
|
||||
print("1. 如果后端未运行:cd backend && uvicorn app.main:app --reload")
|
||||
print("2. 如果前端未运行:cd frontend && npm run dev")
|
||||
print("3. 清除浏览器缓存:Ctrl+Shift+Delete")
|
||||
print("4. 查看控制台错误:F12 → Console")
|
||||
print("5. 查看网络请求:F12 → Network")
|
||||
print("\n测试页面:http://localhost:5173/test-api.html")
|
||||
Reference in New Issue
Block a user