/** * 智能配置加载器 * 根据访问域名自动加载对应的配置文件 */ (function() { 'use strict'; // 域名到配置的映射关系 const DOMAIN_CONFIG_MAP = { 'console.duoweiying.cn': { file: 'frontend.console.config.js', type: 'frontend' }, 'admin.duoweiying.cn': { file: 'admin.config.js', type: 'admin' }, 'h5.duoweiying.cn': { file: 'h5.config.js', type: 'h5' }, 'code.duoweiying.cn': { file: 'code-protect.config.js', type: 'code-protect' }, 'ai-h5.duoweiying.cn': { file: 'miniapp.config.js', type: 'miniapp' } }; /** * 获取当前访问域名 */ function getCurrentDomain() { return window.location.hostname; } /** * 获取配置路径(基于 CDN) */ function getConfigPath(filename) { // 使用相对路径引用 config 目录 const basePaths = [ './config/', '../config/', '../../config/' ]; for (const basePath of basePaths) { try { const fullPath = basePath + filename; console.log(`尝试加载配置:${fullPath}`); return fullPath; } catch (e) { continue; } } throw new Error('无法找到配置文件路径'); } /** * 动态加载 JavaScript 文件 */ function loadScript(src) { return new Promise((resolve, reject) => { const script = document.createElement('script'); script.src = src; script.async = false; // 同步加载,确保顺序 script.onload = resolve; script.onerror = () => reject(new Error(`加载失败:${src}`)); document.head.appendChild(script); }); } /** * 初始化配置加载器 */ async function initializeConfigLoader() { const domain = getCurrentDomain(); const configInfo = DOMAIN_CONFIG_MAP[domain]; if (!configInfo) { console.warn(`未找到域名 ${domain} 的配置,使用默认配置`); return; } try { console.log(`检测到域名:${domain}`); console.log(`加载配置文件:${configInfo.file}`); // 加载配置文件 const configPath = getConfigPath(configInfo.file); await loadScript(configPath); console.log(`✅ 配置加载成功:${configInfo.type}`); // 打印配置信息 const configKey = `${configInfo.type.toUpperCase()}_CONFIG`; if (window[configKey]) { console.log('当前配置:', window[configKey]); } } catch (error) { console.error('❌ 配置加载失败:', error); console.error('请检查配置文件是否存在于 OBS 桶中'); } } // 在 DOM 加载完成后立即执行 if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', initializeConfigLoader); } else { initializeConfigLoader(); } // 导出全局函数 window.ConfigLoader = { getCurrentDomain, initializeConfigLoader }; })();