fix(frontend): use relative baseURL in production, avoid hardcoded localhost

Previously baseURL defaulted to 'http://localhost:5001' in all environments,
causing issues when deployed on servers or Docker with port mapping.

Changes:
- Use relative path (empty string) in production for same-origin deployment
- Keep localhost:5001 default for development environment
- Still respects VITE_API_BASE_URL env var if set

Fixes #93, related to #59, #57
This commit is contained in:
JasonOA888 2026-03-09 16:18:22 +08:00
parent 985f89f49a
commit 1e3451b058
1 changed files with 14 additions and 1 deletions

View File

@ -1,8 +1,21 @@
import axios from 'axios'
// 创建axios实例
// 优先级: VITE_API_BASE_URL > 生产环境相对路径 > 开发环境localhost
const getBaseURL = () => {
if (import.meta.env.VITE_API_BASE_URL) {
return import.meta.env.VITE_API_BASE_URL
}
// 生产环境使用相对路径(同源部署)
if (import.meta.env.PROD) {
return '' // 相对路径,使用当前域名
}
// 开发环境默认localhost
return 'http://localhost:5001'
}
const service = axios.create({
baseURL: import.meta.env.VITE_API_BASE_URL || 'http://localhost:5001',
baseURL: getBaseURL(),
timeout: 300000, // 5分钟超时本体生成可能需要较长时间
headers: {
'Content-Type': 'application/json'