import axios from 'axios' import plus from '@/utils/h5plus' export default new (class { systemInfo = { version: '1.0.0', versionCode: '100000', apiUrl: 'http://localhost', shwoRegister: true } /** 服务配置信息 */ 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('服务加载失败,请稍后再试') } }) } /** * 初始化服务配置 * https://uniapp.dcloud.net.cn/tutorial/app-ios-uiwebview.html */ private init(): Promise { this.isPending = true return new Promise((resolve, reject) => { const filePath = './config/appconfig.json' const getAppConfig = async () => { if (plus.hasPlus()) { const res = await plus.getLocalFileContent(filePath) return JSON.parse(res) } else { const res = await axios(filePath) return res.data } } getAppConfig().then((res) => { this.systemInfo = { ...this.systemInfo, ...res } // 获取服务接口地址 axios(this.systemInfo.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] } /** * 获取系统信息 * @param key * @returns */ getSystemInfo(key: K) { return this.systemInfo[key] } })