Skip to content

Commit 00535ec

Browse files
author
OpenClaw
committed
fix(frontend): fix API URL configuration for production build
- Add API_BASE_URL constant for production builds - Update ChartView to use full API URL - Add Vite dev server proxy configuration
1 parent fd21e5b commit 00535ec

3 files changed

Lines changed: 15 additions & 12 deletions

File tree

frontend/src/api/request.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
import axios from 'axios'
22

3-
// 根据环境自动选择 API 基础 URL
4-
const getBaseURL = () => {
5-
// 优先使用环境变量
6-
if (import.meta.env.VITE_API_BASE_URL) {
7-
return import.meta.env.VITE_API_BASE_URL
8-
}
9-
// 默认使用本地后端(开发测试)
10-
return 'http://localhost:8000'
11-
}
3+
// API 基础 URL - 生产环境使用完整 URL
4+
const API_BASE_URL = 'http://localhost:8000'
125

136
const api = axios.create({
14-
baseURL: getBaseURL(),
7+
baseURL: API_BASE_URL,
158
timeout: 30000,
169
})
1710

frontend/src/views/ChartView.vue

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,11 @@ const sources = [
8181
const activeSource = ref('netease')
8282
const chartData = ref([])
8383
84+
const API_BASE_URL = 'http://localhost:8000'
85+
8486
const fetchChart = async () => {
8587
try {
86-
const response = await fetch(`/api/v1/chart/${activeSource.value}/new_songs?limit=20`)
88+
const response = await fetch(`${API_BASE_URL}/api/v1/chart/${activeSource.value}/new_songs?limit=20`)
8789
const data = await response.json()
8890
chartData.value = data.entries || []
8991
} catch (error) {
@@ -97,7 +99,7 @@ const playSong = (song: any) => {
9799
98100
const subscribeSong = async (song: any) => {
99101
try {
100-
await fetch('/api/v1/subscribes', {
102+
await fetch(`${API_BASE_URL}/api/v1/subscribes`, {
101103
method: 'POST',
102104
headers: { 'Content-Type': 'application/json' },
103105
body: JSON.stringify({

frontend/vite.config.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,12 @@ export default defineConfig({
1010
'@': fileURLToPath(new URL('./src', import.meta.url)),
1111
},
1212
},
13+
server: {
14+
proxy: {
15+
'/api': {
16+
target: 'http://localhost:8000',
17+
changeOrigin: true,
18+
},
19+
},
20+
},
1321
})

0 commit comments

Comments
 (0)