index.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. oem: '',
  20. goAccess: ''
  21. }
  22. /** 服务初始化状态 */
  23. isReady = false;
  24. /** 等待服务初始化 */
  25. private pending = true;
  26. /**
  27. * 尝试自动初始化,若断网或其它原因导致初始化失败,需手动初始化
  28. */
  29. private tryInit = this.init();
  30. /**
  31. * 初始化服务配置
  32. */
  33. private init(): Promise<void> {
  34. return new Promise((resolve, reject) => {
  35. // 获取服务接口地址
  36. httpRequest(appConfig.apiUrl, 'GET', {
  37. success: (res) => {
  38. console.log('服务配置信息', res.data);
  39. this.config = res.data as any;
  40. this.isReady = true;
  41. resolve();
  42. // 连接交易服务
  43. // socket.connectTrade();
  44. },
  45. fail: () => {
  46. reject('获取服务配置地址失败');
  47. },
  48. complete: () => {
  49. this.pending = false;
  50. }
  51. })
  52. })
  53. }
  54. /**
  55. * 服务初始化完成时触发
  56. * @param callback
  57. */
  58. async onReady(callback?: () => void) {
  59. // 初始化失败时重新初始化
  60. if (!this.isReady && !this.pending) {
  61. this.tryInit = this.init();
  62. }
  63. // 确保当前只有一个初始化实例
  64. await this.tryInit.then(() => callback && callback());
  65. }
  66. })