69 lines
2.3 KiB
JavaScript
69 lines
2.3 KiB
JavaScript
// 远端服务配置(统一修改入口)
|
||
// 更换服务器时,仅需修改这里。
|
||
|
||
const SERVER_CONFIG = {
|
||
protocol: 'https',
|
||
host: 'fonts.biboer.cn',
|
||
// 留空表示使用协议默认端口(https:443 / http:80)
|
||
port: '',
|
||
apiPrefix: '/api',
|
||
fontsManifestPath: '/miniprogram/assets/fonts.json',
|
||
defaultConfigPath: '/miniprogram/assets/default.json',
|
||
routeConfigPath: '/miniprogram/assets/route-config.json',
|
||
}
|
||
|
||
function buildOrigin() {
|
||
const protocol = String(SERVER_CONFIG.protocol || 'https').replace(/:$/, '')
|
||
const host = String(SERVER_CONFIG.host || '').trim()
|
||
const port = String(SERVER_CONFIG.port || '').trim()
|
||
|
||
if (!host) {
|
||
throw new Error('SERVER_CONFIG.host 未配置')
|
||
}
|
||
|
||
const hasDefaultPort = (protocol === 'https' && port === '443') || (protocol === 'http' && port === '80')
|
||
const portPart = !port || hasDefaultPort ? '' : `:${port}`
|
||
return `${protocol}://${host}${portPart}`
|
||
}
|
||
|
||
function normalizePath(path, fallback) {
|
||
const value = String(path || fallback || '').trim()
|
||
if (!value) {
|
||
return '/'
|
||
}
|
||
return value.startsWith('/') ? value : `/${value}`
|
||
}
|
||
|
||
function normalizeBaseUrl(baseUrl) {
|
||
const value = String(baseUrl || '').trim()
|
||
if (!value) {
|
||
return buildOrigin()
|
||
}
|
||
|
||
const withProtocol = /^https?:\/\//i.test(value) ? value : `https://${value}`
|
||
return withProtocol.replace(/\/+$/, '')
|
||
}
|
||
|
||
function buildRuntimeConfig(options = {}) {
|
||
const origin = normalizeBaseUrl(options.baseUrl)
|
||
const apiPrefix = normalizePath(SERVER_CONFIG.apiPrefix, '/api').replace(/\/$/, '')
|
||
const fontsManifestPath = normalizePath(SERVER_CONFIG.fontsManifestPath, '/miniprogram/assets/fonts.json')
|
||
const defaultConfigPath = normalizePath(SERVER_CONFIG.defaultConfigPath, '/miniprogram/assets/default.json')
|
||
const routeConfigPath = normalizePath(SERVER_CONFIG.routeConfigPath, '/miniprogram/assets/route-config.json')
|
||
|
||
return {
|
||
activeServerKey: String(options.activeServerKey || '').trim(),
|
||
fontsBaseUrl: origin,
|
||
fontsManifestUrl: `${origin}${fontsManifestPath}`,
|
||
defaultConfigUrl: `${origin}${defaultConfigPath}`,
|
||
routeConfigUrl: `${origin}${routeConfigPath}`,
|
||
svgRenderApiUrl: `${origin}${apiPrefix}/render-svg`,
|
||
pngRenderApiUrl: `${origin}${apiPrefix}/render-png`,
|
||
}
|
||
}
|
||
|
||
module.exports = {
|
||
SERVER_CONFIG,
|
||
buildRuntimeConfig,
|
||
}
|