|
@@ -32,6 +32,44 @@ export default defineConfig(({ mode }) => {
|
|
|
console.log('❌ Proxy error:', err)
|
|
console.log('❌ Proxy error:', err)
|
|
|
})
|
|
})
|
|
|
proxy.on('proxyReq', (proxyReq, req, res) => {
|
|
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} -> ${apiBaseUrl}${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} -> ${apiBaseUrl}${newPath} (added trailing slash)`)
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
console.log(`➡️ Proxying: ${req.method} ${req.url} -> ${apiBaseUrl}${req.url}`)
|
|
console.log(`➡️ Proxying: ${req.method} ${req.url} -> ${apiBaseUrl}${req.url}`)
|
|
|
})
|
|
})
|
|
|
proxy.on('proxyRes', (proxyRes, req, res) => {
|
|
proxy.on('proxyRes', (proxyRes, req, res) => {
|