# 修复CORS配置脚本 Write-Host "🔧 正在修复CORS配置..." -ForegroundColor Green $filePath = "src\index.ts" $content = Get-Content $filePath -Raw # 在CORS配置后添加日志 $content = $content -replace "(await fastify\.register\(cors, \{[^}]+\}\);)", "`$1`nconsole.log('✅ CORS已配置: 允许所有HTTP方法 (GET, POST, PUT, DELETE, PATCH, OPTIONS)');" # 在multipart配置后添加日志 $content = $content -replace "(await fastify\.register\(multipart, \{[^}]+\}\);)", "`$1`nconsole.log('✅ 文件上传插件已配置: 最大文件大小 10MB');" # 修改CORS配置为完整版本 $oldCors = "await fastify.register(cors, \{\s+origin: true, // 开发环境允许所有来源\s+credentials: true,\s+\}\);" $newCors = @" await fastify.register(cors, { origin: true, credentials: true, methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS', 'HEAD'], allowedHeaders: ['Content-Type', 'Authorization', 'X-Requested-With', 'Accept', 'Origin'], exposedHeaders: ['Content-Range', 'X-Content-Range'], maxAge: 600, preflightContinue: false, }); "@ $content = $content -replace $oldCors, $newCors # 保存文件 Set-Content $filePath $content -Encoding UTF8 Write-Host "✅ CORS配置已修复!请重启后端服务。" -ForegroundColor Green