# 后端服务测试脚本 param( [switch]$StartBackend ) Write-Host "🔍 后端服务诊断工具" -ForegroundColor Cyan Write-Host "================================================" -ForegroundColor Cyan Write-Host "" # 检查后端服务状态 Write-Host "📋 检查后端服务状态..." -ForegroundColor Yellow try { $response = Invoke-WebRequest -Uri "http://localhost:8080/health" -TimeoutSec 5 -ErrorAction Stop Write-Host "✅ 后端服务正在运行 (状态码: $($response.StatusCode))" -ForegroundColor Green } catch { Write-Host "❌ 后端服务未运行或无法访问" -ForegroundColor Red Write-Host " 错误信息: $($_.Exception.Message)" -ForegroundColor Red if ($StartBackend) { Write-Host "" Write-Host "🚀 正在启动后端服务..." -ForegroundColor Yellow Start-Process powershell -ArgumentList "-NoExit", "-Command", "cd backend; go run main.go" Write-Host "⏳ 等待后端服务启动..." -ForegroundColor Yellow Start-Sleep -Seconds 5 try { $response = Invoke-WebRequest -Uri "http://localhost:8080/health" -TimeoutSec 5 -ErrorAction Stop Write-Host "✅ 后端服务启动成功" -ForegroundColor Green } catch { Write-Host "❌ 后端服务启动失败" -ForegroundColor Red } } } Write-Host "" # 测试API路由 Write-Host "🔍 测试API路由..." -ForegroundColor Yellow $testRoutes = @( @{Path="/api/auth/roles/test"; Method="GET"; Name="角色列表(测试)"}, @{Path="/api/auth/roles"; Method="GET"; Name="角色列表(正式)"}, @{Path="/api/auth/login"; Method="POST"; Name="登录接口"}, @{Path="/api/auth/captcha"; Method="GET"; Name="验证码接口"} ) foreach ($route in $testRoutes) { try { $uri = "http://localhost:8080$($route.Path)" if ($route.Method -eq "GET") { $response = Invoke-WebRequest -Uri $uri -TimeoutSec 3 -ErrorAction Stop } else { $response = Invoke-WebRequest -Uri $uri -Method $route.Method -TimeoutSec 3 -ErrorAction Stop } $statusColor = if ($response.StatusCode -eq 200) { "Green" } else { "Yellow" } Write-Host "✅ $($route.Name): $($response.StatusCode)" -ForegroundColor $statusColor } catch { $errorMsg = if ($_.Exception.Response) { "HTTP $($_.Exception.Response.StatusCode)" } else { $_.Exception.Message } Write-Host "❌ $($route.Name): $errorMsg" -ForegroundColor Red } } Write-Host "" # 检查数据库连接 Write-Host "🗄️ 检查数据库配置..." -ForegroundColor Yellow if (Test-Path "backend/config.yaml") { Write-Host "✅ 配置文件存在" -ForegroundColor Green $config = Get-Content "backend/config.yaml" -Raw if ($config -match "database") { Write-Host "✅ 数据库配置存在" -ForegroundColor Green } else { Write-Host "⚠️ 数据库配置可能不完整" -ForegroundColor Yellow } } else { Write-Host "❌ 配置文件不存在" -ForegroundColor Red } Write-Host "" # 显示可用的测试命令 Write-Host "💡 可用的测试命令:" -ForegroundColor Cyan Write-Host " .\test-backend.ps1 -StartBackend # 启动后端服务并测试" -ForegroundColor White Write-Host " curl http://localhost:8080/api/auth/roles/test # 测试角色API" -ForegroundColor White Write-Host " curl http://localhost:8080/health # 检查服务健康状态" -ForegroundColor White Write-Host "" Write-Host "🔧 故障排除建议:" -ForegroundColor Cyan Write-Host " 1. 确保后端服务正在运行" -ForegroundColor White Write-Host " 2. 检查端口8080是否被占用" -ForegroundColor White Write-Host " 3. 检查数据库连接配置" -ForegroundColor White Write-Host " 4. 查看后端日志文件" -ForegroundColor White Write-Host " 5. 确保所有依赖已安装" -ForegroundColor White