| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- /**
- * 区域配置统一访问层
- *
- * 数据源:public/js/load.m.js 中的 `regions` 对象
- * 在 bundle 执行前,load.m.js 已将当前站点的区域配置挂到
- * window.themeConfig.regionConfig
- * window.themeConfig.lang
- * window.themeConfig.api
- * window.themeConfig.ws
- *
- * 本模块封装所有对 regionConfig 的读取,避免业务代码中重复写
- * window.themeConfig?.regionConfig?.xxx || fallback
- *
- * 所有业务代码应通过本模块的 helper 获取区域信息。
- */
- // ============ 兜底默认值(与 load.m.js 中 regions.us 对齐) ============
- // `code` 暂时保留内部兼容值 us,面向用户的地区配置均为英国。
- const FALLBACK_REGION = {
- code: "us",
- countryCode: "GB",
- locale: "en-GB",
- languages: ["en-GB"],
- defaultLang: "en-GB",
- api: "https://api1.usvip.org/",
- apiHost: "api1.usvip.org",
- ws: "wss://api1.usvip.org:10000",
- wsPort: 10000,
- phoneCode: "+44",
- currency: "GBP",
- currencySymbol: "£",
- timezone: "Europe/London",
- dateFormat: "DD/MM/YYYY",
- numberFormat: "en-GB",
- titleSuffix: "Online Slots & Real Money Games"
- };
- // ============ 核心读取 ============
- /** 获取当前 themeConfig(可能为 undefined,发生在极早期或非浏览器环境) */
- export const getThemeConfig = () =>
- (typeof window !== "undefined" && window.themeConfig) || null;
- /** 获取完整 regionConfig(带兜底) */
- export const getRegion = () => {
- const tc = getThemeConfig();
- return (tc && tc.regionConfig) || FALLBACK_REGION;
- };
- // ============ 字段级 helper ============
- /** 区域 code:us / br / mx / ru / eu ... */
- export const getRegionCode = () => getRegion().code || FALLBACK_REGION.code;
- /** 国家 ISO 码:GB / BR / MX ... */
- export const getCountryCode = () => getRegion().countryCode || FALLBACK_REGION.countryCode;
- /** 完整 locale:en-GB / pt-BR ... */
- export const getLocale = () => getRegion().locale || FALLBACK_REGION.locale;
- /** 区域可用语言列表 */
- export const getRegionLangs = () => getRegion().languages || FALLBACK_REGION.languages;
- /** 区域默认语言 */
- export const getRegionDefaultLang = () => getRegion().defaultLang || FALLBACK_REGION.defaultLang;
- /** 当前站点实际生效语言(优先 themeConfig.lang,其次 region.defaultLang) */
- export const getSiteLang = () => {
- const tc = getThemeConfig();
- return (tc && tc.lang) || getRegionDefaultLang();
- };
- /** HTTP API base URL(优先 themeConfig.api) */
- export const getRegionApi = () => {
- const tc = getThemeConfig();
- return (tc && tc.api) || getRegion().api || FALLBACK_REGION.api;
- };
- /** WebSocket base URL */
- export const getRegionWs = () => {
- const tc = getThemeConfig();
- return (tc && tc.ws) || getRegion().ws || FALLBACK_REGION.ws;
- };
- /** 电话区号:+44 / +55 ... */
- export const getPhoneCode = () => getRegion().phoneCode || FALLBACK_REGION.phoneCode;
- /** 货币 ISO:GBP / BRL ... */
- export const getCurrency = () => getRegion().currency || FALLBACK_REGION.currency;
- /** 货币符号:$ / R$ ... */
- export const getCurrencySymbol = () => getRegion().currencySymbol || FALLBACK_REGION.currencySymbol;
- /** 时区:America/Sao_Paulo ... */
- export const getTimezone = () => getRegion().timezone || FALLBACK_REGION.timezone;
- /** 页面标题后缀 */
- export const getTitleSuffix = () => getRegion().titleSuffix || FALLBACK_REGION.titleSuffix;
- /** 日期格式 */
- export const getDateFormat = () => getRegion().dateFormat || FALLBACK_REGION.dateFormat;
- /** 数字格式化 locale */
- export const getNumberFormat = () => getRegion().numberFormat || FALLBACK_REGION.numberFormat;
- // ============ 便捷断言 ============
- /** 是否为指定区域(不区分大小写) */
- export const isRegion = (code) => getRegionCode().toLowerCase() === String(code || "").toLowerCase();
- /** 是否为指定国家(不区分大小写) */
- export const isCountry = (iso) => getCountryCode().toLowerCase() === String(iso || "").toLowerCase();
- const regionUtils = {
- FALLBACK_REGION,
- getThemeConfig,
- getRegion,
- getRegionCode,
- getCountryCode,
- getLocale,
- getRegionLangs,
- getRegionDefaultLang,
- getSiteLang,
- getRegionApi,
- getRegionWs,
- getPhoneCode,
- getCurrency,
- getCurrencySymbol,
- getTimezone,
- getTitleSuffix,
- getDateFormat,
- getNumberFormat,
- isRegion,
- isCountry
- };
- export default regionUtils;
|