import { appConfig } from '../config/index' import { httpRequest } from './http/index' // import socket from './socket/index' export default new (class { /** 服务配置信息 */ config = { commSearchUrl: '', goCommonSearchUrl: '', mobileAuthUrl: '', mobileOpenUrl: '', newsUrl: '', openApiUrl: '', quoteHost: '', quotePort: '', quoteUrl: '', tradeHost: '', tradePort: '', tradeUrl: '', uploadUrl: '', oem: '', pcNewsUrl: '', pcMangerUrl: '', goAccess: '' } /** 服务初始化状态 */ isReady = false; /** 等待服务初始化 */ private pending = true; /** * 尝试自动初始化,若断网或其它原因导致初始化失败,需手动初始化 */ private tryInit = this.init(); /** * 初始化服务配置 */ private init(): Promise { return new Promise((resolve, reject) => { // 获取服务接口地址 httpRequest(appConfig.apiUrl, 'GET', { success: (res) => { console.log('服务配置信息', res.data); this.config = res.data as any; this.isReady = true; resolve(); // 连接交易服务 // socket.connectTrade(); }, fail: () => { reject('获取服务配置地址失败'); }, complete: () => { this.pending = false; } }) }) } /** * 服务初始化完成时触发 * @param callback */ async onReady(callback?: () => void) { // 初始化失败时重新初始化 if (!this.isReady && !this.pending) { this.tryInit = this.init(); } // 确保当前只有一个初始化实例 await this.tryInit.then(() => callback && callback()); } })