globalData.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * 全局运行时数据
  3. */
  4. export const GLOBAL_DATA = {
  5. filtedLeagues: [],
  6. filtedGames: [],
  7. gamesMap: {},
  8. straightFixturesVersion: 0,
  9. straightFixturesCount: 0,
  10. specialFixturesVersion: 0,
  11. specialFixturesCount: 0,
  12. straightOddsVersion: 0,
  13. // straightOddsCount: 0,
  14. specialsOddsVersion: 0,
  15. // specialsOddsCount: 0,
  16. currencies: [],
  17. currenciesUpdatedAt: 0,
  18. requestErrorCount: 0,
  19. loopActive: false,
  20. loopResultTime: 0,
  21. };
  22. export const getCurrenciesInfo = () => {
  23. const { currencies, currenciesUpdatedAt } = GLOBAL_DATA;
  24. if (!currenciesUpdatedAt) {
  25. return Promise.reject({
  26. cause: 503,
  27. message: 'currencies data is not ready',
  28. data: { currenciesUpdatedAt },
  29. });
  30. }
  31. return Promise.resolve({ data: currencies, updatedAt: currenciesUpdatedAt });
  32. }
  33. export const findCurrencyInfo = (currencyCode) => {
  34. const code = currencyCode?.toUpperCase();
  35. const currencies = GLOBAL_DATA.currencies?.currencies ?? GLOBAL_DATA.currencies;
  36. if (!code || !Array.isArray(currencies)) {
  37. return null;
  38. }
  39. return currencies.find(currency => currency.code?.toUpperCase() === code) ?? null;
  40. }
  41. const normalizeIds = (ids) => {
  42. if (ids === undefined || ids === null || ids === '') {
  43. return [];
  44. }
  45. if (Array.isArray(ids)) {
  46. return ids.flatMap(normalizeIds);
  47. }
  48. return String(ids).split(',').map(id => id.trim()).filter(Boolean);
  49. }
  50. export const getEventsByIds = (ids) => {
  51. const normalizedIds = normalizeIds(ids);
  52. const { gamesMap={} } = GLOBAL_DATA;
  53. if (!normalizedIds.length) {
  54. return Object.values(gamesMap);
  55. }
  56. return normalizedIds.map(id => gamesMap[id]).filter(Boolean);
  57. }
  58. /**
  59. * 获取盘口详情
  60. * @param {*} ior
  61. * @param {*} id
  62. * @returns
  63. */
  64. export const getIorInfo = async(ior, id) => {
  65. if (!id || !ior) {
  66. return Promise.reject({ cause: 400, message: 'id and ior are required', data: { id, ior } });
  67. }
  68. const { gamesMap } = GLOBAL_DATA;
  69. const iorInfo = parseIorDetail(ior, id, gamesMap);
  70. if (iorInfo.cause === 400) {
  71. return Promise.reject({ cause: 400, message: iorInfo.message, data: { id, ior } });
  72. }
  73. return Promise.resolve(iorInfo);
  74. }