Jacob
2026-10-14 d44cf3af14c46fc96cd94525e46d191e5f700463
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// +----------------------------------------------------------------------
// | 环境选择入口:一套代码跑多环境
// | 识别优先级:手动指定 > 小程序 appid 自动识别 > H5 域名识别 > 默认 prod
// | 说明:小程序 appid 由 manifest.json -> mp-weixin.appid 决定,
// |       所以“用哪个 appid 跑”就等于“用哪套接口”,不会出现接口与环境错配
// +----------------------------------------------------------------------
const envs = {
   prod: require('./prod.js'),
   sit: require('./sit.js')
};
 
// 手动强制指定环境:'' = 自动识别(推荐);也可填 'prod' 或 'sit'
const OVERRIDE_ENV = '';
 
// appid -> 环境名 映射
const APPID_ENV_MAP = {};
Object.keys(envs).forEach(function(key) {
   APPID_ENV_MAP[envs[key].APPID] = key;
});
 
function detectEnv() {
   if (OVERRIDE_ENV && envs[OVERRIDE_ENV]) return OVERRIDE_ENV;
 
   // #ifdef MP
   try {
       const info = uni.getAccountInfoSync();
       const appId = info && info.miniProgram && info.miniProgram.appId;
       if (appId && APPID_ENV_MAP[appId]) return APPID_ENV_MAP[appId];
   } catch (e) {}
   // #endif
 
   // #ifdef H5
   if (typeof location !== 'undefined' && location.hostname) {
       const host = location.hostname;
       if (/sit|tohours|localhost|127\.0\.0\.1/.test(host)) return 'sit';
   }
   // #endif
 
   return 'prod';
}
 
const ENV_NAME = detectEnv();
const current = envs[ENV_NAME] || envs.prod;
 
module.exports = Object.assign({}, current, {
   ENV_NAME: ENV_NAME,
   IS_PROD: ENV_NAME === 'prod',
   IS_SIT: ENV_NAME === 'sit'
});