index.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { appConfig } from '../config/index'
  2. import { httpRequest } from './http/index'
  3. export default new (class {
  4. /** 服务配置信息 */
  5. config = {
  6. commSearchUrl: '',
  7. goCommonSearchUrl: '',
  8. mobileAuthUrl: '',
  9. mobileOpenUrl: '',
  10. newsUrl: '',
  11. openApiUrl: '',
  12. quoteHost: '',
  13. quotePort: '',
  14. quoteUrl: '',
  15. tradeHost: '',
  16. tradePort: '',
  17. tradeUrl: '',
  18. uploadUrl: '',
  19. goAccess: ''
  20. }
  21. /** 服务初始化状态 */
  22. isReady = false;
  23. /** 等待服务初始化 */
  24. private pending = true;
  25. /**
  26. * 尝试自动初始化,若断网或其它原因导致初始化失败,需手动初始化
  27. */
  28. private tryInit = this.init();
  29. /**
  30. * 初始化服务配置
  31. */
  32. private init(): Promise<void> {
  33. return new Promise((resolve, reject) => {
  34. // 获取服务接口地址
  35. httpRequest(appConfig.apiUrl, 'GET', {
  36. success: (res) => {
  37. this.config = res.data as any;
  38. this.isReady = true;
  39. resolve();
  40. // 连接交易服务
  41. // socket.connectTrade();
  42. },
  43. fail: () => {
  44. reject('获取服务配置地址失败');
  45. },
  46. complete: () => {
  47. this.pending = false;
  48. }
  49. })
  50. })
  51. }
  52. /**
  53. * 服务初始化完成时触发
  54. * @param callback
  55. */
  56. async onReady(callback?: () => void) {
  57. // 初始化失败时重新初始化
  58. if (!this.isReady && !this.pending) {
  59. this.tryInit = this.init();
  60. }
  61. // 确保当前只有一个初始化实例
  62. await this.tryInit.then(() => callback && callback());
  63. }
  64. })