import { reactive } from 'vue' import axios from 'axios' import plus from '@/utils/h5plus' interface AppConfig { appId: string; appName: string; // 应用名称 version: string; // 应用版本 versionCode: string; // 应用版本号 apiUrl: string; // API地址 quoteUrl: string; // WS地址 tradeUrl: string; // WS地址 tokenStore: 'session' | 'local'; // token 存储方式 locales: string[]; // 多语言配置 loginVerifyCodeEnabled: boolean; // 是否启用登录验证码 } export default new (class { /** 服务配置信息 */ private appConfig = reactive({ appId: '', appName: '', version: '1.0.0', versionCode: '100000', apiUrl: '', quoteUrl: '', tradeUrl: '', tokenStore: 'session', locales: [], loginVerifyCodeEnabled: true }) /** 服务初始化完成状态 */ isReady = false /** * 手动初始化 */ private onload = Promise.resolve() /** * 初始化服务配置 */ private async init() { const filePath = './config/appconfig.json' if (plus.hasPlus()) { const res = await plus.getLocalFileContent(filePath) Object.assign(this.appConfig, JSON.parse(res)) } else { const res = await axios(filePath) Object.assign(this.appConfig, res.data) } this.isReady = true } /** * 服务初始化完成时触发 */ onReady() { if (!this.isReady) { this.onload = this.init() } // 确保当前只有一个初始化实例 return this.onload } /** * 获取服务配置地址 * @param key * @returns */ getConfig(key: K) { return this.appConfig[key] } })