index.ts 1.8 KB

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