index.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import axios from 'axios'
  2. import { useGlobalStore } from '@/stores/modules/global'
  3. const { getSystemConfig } = useGlobalStore()
  4. export default new (class {
  5. constructor() {
  6. this.systemInfoAsync = getSystemConfig()
  7. }
  8. systemInfoAsync
  9. /** 服务配置信息 */
  10. config = {
  11. commSearchUrl: '',
  12. goAccess: '',
  13. goCommonSearchUrl: '',
  14. hsbyBankSignZone: '',
  15. hsbyRegister: '',
  16. hsbyPayUrlWeb: '',
  17. hsbySignUp: '',
  18. mobileAuthUrl: '',
  19. mobileOpenUrl: '',
  20. newsUrl: '',
  21. openApiUrl: '',
  22. quoteHost: '',
  23. quotePort: '',
  24. quoteUrl: '',
  25. tradeHost: '',
  26. tradePort: '',
  27. tradeUrl: '',
  28. uploadUrl: '',
  29. quoteWS: '',
  30. tradeWS: '',
  31. oem: '',
  32. iOS: '',
  33. android: '',
  34. androidUpdateUrl: '',
  35. pcNewsUrl: '',
  36. pcMangerUrl: '',
  37. shareUrl: '',
  38. }
  39. /** 服务初始化完成状态 */
  40. isReady = false
  41. /** 等待服务初始化 */
  42. private isPending = false
  43. /**
  44. * 自动初始化,若断网或其它原因导致初始化失败,需重新初始化
  45. * IOS上架审核可能会遇到网络权限的情况,应用可能会在未授权网络权限的情况下发起请求,导致请求等待时间过长,最终审核被拒
  46. */
  47. //private onload = this.init()
  48. /**
  49. * 手动初始化
  50. */
  51. private onload = Promise.resolve(this.config)
  52. /** 重试次数 */
  53. private retryCount = 0
  54. /** 限制重试次数,0 = 无限制 */
  55. private retryLimit = 5
  56. /**
  57. * 失败时重新尝试初始化,直到成功为止
  58. */
  59. private tryinit = () => {
  60. return new Promise<typeof this.config>((resolve, reject) => {
  61. if (this.retryLimit === 0 || this.retryCount < this.retryLimit) {
  62. this.retryCount++
  63. setTimeout(() => {
  64. resolve(this.init())
  65. }, 3000)
  66. } else {
  67. this.retryCount = 0
  68. this.isPending = false
  69. reject('服务加载失败,请稍后再试')
  70. }
  71. })
  72. }
  73. /**
  74. * 初始化服务配置
  75. */
  76. private async init(): Promise<typeof this.config> {
  77. this.isPending = true
  78. await this.systemInfoAsync.catch(() => {
  79. this.systemInfoAsync = getSystemConfig()
  80. })
  81. return new Promise((resolve, reject) => {
  82. this.systemInfoAsync.then((res) => {
  83. // 获取服务接口地址
  84. axios(res.apiUrl).then((res) => {
  85. this.config = res.data.data
  86. this.isReady = true
  87. resolve(this.config)
  88. }).catch(() => {
  89. const result = this.tryinit()
  90. resolve(result)
  91. })
  92. }).catch(() => {
  93. reject('配置文件加载失败,请稍后再试')
  94. })
  95. })
  96. }
  97. /**
  98. * 服务初始化完成时触发
  99. */
  100. onReady() {
  101. if (!this.isReady && !this.isPending) {
  102. this.onload = this.init()
  103. }
  104. // 确保当前只有一个初始化实例
  105. return this.onload
  106. }
  107. /**
  108. * 获取服务配置地址
  109. * @param key
  110. * @returns
  111. */
  112. getConfig<K extends keyof typeof this.config>(key: K) {
  113. return this.config[key]
  114. }
  115. })