hookehuyr

feat(配置): 根据小程序环境动态设置API地址

添加getBaseUrl函数自动检测小程序运行环境并返回对应的API地址,简化环境切换流程
1 /* 1 /*
2 * @Date: 2022-09-19 14:11:06 2 * @Date: 2022-09-19 14:11:06
3 * @LastEditors: hookehuyr hookehuyr@gmail.com 3 * @LastEditors: hookehuyr hookehuyr@gmail.com
4 - * @LastEditTime: 2025-08-12 15:09:48 4 + * @LastEditTime: 2025-08-15 10:50:51
5 * @FilePath: /jgdl/src/utils/config.js 5 * @FilePath: /jgdl/src/utils/config.js
6 - * @Description: 文件描述 6 + * @Description: 环境配置文件 - 根据小程序运行环境自动切换API地址
7 */ 7 */
8 -// TAG:服务器环境配置
9 -const BASE_URL = "https://oa-dev.onwall.cn"; // 测试服务器
10 -// const BASE_URL = "https://jiangedianlv.onwall.cn"; // 正式服务器
11 8
12 -export default BASE_URL 9 +/**
10 + * 获取当前小程序运行环境对应的API地址
11 + * @returns {string} 对应环境的API基础地址
12 + */
13 +function getBaseUrl() {
14 + // 获取当前小程序的运行环境
15 + const accountInfo = wx.getAccountInfoSync();
16 + const envVersion = accountInfo.miniProgram.envVersion;
17 +
18 + // 定义不同环境的服务器地址
19 + let baseUrl = '';
20 + switch (envVersion) {
21 + case 'develop': // 开发版
22 + baseUrl = 'https://oa-dev.onwall.cn';
23 + break;
24 + case 'trial': // 体验版
25 + baseUrl = 'https://oa-dev.onwall.cn'; // 体验版暂时使用开发环境
26 + break;
27 + case 'release': // 正式版
28 + baseUrl = 'https://jiangedianlv.onwall.cn';
29 + break;
30 + default: // 未知环境,默认使用正式版地址
31 + baseUrl = 'https://jiangedianlv.onwall.cn';
32 + }
33 +
34 + // eslint-disable-next-line no-console
35 + console.log(`当前小程序环境: ${envVersion}, 使用API地址: ${baseUrl}`);
36 + return baseUrl;
37 +}
38 +
39 +// 导出API基础地址
40 +const BASE_URL = getBaseUrl();
41 +export default BASE_URL;
13 42
14 // 默认封面图片地址 43 // 默认封面图片地址
15 export const DEFAULT_COVER_IMG = 'https://images.unsplash.com/photo-1558981806-ec527fa84c39?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=60' 44 export const DEFAULT_COVER_IMG = 'https://images.unsplash.com/photo-1558981806-ec527fa84c39?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=60'
......