Files
JayuCloud/WeChatProgram/utils/config.js

190 lines
6.9 KiB
JavaScript

/**
* Purpose: safeRequire supports config.js by holding or executing the safe require page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
function safeRequire(modulePath) {
try {
return require(modulePath)
} catch (error) {
return {}
}
}
/**
* Purpose: getEnvVersion supports config.js by holding or executing the get env version page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
function getEnvVersion() {
if (typeof wx === 'undefined') {
return 'develop'
}
try {
if (typeof wx.getAccountInfoSync === 'function') {
const accountInfo = wx.getAccountInfoSync()
const miniProgram = accountInfo && accountInfo.miniProgram
if (miniProgram && miniProgram.envVersion) {
return miniProgram.envVersion
}
}
} catch (error) {}
try {
if (typeof __wxConfig !== 'undefined' && __wxConfig && __wxConfig.envVersion) {
return __wxConfig.envVersion
}
} catch (error) {}
return 'develop'
}
/**
* Purpose: getRuntimePlatform supports config.js by holding or executing the get runtime platform page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
function getRuntimePlatform() {
if (typeof wx === 'undefined') {
return 'devtools'
}
try {
if (typeof wx.getDeviceInfo === 'function') {
const deviceInfo = wx.getDeviceInfo()
if (deviceInfo && deviceInfo.platform) {
return deviceInfo.platform
}
}
} catch (error) {}
try {
if (typeof wx.getSystemInfoSync === 'function') {
const systemInfo = wx.getSystemInfoSync()
if (systemInfo && systemInfo.platform) {
return systemInfo.platform
}
}
} catch (error) {}
return 'unknown'
}
/**
* Purpose: normalizeDomain supports config.js by holding or executing the normalize domain page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
function normalizeDomain(value, fallbackValue) {
const target = String(value || fallbackValue || '').trim()
if (!target) {
return ''
}
return target.replace(/\/+$/, '')
}
/**
* Purpose: ensureHttps supports config.js by holding or executing the ensure https page or API role.
* Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow.
*/
function ensureHttps(value, envVersion) {
if (!value) {
throw new Error(`Missing API domain for ${envVersion}`)
}
if (envVersion !== 'develop' && !value.startsWith('https://')) {
throw new Error(`${envVersion} environment must use HTTPS`)
}
return value
}
/**
* Purpose: localOverride supports config.js by holding or executing the local override page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
const localOverride = safeRequire('./config.local')
/**
* Purpose: ENV_VERSION supports config.js by holding or executing the env version page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
const ENV_VERSION = getEnvVersion()
/**
* Purpose: RUNTIME_PLATFORM supports config.js by holding or executing the runtime platform page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
const RUNTIME_PLATFORM = getRuntimePlatform()
/**
* Purpose: IS_DEVTOOLS supports config.js by holding or executing the is devtools page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
const IS_DEVTOOLS = RUNTIME_PLATFORM === 'devtools'
/**
* Purpose: DEFAULT_DOMAINS supports config.js by holding or executing the default domains page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
const DEFAULT_DOMAINS = {
local: 'http://127.0.0.1:80',
tunnel: 'https://yunail.art',
'dev-remote': 'https://yunail.art',
trial: 'https://yunail.art',
release: 'https://yunail.art'
}
/**
* Purpose: DOMAIN_MAP supports config.js by holding or executing the domain map page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
const DOMAIN_MAP = {
...DEFAULT_DOMAINS,
...(localOverride.domains || {})
}
/**
* Purpose: DEFAULT_DEVELOP_TARGET supports config.js by holding or executing the default develop target page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
const DEFAULT_DEVELOP_TARGET = IS_DEVTOOLS ? 'local' : 'dev-remote'
/**
* Purpose: DEVELOP_TARGET supports config.js by holding or executing the develop target page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
const DEVELOP_TARGET = String(localOverride.developTarget || DEFAULT_DEVELOP_TARGET)
/**
* Purpose: API_DOMAIN supports config.js by holding or executing the api domain page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
const API_DOMAIN = ensureHttps(
ENV_VERSION === 'develop'
? normalizeDomain(DOMAIN_MAP[DEVELOP_TARGET], DOMAIN_MAP['dev-remote'])
: normalizeDomain(DOMAIN_MAP[ENV_VERSION], DOMAIN_MAP.release),
ENV_VERSION
)
module.exports = {
ENV_VERSION,
RUNTIME_PLATFORM,
IS_DEVTOOLS,
DEVELOP_TARGET,
API_DOMAIN,
API_BASE_URL: `${API_DOMAIN}/api`,
IS_DEV_ENV: ENV_VERSION === 'develop',
REQUEST_TIMEOUT: Number(localOverride.requestTimeout || 10000),
ENABLE_REQUEST_SIGNING: localOverride.enableRequestSigning !== false,
IDLE_TIMEOUT_MS: 60 * 60 * 1000,
IDLE_WARNING_MS: 2 * 60 * 1000,
CACHE_TTL: {
banner: 5 * 60 * 1000,
publicShops: 5 * 60 * 1000,
tags: 10 * 60 * 1000,
homeList: 2 * 60 * 1000,
signedUrl: 50 * 60 * 1000
},
DEFAULT_AVATAR: 'https://api.dicebear.com/7.x/avataaars/svg?seed=default',
DEFAULT_PRODUCT_IMAGE: '/assets/images/default-product.png',
DEFAULT_SHOP_IMAGE: '/assets/images/default-shop.png',
NO_COUPON_KEY: 'NONE',
TAB_PAGES: [
'/pages/home/index',
'/pages/products/index',
'/pages/cart/index',
'/pages/profile/index'
]
}