修正开发环境和远程生产环境切换的bug。
This commit is contained in:
@@ -58,23 +58,16 @@ export function getApiEnv() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 导出完整的 API URL 构建函数
|
// 导出完整的 API URL 构建函数
|
||||||
// H5环境下:local环境直接使用完整URL(避免代理配置问题),prod环境使用代理路径
|
// H5环境下:local / prod 均直接使用完整URL,避免依赖本地代理
|
||||||
// 其他环境使用完整URL
|
// 其他环境使用完整URL
|
||||||
export function getApiUrl(path = '') {
|
export function getApiUrl(path = '') {
|
||||||
const apiPath = path.startsWith('/') ? path : `/${path}`
|
const apiPath = path.startsWith('/') ? path : `/${path}`
|
||||||
// #ifdef H5
|
// #ifdef H5
|
||||||
// H5环境下,local环境直接使用完整URL,prod环境使用代理路径
|
// H5环境下,local / prod 都返回完整URL,确保 prod 时直接请求远程服务器
|
||||||
if (API_ENV === 'local') {
|
const fullPath = apiPath.startsWith('/') ? apiPath.slice(1) : apiPath
|
||||||
// local环境直接使用完整URL,避免代理配置问题
|
const fullUrl = `${API_BASE_URL}${fullPath}`
|
||||||
const fullPath = apiPath.startsWith('/') ? apiPath.slice(1) : apiPath
|
console.log(`[API Config] H5环境(${API_ENV})直接使用完整URL:`, fullUrl)
|
||||||
const fullUrl = `${API_BASE_URL}${fullPath}`
|
return fullUrl
|
||||||
console.log('[API Config] H5环境(local)直接使用完整URL:', fullUrl)
|
|
||||||
return fullUrl
|
|
||||||
} else {
|
|
||||||
// prod环境使用代理路径,代理配置在manifest.json中
|
|
||||||
console.log('[API Config] H5环境(prod)使用代理路径:', apiPath, '(代理target:', API_BASE_URL.replace(/\/$/, ''), ')')
|
|
||||||
return apiPath
|
|
||||||
}
|
|
||||||
// #endif
|
// #endif
|
||||||
// #ifndef H5
|
// #ifndef H5
|
||||||
// 其他环境使用完整URL
|
// 其他环境使用完整URL
|
||||||
|
|||||||
@@ -3,6 +3,6 @@
|
|||||||
* apiEnv 可选:'local' | 'prod'
|
* apiEnv 可选:'local' | 'prod'
|
||||||
*/
|
*/
|
||||||
export default {
|
export default {
|
||||||
apiEnv: 'prod'
|
apiEnv: 'local'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
* 自动添加 tenantId、token 等公共参数
|
* 自动添加 tenantId、token 等公共参数
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { getApiUrl } from './config.js'
|
import { getApiUrl, getApiEnv } from './config.js'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取 tenantId(从本地存储)
|
* 获取 tenantId(从本地存储)
|
||||||
@@ -55,6 +55,11 @@ export function request(options = {}) {
|
|||||||
|
|
||||||
// 处理 URL(自动添加 baseUrl)
|
// 处理 URL(自动添加 baseUrl)
|
||||||
const fullUrl = url.startsWith('http') ? url : getApiUrl(url)
|
const fullUrl = url.startsWith('http') ? url : getApiUrl(url)
|
||||||
|
const apiEnv = typeof getApiEnv === 'function' ? getApiEnv() : ''
|
||||||
|
|
||||||
|
// 每次请求打印完整 URL,格式示例:
|
||||||
|
// H5环境(local)直接使用完整URL: http://localhost:8090/api/audioManagement/list
|
||||||
|
console.log(`H5环境(${apiEnv})直接使用完整URL: ${fullUrl}`)
|
||||||
|
|
||||||
// 构建请求头
|
// 构建请求头
|
||||||
const requestHeader = {
|
const requestHeader = {
|
||||||
@@ -88,7 +93,7 @@ export function request(options = {}) {
|
|||||||
// 发起请求
|
// 发起请求
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
uni.request({
|
uni.request({
|
||||||
url: finalUrl,
|
url: fullUrl,
|
||||||
method: method.toUpperCase(),
|
method: method.toUpperCase(),
|
||||||
data: requestData,
|
data: requestData,
|
||||||
header: requestHeader,
|
header: requestHeader,
|
||||||
@@ -99,7 +104,7 @@ export function request(options = {}) {
|
|||||||
},
|
},
|
||||||
fail: (err) => {
|
fail: (err) => {
|
||||||
console.error('[Request] 请求失败:', {
|
console.error('[Request] 请求失败:', {
|
||||||
url: finalUrl,
|
url: fullUrl,
|
||||||
method: method.toUpperCase(),
|
method: method.toUpperCase(),
|
||||||
error: err
|
error: err
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user