|
|
@@ -1,138 +1,149 @@
|
|
|
-import axios from 'axios'
|
|
|
-import { useGlobalStore } from '@/stores/modules/global'
|
|
|
-import { getServerTime } from './api/common'
|
|
|
import moment from 'moment'
|
|
|
-
|
|
|
-const { getSystemConfig, $toRefs } = useGlobalStore()
|
|
|
-const { dateDiff } = $toRefs()
|
|
|
+import plus from '@/utils/h5plus'
|
|
|
+import { TaskQueue } from '@/utils/queue'
|
|
|
+import { AppConfig, ServiceConfig } from './types'
|
|
|
+import { getServerTime } from './api/common'
|
|
|
|
|
|
export default new (class {
|
|
|
constructor() {
|
|
|
- this.systemInfoAsync = getSystemConfig()
|
|
|
+ // 自动初始化,若断网或其它原因导致初始化失败,需重新初始化
|
|
|
+ // IOS上架审核可能会遇到网络权限的情况,应用可能会在未授权网络权限的情况下发起请求,导致请求等待时间过长,最终审核被拒
|
|
|
+ //this.init()
|
|
|
+
|
|
|
+ this.onReady(() => {
|
|
|
+ // 同步服务器时间
|
|
|
+ getServerTime().then((res) => {
|
|
|
+ const serverTime = moment(res.data)
|
|
|
+ this.serverDiffTime = moment().diff(serverTime)
|
|
|
+ })
|
|
|
+ })
|
|
|
}
|
|
|
|
|
|
- systemInfoAsync
|
|
|
+ isReady = false
|
|
|
+
|
|
|
+ appConfigAsync = this.loadAppConfig()
|
|
|
+
|
|
|
+ private taskQueue = new TaskQueue()
|
|
|
+
|
|
|
+ /** 服务器和本地的时间差(毫秒数) */
|
|
|
+ private serverDiffTime = 0
|
|
|
+
|
|
|
+ /** 限制重试次数,0 = 无限制 */
|
|
|
+ private maxRetryCount = 5
|
|
|
+
|
|
|
+ private initPromise: Promise<void> | null = null
|
|
|
|
|
|
- /** 服务配置信息 */
|
|
|
- config = {
|
|
|
+ private appConfig: AppConfig = {
|
|
|
+ appName: document.title,
|
|
|
+ version: '1.0.0',
|
|
|
+ versionCode: '100000',
|
|
|
+ apiUrl: 'http://localhost',
|
|
|
+ tradeChannel: 'ws',
|
|
|
+ showLoginAlert: false,
|
|
|
+ modules: [],
|
|
|
+ quotationProperties: [],
|
|
|
+ forcedPasswordChange: false,
|
|
|
+ strongPassword: true,
|
|
|
+ registrationCodeRule: 0,
|
|
|
+ riskType: 0,
|
|
|
+ i18nEnabled: true,
|
|
|
+ allCloseEnabled: false,
|
|
|
+ allDeliveryEnabled: false,
|
|
|
+ metaPixelId: '',
|
|
|
+ appsFlyerId: '',
|
|
|
+ appsFlyerKey: ''
|
|
|
+ }
|
|
|
+
|
|
|
+ private serviceConfig: ServiceConfig = {
|
|
|
commSearchUrl: '',
|
|
|
- goAccess: '',
|
|
|
goCommonSearchUrl: '',
|
|
|
- hsbyBankSignZone: '',
|
|
|
- hsbyRegister: '',
|
|
|
- hsbyPayUrlWeb: '',
|
|
|
- hsbySignUp: '',
|
|
|
- mobileAuthUrl: '',
|
|
|
+ androidUpdateUrl: '',
|
|
|
+ tradeUrl: '',
|
|
|
+ quoteUrl: '',
|
|
|
+ goAccess: '',
|
|
|
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
|
|
|
+ /** 加载本地配置 */
|
|
|
+ private async loadAppConfig() {
|
|
|
+ const data = await plus.getLocalFileContent<AppConfig>('./config/appconfig.json')
|
|
|
+ Object.assign(this.appConfig, data)
|
|
|
+ return data
|
|
|
+ }
|
|
|
|
|
|
- /** 限制重试次数,0 = 无限制 */
|
|
|
- private retryLimit = 5
|
|
|
-
|
|
|
- /**
|
|
|
- * 失败时重新尝试初始化,直到成功为止
|
|
|
- */
|
|
|
- private tryinit = () => {
|
|
|
- return new Promise<typeof this.config>((resolve, reject) => {
|
|
|
- if (this.retryLimit === 0 || this.retryCount < this.retryLimit) {
|
|
|
- this.retryCount++
|
|
|
- setTimeout(() => {
|
|
|
- resolve(this.init())
|
|
|
- }, 3000)
|
|
|
+ /** 加载服务配置 */
|
|
|
+ private loadServiceConfig = (url: string, retryCount = 0) => {
|
|
|
+ return new Promise<void>((resolve, reject) => {
|
|
|
+ if (this.maxRetryCount === 0 || retryCount <= this.maxRetryCount) {
|
|
|
+ plus.httpRequest({ url }).then((res) => {
|
|
|
+ this.serviceConfig = res.data.data
|
|
|
+ this.isReady = true
|
|
|
+ this.taskQueue.run()
|
|
|
+ resolve()
|
|
|
+ }).catch(() => {
|
|
|
+ setTimeout(() => {
|
|
|
+ retryCount++
|
|
|
+ this.loadServiceConfig(url, retryCount).then(resolve).catch(reject)
|
|
|
+ }, 3000)
|
|
|
+ })
|
|
|
} else {
|
|
|
- this.retryCount = 0
|
|
|
- this.isPending = false
|
|
|
+ this.initPromise = null
|
|
|
reject('服务加载失败,请稍后再试')
|
|
|
}
|
|
|
})
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 初始化服务配置
|
|
|
- */
|
|
|
- private async init(): Promise<typeof this.config> {
|
|
|
- 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
|
|
|
- // 同步服务器时间
|
|
|
- getServerTime().then((res) => {
|
|
|
- const serverTime = moment(res.data)
|
|
|
- dateDiff.value = moment().diff(serverTime)
|
|
|
- })
|
|
|
- resolve(this.config)
|
|
|
+ async init() {
|
|
|
+ if (!this.initPromise) {
|
|
|
+ try {
|
|
|
+ await this.appConfigAsync
|
|
|
+ } catch {
|
|
|
+ this.appConfigAsync = this.loadAppConfig()
|
|
|
+ }
|
|
|
+ this.initPromise = new Promise<void>((resolve, reject) => {
|
|
|
+ this.appConfigAsync.then((data) => {
|
|
|
+ this.loadServiceConfig(data.apiUrl).then(resolve).catch(reject)
|
|
|
}).catch(() => {
|
|
|
- const result = this.tryinit()
|
|
|
- resolve(result)
|
|
|
+ this.initPromise = null
|
|
|
+ reject('配置文件加载失败,请稍后再试')
|
|
|
})
|
|
|
- }).catch(() => {
|
|
|
- this.isPending = false
|
|
|
- reject('配置文件加载失败,请稍后再试')
|
|
|
})
|
|
|
- })
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 服务初始化完成时触发
|
|
|
- */
|
|
|
- onReady() {
|
|
|
- if (!this.isReady && !this.isPending) {
|
|
|
- this.onload = this.init()
|
|
|
}
|
|
|
// 确保当前只有一个初始化实例
|
|
|
- return this.onload
|
|
|
+ return this.initPromise
|
|
|
+ }
|
|
|
+
|
|
|
+ get timestamp() {
|
|
|
+ return Date.now() - this.serverDiffTime
|
|
|
+ }
|
|
|
+
|
|
|
+ getAppConfig<K extends keyof AppConfig>(key: K) {
|
|
|
+ return this.appConfig[key]
|
|
|
+ }
|
|
|
+
|
|
|
+ getServiceConfig<K extends keyof ServiceConfig>(key: K) {
|
|
|
+ return this.serviceConfig[key]
|
|
|
+ }
|
|
|
+
|
|
|
+ hasModule = (module: AppConfig['modules'][number]) => {
|
|
|
+ return this.appConfig.modules.includes(module)
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 获取服务配置地址
|
|
|
- * @param key
|
|
|
- * @returns
|
|
|
- */
|
|
|
- getConfig<K extends keyof typeof this.config>(key: K) {
|
|
|
- return this.config[key]
|
|
|
+ hasQuotationProperty = (key: keyof Model.QuoteDayRsp) => {
|
|
|
+ return this.appConfig.quotationProperties.includes(key)
|
|
|
+ }
|
|
|
+
|
|
|
+ onReady(callback: () => void) {
|
|
|
+ const cancel = this.taskQueue.add(callback)
|
|
|
+ if (this.isReady) {
|
|
|
+ this.taskQueue.run()
|
|
|
+ }
|
|
|
+ return cancel
|
|
|
}
|
|
|
})
|