index.ts 4.0 KB

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