/** * API 配置工具 * 根据运行环境自动选择正确的 API 地址 */ // 生产环境配置 const PROD_CONFIG = { web: 'https://book.rrbrr.com/api', android: 'https://book.rrbrr.com/api', ios: 'https://book.rrbrr.com/api', h5: 'https://book.rrbrr.com/api', }; // 开发环境配置 const DEV_CONFIG = { // 开发模式(PC浏览器)- 需加 /api 以匹配 Vite 代理 web: '/api', // 安卓真机 - 使用真实域名测试 android: 'https://book.rrbrr.com/api', // iOS 真机 - 使用真实域名测试 ios: 'https://book.rrbrr.com/api', // H5 - 需加 /api 以匹配 Vite 代理 h5: '/api', }; // 判断运行环境 function getPlatform(): 'web' | 'android' | 'ios' | 'h5' { // #ifdef H5 return 'h5'; // #endif // #ifdef APP-PLUS // @ts-ignore const systemInfo = uni.getSystemInfoSync(); const platform = (systemInfo.platform || '').toLowerCase(); if (platform === 'android') return 'android'; if (platform === 'ios') return 'ios'; return 'android'; // #endif return 'web'; } // 获取当前 API 地址 export function getApiBaseUrl(): string { const platform = getPlatform(); // 生产环境使用固定域名 // #ifdef H5 if (process.env.NODE_ENV === 'production') { return PROD_CONFIG.h5; } // #endif // #ifdef H5 // H5 环境判断是否在微信小程序模拟器中运行 // @ts-ignore if (typeof window !== 'undefined' && window.__uniConfig && window.__uniConfig.platform === 'mp-weixin') { return DEV_CONFIG.web; } return DEV_CONFIG.h5; // #endif // @ts-ignore return DEV_CONFIG[platform]; } // 获取后端服务器地址(不含 /api) export function getServerBaseUrl(): string { const apiUrl = getApiBaseUrl(); return apiUrl.replace('/api', ''); } // 获取前端基础地址(用于生成分享链接) export function getBaseUrl(): string { // #ifdef H5 if (typeof window !== 'undefined') { return window.location.origin; } // #endif const apiUrl = getApiBaseUrl(); return apiUrl.replace('/api', ''); } // 获取局域网 IP 地址 export function getLocalIP(): string { // 可以通过执行系统命令获取,或者硬编码配置 // 这里返回空字符串,用户需要在 DEV_CONFIG 中配置 return ''; }