58 lines
1.2 KiB
JavaScript
58 lines
1.2 KiB
JavaScript
const { REQUEST_TIMEOUT } = require('./constants')
|
|
|
|
const ENVIRONMENTS = {
|
|
dev: {
|
|
name: 'dev',
|
|
baseURL: 'https://dev-api.example.com',
|
|
timeout: REQUEST_TIMEOUT
|
|
},
|
|
test: {
|
|
name: 'test',
|
|
baseURL: 'https://test-api.example.com',
|
|
timeout: REQUEST_TIMEOUT
|
|
},
|
|
prod: {
|
|
name: 'prod',
|
|
baseURL: 'https://api.example.com',
|
|
timeout: REQUEST_TIMEOUT
|
|
}
|
|
}
|
|
|
|
function resolveRuntimeEnv(envVersion) {
|
|
const runtime = envVersion || getWxEnvVersion()
|
|
|
|
if (runtime === 'release' || runtime === 'prod') {
|
|
return 'prod'
|
|
}
|
|
|
|
if (runtime === 'trial' || runtime === 'test') {
|
|
return 'test'
|
|
}
|
|
|
|
return 'dev'
|
|
}
|
|
|
|
function getWxEnvVersion() {
|
|
if (typeof wx === 'undefined' || typeof wx.getAccountInfoSync !== 'function') {
|
|
return process.env.MINIPROGRAM_ENV || 'develop'
|
|
}
|
|
|
|
try {
|
|
const accountInfo = wx.getAccountInfoSync()
|
|
return accountInfo?.miniProgram?.envVersion || 'develop'
|
|
} catch (error) {
|
|
return 'develop'
|
|
}
|
|
}
|
|
|
|
function getRuntimeConfig(envVersion) {
|
|
const envKey = resolveRuntimeEnv(envVersion)
|
|
return ENVIRONMENTS[envKey]
|
|
}
|
|
|
|
module.exports = {
|
|
ENVIRONMENTS,
|
|
getRuntimeConfig,
|
|
resolveRuntimeEnv
|
|
}
|