/** * 接口根地址(与 ruoyi-ui 的 VUE_APP_BASE_API 对齐) * * - 开发:.env.development → /dev-api(H5 代理)或 VITE_APP_API_HOST 直连 * - 生产:.env.production → /prod-api(与 ruoyi-ui/.env.production 一致,由 Nginx 转发) * * 打包 H5:uni build -p h5 */ import { isExternal } from '@/utils/validate' const apiHost = String(import.meta.env.VITE_APP_API_HOST || 'http://192.168.1.6:8010').replace(/\/$/, '') /** 与 ruoyi-ui 的 VUE_APP_BASE_API 同源配置(dev/prod 由 .env 注入) */ const envBaseApi = String(import.meta.env.VITE_APP_BASE_API || '').trim() /** H5 打包后取浏览器当前 host(IP/域名),与 ruoyi-ui 生产 /prod-api 同域反代一致 */ function getBrowserOrigin() { if (typeof window === 'undefined' || !window.location) { return '' } return String(window.location.origin || '').replace(/\/$/, '') } /** * H5 开发是否走 /dev-api 代理(需 vite.config.js rewrite) * 默认 false:直连 VITE_APP_API_HOST */ export const H5_USE_PROXY = false function resolveBaseApi() { if (import.meta.env.PROD) { const apiPath = envBaseApi || '/prod-api' // #ifdef H5 const origin = getBrowserOrigin() if (origin) { const normalized = apiPath.startsWith('/') ? apiPath : `/${apiPath}` return `${origin}${normalized}`.replace(/\/$/, '') } // #endif // #ifndef H5 if (/^https?:\/\//i.test(apiHost)) { return apiHost } // #endif return apiPath } const devBase = envBaseApi || '/dev-api' // #ifdef H5 return H5_USE_PROXY ? devBase : apiHost // #endif // #ifndef H5 return apiHost // #endif } export const BASE_API = resolveBaseApi() /** * 拼接接口或静态资源地址(/login、/profile/...) * @param {string} path */ export function joinApiUrl(path) { if (!path) { return BASE_API } if (isExternal(path)) { return path } const p = path.startsWith('/') ? path : '/' + path const base = String(BASE_API).replace(/\/$/, '') return base + p } export const TOKEN_KEY = 'Admin-Token' export const REMEMBER_USERNAME_KEY = 'login_username'