import axios from 'axios' import { useGlobalStore } from '@/stores/modules/global' const { getSystemConfig } = useGlobalStore() export default new (class { constructor() { this.systemInfoAsync = getSystemConfig() } systemInfoAsync /** 服务配置信息 */ config = { commSearchUrl: '', goAccess: '', goCommonSearchUrl: '', hsbyBankSignZone: '', hsbyRegister: '', hsbyPayUrlWeb: '', hsbySignUp: '', mobileAuthUrl: '', mobileOpenUrl: '', newsUrl: '', openApiUrl: '', quoteHost: '', quotePort: '', quoteUrl: '', tradeHost: '', tradePort: '', tradeUrl: '', uploadUrl: '', quoteWS: '', tradeWS: '', oem: '', iOS: '', android: '', androidUpdateUrl: '', pcNewsUrl: '', pcMangerUrl: '', shareUrl: '', } /** 服务初始化完成状态 */ isReady = false /** 等待服务初始化 */ private isPending = false /** * 自动初始化,若断网或其它原因导致初始化失败,需重新初始化 * IOS上架审核可能会遇到网络权限的情况,应用可能会在未授权网络权限的情况下发起请求,导致请求等待时间过长,最终审核被拒 */ //private onload = this.init() /** * 手动初始化 */ private onload = Promise.resolve(this.config) /** 重试次数 */ private retryCount = 0 /** 限制重试次数,0 = 无限制 */ private retryLimit = 5 /** * 失败时重新尝试初始化,直到成功为止 */ private tryinit = () => { return new Promise((resolve, reject) => { if (this.retryLimit === 0 || this.retryCount < this.retryLimit) { this.retryCount++ setTimeout(() => { resolve(this.init()) }, 3000) } else { this.retryCount = 0 this.isPending = false reject('服务加载失败,请稍后再试') } }) } /** * 初始化服务配置 */ private async init(): Promise { this.isPending = true await this.systemInfoAsync.catch(() => { this.systemInfoAsync = getSystemConfig() }) return new Promise((resolve, reject) => { this.systemInfoAsync.then((res) => { // 获取服务接口地址 axios(res.apiUrl).then((res) => { this.config = res.data.data this.isReady = true resolve(this.config) }).catch(() => { const result = this.tryinit() resolve(result) }) }).catch(() => { reject('配置文件加载失败,请稍后再试') }) }) } /** * 服务初始化完成时触发 */ onReady() { if (!this.isReady && !this.isPending) { this.onload = this.init() } // 确保当前只有一个初始化实例 return this.onload } /** * 获取服务配置地址 * @param key * @returns */ getConfig(key: K) { return this.config[key] } })