更新API配置,使用相对路径以简化前端与后端的交互。

This commit is contained in:
wangqifan 2025-09-02 14:37:29 +08:00
parent d9c7da8ef9
commit e634d68b53
6 changed files with 27 additions and 10 deletions

2
.env
View File

@ -1,2 +1,2 @@
# API配置 # API配置
VUE_APP_API_BASE_URL=http://localhost:8000 VUE_APP_API_BASE_URL=/api/

View File

@ -1,2 +1,2 @@
# 生产环境API配置 # 生产环境API配置
VUE_APP_API_BASE_URL=http://calc.buddyscloud.com:8000 VUE_APP_API_BASE_URL=/api

Binary file not shown.

View File

@ -41,7 +41,7 @@ const apiService = {
// 获取区域列表 // 获取区域列表
getRegions: async () => { getRegions: async () => {
try { try {
const response = await apiClient.get('/api/regions') const response = await apiClient.get('/regions')
return response.data return response.data
} catch (error) { } catch (error) {
console.error('获取区域列表失败:', error) console.error('获取区域列表失败:', error)
@ -52,7 +52,7 @@ const apiService = {
// 获取实例类型列表 // 获取实例类型列表
getInstanceTypes: async () => { getInstanceTypes: async () => {
try { try {
const response = await apiClient.get('/api/instance-types') const response = await apiClient.get('/instance-types')
return response.data return response.data
} catch (error) { } catch (error) {
console.error('获取实例类型列表失败:', error) console.error('获取实例类型列表失败:', error)
@ -63,7 +63,7 @@ const apiService = {
// 搜索实例 // 搜索实例
searchInstances: async (params) => { searchInstances: async (params) => {
try { try {
const response = await apiClient.post('/api/search-instances-v2', params) const response = await apiClient.post('/search-instances-v2', params)
return response.data return response.data
} catch (error) { } catch (error) {
console.error('搜索实例失败:', error) console.error('搜索实例失败:', error)
@ -74,7 +74,7 @@ const apiService = {
// 计算价格 // 计算价格
calculatePrice: async (params) => { calculatePrice: async (params) => {
try { try {
const response = await apiClient.post('/api/calculate-price', params) const response = await apiClient.post('/calculate-price', params)
return response.data return response.data
} catch (error) { } catch (error) {
console.error('计算价格失败:', error) console.error('计算价格失败:', error)
@ -85,7 +85,7 @@ const apiService = {
// 比较价格 // 比较价格
comparePrices: async (params) => { comparePrices: async (params) => {
try { try {
const response = await apiClient.post('/api/compare-prices', params) const response = await apiClient.post('/compare-prices', params)
return response.data return response.data
} catch (error) { } catch (error) {
console.error('比较价格失败:', error) console.error('比较价格失败:', error)
@ -96,7 +96,7 @@ const apiService = {
// 获取预算估算 // 获取预算估算
getBudgetEstimate: async (params) => { getBudgetEstimate: async (params) => {
try { try {
const response = await apiClient.post('/api/budget', params) const response = await apiClient.post('/budget', params)
return response.data return response.data
} catch (error) { } catch (error) {
console.error('获取预算估算失败:', error) console.error('获取预算估算失败:', error)

View File

@ -1,7 +1,8 @@
// API 配置文件 // API 配置文件
const config = { const config = {
// 后端API基础URL - 从环境变量中读取,如果不存在则使用默认值 // 后端API基础URL 使用相对路径,通过 devServer 代理到后端
apiBaseUrl: process.env.VUE_APP_API_BASE_URL || 'http://localhost:8000', // apiBaseUrl: process.env.VUE_APP_API_BASE_URL ,
apiBaseUrl: '/api',
// 其他全局配置 // 其他全局配置
defaultRegion: 'us-east-1', defaultRegion: 'us-east-1',

16
frontend/vue.config.js Normal file
View File

@ -0,0 +1,16 @@
module.exports = {
devServer: {
proxy: {
'/api': {
target: 'http://127.0.0.1:8000',
changeOrigin: true,
ws: false,
secure: false,
// 保留 /api 前缀并转发到后端 FastAPI后端已以 /api 为前缀)
pathRewrite: { '^/api': '/api' },
},
},
},
}