fix: 生产环境 API 同源,HF Space / Docker 前端可正常请求后端
Browse filesFiles changed (7)
10 unmodified lines111213141516171819202110 unmodified linesENV NODE_ENV=productionENV PATH=/app/node_modules/.bin:$PATH# 替换前端中的地址RUN sed -i 's#\${baseUrl}##g' /app/src/views/AccountManagement.vueRUN sed -i "s#\${import\.meta\.env\.VITE_API_BASE_URL || 'http:\/\/localhost:5409'}##g" /app/src/api/material.jsRUN sed -i 's#localhost:5409##g' /app/.env.productionRUN npm run build10 unmodified lines1112131415161710 unmodified linesENV NODE_ENV=productionENV PATH=/app/node_modules/.bin:$PATH# 生产构建:.env.production 中 VITE_API_BASE_URL 为空,前端与 Flask 同源(见 sau_frontend/src/utils/apiBase.js)RUN npm run build
12345678# 生产环境配置NODE_ENV=production# API 基础地址VITE_API_BASE_URL=http://localhost:5409# 是否开启 mockVITE_USE_MOCK=false12345678# 生产环境配置NODE_ENV=production# API:留空则与页面同源(Docker / HF Space 同一进程提供前后端)。本地直连后端可设为 http://127.0.0.1:5409VITE_API_BASE_URL=# 是否开启 mockVITE_USE_MOCK=false
123415 unmodified lines2021222324252627282930import { http } from '@/utils/request'// 素材管理APIexport const materialApi = {15 unmodified lines// 下载素材downloadMaterial: (filePath) => {return `${import.meta.env.VITE_API_BASE_URL || 'http://localhost:5409'}/download/${filePath}`},// 获取素材预览URLgetMaterialPreviewUrl: (filename) => {return `${import.meta.env.VITE_API_BASE_URL || 'http://localhost:5409'}/getFile?filename=${filename}`}}1234515 unmodified lines21222324252627282930313233import { http } from '@/utils/request'import { getApiBase } from '@/utils/apiBase'// 素材管理APIexport const materialApi = {15 unmodified lines// 下载素材downloadMaterial: (filePath) => {const b = getApiBase()return `${b}/download/${filePath}`},// 获取素材预览URLgetMaterialPreviewUrl: (filename) => {const b = getApiBase()return `${b}/getFile?filename=${filename}`}}
1234567891011/** * 生产环境前后端同源(Docker / Hugging Face Space)时请在 .env.production 中设 VITE_API_BASE_URL 为空。 * 开发环境使用 .env.development 中的 /api(Vite 代理到后端)。 */export function getApiBase() { const v = import.meta.env.VITE_API_BASE_URL if (v != null && String(v).trim() !== '') { return String(v).replace(/\/$/, '') } return ''}123456789import axios from 'axios'import { ElMessage } from 'element-plus'// 创建axios实例const request = axios.create({baseURL: import.meta.env.VITE_API_BASE_URL || 'http://localhost:5409',headers: {'Content-Type': 'application/json'}12345678910import axios from 'axios'import { ElMessage } from 'element-plus'import { getApiBase } from '@/utils/apiBase'// 创建axios实例const request = axios.create({baseURL: getApiBase(),headers: {'Content-Type': 'application/json'}
433 unmodified lines434435436437438439232 unmodified lines672673674675676677678113 unmodified lines792793794795796797798433 unmodified linesimport { useAccountStore } from '@/stores/account'import { useAppStore } from '@/stores/app'import { http } from '@/utils/request'// 获取账号状态管理const accountStore = useAccountStore()232 unmodified lines// 下载Cookie文件const handleDownloadCookie = (row) => {// 从后端获取Cookie文件const baseUrl = import.meta.env.VITE_API_BASE_URL || 'http://localhost:5409'const downloadUrl = `${baseUrl}/downloadCookie?filePath=${encodeURIComponent(row.filePath)}`// 创建一个隐藏的链接来触发下载113 unmodified linesconst type = platformTypeMap[platform] || '1'// 创建SSE连接const baseUrl = import.meta.env.VITE_API_BASE_URL || 'http://localhost:5409'const url = `${baseUrl}/login?type=${type}&id=${encodeURIComponent(name)}`eventSource = new EventSource(url)433 unmodified lines434435436437438439440232 unmodified lines673674675676677678679113 unmodified lines793794795796797798799433 unmodified linesimport { useAccountStore } from '@/stores/account'import { useAppStore } from '@/stores/app'import { http } from '@/utils/request'import { getApiBase } from '@/utils/apiBase'// 获取账号状态管理const accountStore = useAccountStore()232 unmodified lines// 下载Cookie文件const handleDownloadCookie = (row) => {// 从后端获取Cookie文件const baseUrl = getApiBase()const downloadUrl = `${baseUrl}/downloadCookie?filePath=${encodeURIComponent(row.filePath)}`// 创建一个隐藏的链接来触发下载113 unmodified linesconst type = platformTypeMap[platform] || '1'// 创建SSE连接const baseUrl = getApiBase()const url = `${baseUrl}/login?type=${type}&id=${encodeURIComponent(name)}`eventSource = new EventSource(url)
497 unmodified lines498499500501502503504505506497 unmodified linesimport { useAppStore } from '@/stores/app'import { materialApi } from '@/api/material'import { http } from '@/utils/request'// API base URLconst apiBaseUrl = import.meta.env.VITE_API_BASE_URL || 'http://localhost:5409'// Authorization headersconst authHeaders = computed(() => ({497 unmodified lines498499500501502503504505506507497 unmodified linesimport { useAppStore } from '@/stores/app'import { materialApi } from '@/api/material'import { http } from '@/utils/request'import { getApiBase } from '@/utils/apiBase'// API base URL(生产同源时为空,请求走当前域名)const apiBaseUrl = getApiBase()// Authorization headersconst authHeaders = computed(() => ({
Dockerfile+1-5
sau_frontend/.env.production+2-2
sau_frontend/src/api/material.js+5-2
sau_frontend/src/utils/apiBase.jsAdded+11
sau_frontend/src/utils/request.js+2-1
sau_frontend/src/views/AccountManagement.vue+3-2
sau_frontend/src/views/PublishCenter.vue+3-2