region.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. * 区域配置统一访问层
  3. *
  4. * 数据源:public/js/load.m.js 中的 `regions` 对象
  5. * 在 bundle 执行前,load.m.js 已将当前站点的区域配置挂到
  6. * window.themeConfig.regionConfig
  7. * window.themeConfig.lang
  8. * window.themeConfig.api
  9. * window.themeConfig.ws
  10. *
  11. * 本模块封装所有对 regionConfig 的读取,避免业务代码中重复写
  12. * window.themeConfig?.regionConfig?.xxx || fallback
  13. *
  14. * 所有业务代码应通过本模块的 helper 获取区域信息。
  15. */
  16. // ============ 兜底默认值(与 load.m.js 中 regions.us 对齐) ============
  17. // `code` 暂时保留内部兼容值 us,面向用户的地区配置均为英国。
  18. const FALLBACK_REGION = {
  19. code: "us",
  20. countryCode: "GB",
  21. locale: "en-GB",
  22. languages: ["en-GB"],
  23. defaultLang: "en-GB",
  24. api: "https://api1.usvip.org/",
  25. apiHost: "api1.usvip.org",
  26. ws: "wss://api1.usvip.org:10000",
  27. wsPort: 10000,
  28. phoneCode: "+44",
  29. currency: "GBP",
  30. currencySymbol: "£",
  31. timezone: "Europe/London",
  32. dateFormat: "DD/MM/YYYY",
  33. numberFormat: "en-GB",
  34. titleSuffix: "Online Slots & Real Money Games"
  35. };
  36. // ============ 核心读取 ============
  37. /** 获取当前 themeConfig(可能为 undefined,发生在极早期或非浏览器环境) */
  38. export const getThemeConfig = () =>
  39. (typeof window !== "undefined" && window.themeConfig) || null;
  40. /** 获取完整 regionConfig(带兜底) */
  41. export const getRegion = () => {
  42. const tc = getThemeConfig();
  43. return (tc && tc.regionConfig) || FALLBACK_REGION;
  44. };
  45. // ============ 字段级 helper ============
  46. /** 区域 code:us / br / mx / ru / eu ... */
  47. export const getRegionCode = () => getRegion().code || FALLBACK_REGION.code;
  48. /** 国家 ISO 码:GB / BR / MX ... */
  49. export const getCountryCode = () => getRegion().countryCode || FALLBACK_REGION.countryCode;
  50. /** 完整 locale:en-GB / pt-BR ... */
  51. export const getLocale = () => getRegion().locale || FALLBACK_REGION.locale;
  52. /** 区域可用语言列表 */
  53. export const getRegionLangs = () => getRegion().languages || FALLBACK_REGION.languages;
  54. /** 区域默认语言 */
  55. export const getRegionDefaultLang = () => getRegion().defaultLang || FALLBACK_REGION.defaultLang;
  56. /** 当前站点实际生效语言(优先 themeConfig.lang,其次 region.defaultLang) */
  57. export const getSiteLang = () => {
  58. const tc = getThemeConfig();
  59. return (tc && tc.lang) || getRegionDefaultLang();
  60. };
  61. /** HTTP API base URL(优先 themeConfig.api) */
  62. export const getRegionApi = () => {
  63. const tc = getThemeConfig();
  64. return (tc && tc.api) || getRegion().api || FALLBACK_REGION.api;
  65. };
  66. /** WebSocket base URL */
  67. export const getRegionWs = () => {
  68. const tc = getThemeConfig();
  69. return (tc && tc.ws) || getRegion().ws || FALLBACK_REGION.ws;
  70. };
  71. /** 电话区号:+44 / +55 ... */
  72. export const getPhoneCode = () => getRegion().phoneCode || FALLBACK_REGION.phoneCode;
  73. /** 货币 ISO:GBP / BRL ... */
  74. export const getCurrency = () => getRegion().currency || FALLBACK_REGION.currency;
  75. /** 货币符号:$ / R$ ... */
  76. export const getCurrencySymbol = () => getRegion().currencySymbol || FALLBACK_REGION.currencySymbol;
  77. /** 时区:America/Sao_Paulo ... */
  78. export const getTimezone = () => getRegion().timezone || FALLBACK_REGION.timezone;
  79. /** 页面标题后缀 */
  80. export const getTitleSuffix = () => getRegion().titleSuffix || FALLBACK_REGION.titleSuffix;
  81. /** 日期格式 */
  82. export const getDateFormat = () => getRegion().dateFormat || FALLBACK_REGION.dateFormat;
  83. /** 数字格式化 locale */
  84. export const getNumberFormat = () => getRegion().numberFormat || FALLBACK_REGION.numberFormat;
  85. // ============ 便捷断言 ============
  86. /** 是否为指定区域(不区分大小写) */
  87. export const isRegion = (code) => getRegionCode().toLowerCase() === String(code || "").toLowerCase();
  88. /** 是否为指定国家(不区分大小写) */
  89. export const isCountry = (iso) => getCountryCode().toLowerCase() === String(iso || "").toLowerCase();
  90. const regionUtils = {
  91. FALLBACK_REGION,
  92. getThemeConfig,
  93. getRegion,
  94. getRegionCode,
  95. getCountryCode,
  96. getLocale,
  97. getRegionLangs,
  98. getRegionDefaultLang,
  99. getSiteLang,
  100. getRegionApi,
  101. getRegionWs,
  102. getPhoneCode,
  103. getCurrency,
  104. getCurrencySymbol,
  105. getTimezone,
  106. getTitleSuffix,
  107. getDateFormat,
  108. getNumberFormat,
  109. isRegion,
  110. isCountry
  111. };
  112. export default regionUtils;