Files
jiapuapp/utils/runtime-config.js
T

35 lines
1.5 KiB
JavaScript

const configuredMode = import.meta.env.VITE_JIAPU_RUNTIME_MODE || 'remote'
const configuredBaseUrl = import.meta.env.VITE_JIAPU_API_BASE_URL || 'https://backend-api.ddxcjp.cn'
const configuredClientId = import.meta.env.VITE_JIAPU_CLIENT_ID || '428a8310cd442757ae699df5d894f051'
const configuredTenantId = import.meta.env.VITE_JIAPU_TENANT_ID || '000000'
if (!['mock', 'remote'].includes(configuredMode)) {
throw new Error('VITE_JIAPU_RUNTIME_MODE 只允许 mock 或 remote')
}
if (configuredMode === 'remote' && !/^https:\/\/[^\s/]+(?:\/[^\s]*)?$/.test(configuredBaseUrl)) {
throw new Error('VITE_JIAPU_API_BASE_URL 必须是 HTTPS 地址')
}
if (configuredBaseUrl.endsWith('/')) {
throw new Error('VITE_JIAPU_API_BASE_URL 不能以斜杠结尾')
}
if (!configuredClientId || !configuredTenantId) {
throw new Error('远程服务的 clientId 和 tenantId 不能为空')
}
// 业务模块只消费这一个不可变配置;不同构建环境通过 VITE_JIAPU_* 覆盖。
export const runtimeConfig = Object.freeze({
mode: configuredMode,
baseUrl: configuredMode === 'remote' ? configuredBaseUrl : '',
clientId: configuredClientId,
tenantId: configuredTenantId
})
export const isMockMode = () => runtimeConfig.mode === 'mock'
export const hasRemoteConfig = () => runtimeConfig.mode === 'remote' && Boolean(runtimeConfig.baseUrl && runtimeConfig.clientId)
export const resolveRuntimeMode = () => {
if (isMockMode()) return 'mock'
if (hasRemoteConfig()) return 'remote'
throw new Error('运行模式配置无效:只允许 mock 或配置完整的 remote')
}