This commit is contained in:
2026-08-15 22:16:58 +08:00
parent a344aaedf3
commit aed71d237d
15 changed files with 13296 additions and 306 deletions
+26 -8
View File
@@ -1,12 +1,13 @@
/**
* 从项目根目录 .env 读取接口地址。
* 变量名:VITE_API_BASE_URL
* 调试默认 localhost;正式构建前在 .env 里改成 https://api.yunzer.cn 即可。
* 接口 baseURL 解析。
* 优先级:VITE_API_BASE_URL > H5 开发代理 > H5 生产域名推断 > 默认生产 API
*/
/** 极验 4.0 captcha_id(前端展示用;KEY 仅在后端校验) */
export const GEETEST4_CAPTCHA_ID = '75e8a175c43b9ecfa15372c658be05e5'
const DEFAULT_PROD_API = 'https://api.yunzer.cn'
/**
* @returns {string} 去掉末尾斜杠的 baseURL
*/
@@ -27,14 +28,31 @@ export function resolveBaseURL() {
}
// #ifdef H5
// H5 开发未配置 env 时走 vite 代理(前缀勿用 /api,会与 api/ 源码目录冲突)
if (typeof import.meta !== 'undefined' && import.meta.env && import.meta.env.DEV) {
return '/proxy-api'
if (typeof window !== 'undefined' && window.location) {
const { hostname, protocol } = window.location
// 生产域名优先:避免 HBuilderX 构建后 DEV 仍为 true 时误走 /proxy-api
if (hostname.includes('yunzer.cn')) {
return DEFAULT_PROD_API
}
// 本地 H5 开发走 vite 代理
if (
(hostname === 'localhost' || hostname === '127.0.0.1') &&
typeof import.meta !== 'undefined' &&
import.meta.env &&
import.meta.env.DEV
) {
return '/proxy-api'
}
if (hostname === 'localhost' || hostname === '127.0.0.1') {
return `${protocol}//${hostname}:9000`.replace(/\/$/, '')
}
}
// #endif
// App / 真机:localhost 指向设备自身,需用 .env.development 配置局域网 IP
return 'http://localhost:9000'
return DEFAULT_PROD_API
}
/** 与 resolveBaseURL 相同,便于业务侧统一引用 */
+6 -4
View File
@@ -5,10 +5,12 @@
import { getToken, logout } from '@/utils/auth.js'
import { resolveBaseURL, getBaseURL } from '@/api/config.js'
const BASE_URL = resolveBaseURL()
export { getBaseURL }
function buildFullUrl(url) {
return resolveBaseURL().replace(/\/$/, '') + url
}
/**
* @param {{ url: string, method?: string, data?: object, header?: object, silent?: boolean }} options
* @returns {Promise<any>} 业务 data 字段(若无 data 则返回整包)
@@ -24,7 +26,7 @@ export function request(options = {}) {
headers.Authorization = `Bearer ${token}`
}
const fullUrl = BASE_URL.replace(/\/$/, '') + url
const fullUrl = buildFullUrl(url)
return new Promise((resolve, reject) => {
uni.request({
@@ -96,7 +98,7 @@ export function requestRaw(options = {}) {
headers.Authorization = `Bearer ${token}`
}
const fullUrl = BASE_URL.replace(/\/$/, '') + url
const fullUrl = buildFullUrl(url)
return new Promise((resolve, reject) => {
uni.request({