// +----------------------------------------------------------------------
|
// | 环境选择入口:一套代码跑多环境
|
// | 识别优先级:手动指定 > 小程序 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'
|
});
|