|
|
@@ -28,7 +28,58 @@ export default ({ mode }) => {
|
|
|
proxy: {
|
|
|
'/api': {
|
|
|
target: VITE_API_URL,
|
|
|
- changeOrigin: true
|
|
|
+ changeOrigin: true,
|
|
|
+ secure: false,
|
|
|
+ // 处理 FastAPI 的尾部斜杠重定向问题
|
|
|
+ configure: (proxy, options) => {
|
|
|
+ proxy.on('error', (err, req, res) => {
|
|
|
+ console.log('❌ Proxy error:', err)
|
|
|
+ })
|
|
|
+ proxy.on('proxyReq', (proxyReq, req, res) => {
|
|
|
+ // 处理 FastAPI 的尾部斜杠重定向问题
|
|
|
+ // FastAPI 的列表接口使用 @router.get("/"),需要尾部斜杠
|
|
|
+ // 例如:/api/plans -> /api/plans/
|
|
|
+ // 例如:/api/master-data/companies -> /api/master-data/companies/
|
|
|
+ const originalUrl = req.url || ''
|
|
|
+ if (originalUrl && !originalUrl.endsWith('/')) {
|
|
|
+ // 分离路径和查询参数
|
|
|
+ const [path, query] = originalUrl.split('?')
|
|
|
+ const pathParts = path.split('/').filter(Boolean)
|
|
|
+
|
|
|
+ // 处理二级路径:/api/xxx(列表接口)
|
|
|
+ if (pathParts.length === 2 && pathParts[0] === 'api') {
|
|
|
+ const resource = pathParts[1]
|
|
|
+ // 列表接口资源:plans, inventory, weighing, users, companies, cargos
|
|
|
+ const listResources = ['plans', 'inventory', 'weighing', 'users', 'companies', 'cargos']
|
|
|
+ if (listResources.includes(resource)) {
|
|
|
+ const newPath = path + '/' + (query ? '?' + query : '')
|
|
|
+ proxyReq.path = newPath
|
|
|
+ req.url = newPath
|
|
|
+ console.log(`➡️ Proxying: ${req.method} ${originalUrl} -> ${VITE_API_URL}${newPath} (added trailing slash)`)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理三级路径:/api/master-data/xxx(基础资料模块)
|
|
|
+ if (pathParts.length === 3 && pathParts[0] === 'api' && pathParts[1] === 'master-data') {
|
|
|
+ const resource = pathParts[2]
|
|
|
+ // 基础资料模块的列表接口:companies, cargos
|
|
|
+ const masterDataResources = ['companies', 'cargos']
|
|
|
+ if (masterDataResources.includes(resource)) {
|
|
|
+ const newPath = path + '/' + (query ? '?' + query : '')
|
|
|
+ proxyReq.path = newPath
|
|
|
+ req.url = newPath
|
|
|
+ console.log(`➡️ Proxying: ${req.method} ${originalUrl} -> ${VITE_API_URL}${newPath} (added trailing slash)`)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ console.log(`➡️ Proxying: ${req.method} ${req.url} -> ${VITE_API_URL}${req.url}`)
|
|
|
+ })
|
|
|
+ proxy.on('proxyRes', (proxyRes, req, res) => {
|
|
|
+ console.log(`⬅️ Response: ${req.url} -> ${proxyRes.statusCode}`)
|
|
|
+ })
|
|
|
+ }
|
|
|
}
|
|
|
},
|
|
|
host: true
|