index.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. console.log('服务配置信息', res.data);
  38. this.config = res.data as any;
  39. this.isReady = true;
  40. resolve();
  41. // 连接交易服务
  42. // socket.connectTrade();
  43. },
  44. fail: () => {
  45. reject('获取服务配置地址失败');
  46. },
  47. complete: () => {
  48. this.pending = false;
  49. }
  50. })
  51. })
  52. }
  53. /**
  54. * 服务初始化完成时触发
  55. * @param callback
  56. */
  57. async onReady(callback?: () => void) {
  58. // 初始化失败时重新初始化
  59. if (!this.isReady && !this.pending) {
  60. this.tryInit = this.init();
  61. }
  62. // 确保当前只有一个初始化实例
  63. await this.tryInit.then(() => callback && callback());
  64. }
  65. })