index.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { getDomainParts, setDomainParts, buildHostUrls } from './domainState'
  2. import { DOMAIN_CACHE_KEY } from './domainConstants'
  3. declare const uni: any
  4. /** 模块加载时同步恢复缓存(含过期缓存),避免首屏走默认域名 */
  5. function loadCachedDomainSync() {
  6. try {
  7. const raw = uni.getStorageSync(DOMAIN_CACHE_KEY)
  8. if (!raw) return
  9. const cache = typeof raw === 'string' ? JSON.parse(raw) : raw
  10. if (cache?.ho && cache?.dt) {
  11. setDomainParts({ ho: cache.ho, dt: cache.dt, ht: cache.ht || 'https:' })
  12. }
  13. } catch (e) {
  14. console.warn('[dynamicDomain] 同步恢复缓存失败', e)
  15. }
  16. }
  17. // #ifdef H5
  18. const [p, h] = [window.location.protocol, window.location.host]
  19. const isIP = /^\d+\.\d+\.\d+\.\d+:\d+$/.test(h)
  20. const [h5Ho, h5Dt] = isIP ? ['44a5c8109e4', 'com'] : h.split('.').slice(-2)
  21. const h5Ht = p == 'http:' ? 'https:' : p
  22. setDomainParts({ ho: h5Ho, dt: h5Dt, ht: h5Ht })
  23. // #endif
  24. // #ifdef APP-PLUS
  25. loadCachedDomainSync()
  26. // #endif
  27. const staticConfig = {
  28. Code: {
  29. StatusOK: 200,
  30. StatusFail: 400,
  31. StatusSessionExpire: 600,
  32. StatusSNotFound: 404,
  33. },
  34. Pattern: {
  35. Email: /^[\w.%+-]+@[a-z0-9.-]+\.[a-z]{2,}$/i,
  36. Phone: /^1[3-9]\d{9}$/,
  37. Pin: /^(?!(\d)\1{5})(?!012345)(?!123456)(?!234567)(?!345678)(?!456789)(?!987654)(?!876543)(?!765432)(?!654321)(?!543210)\d{6}$/,
  38. Password: /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d!@#$%^&*()_+\-=\[\]{}|;:,.<>?/~]{8,16}$/,
  39. Tel: /^0[1-9]{2,3}-\d{5,10}$/,
  40. Num: /\d/,
  41. NonNegInt: /^\d+$/, // 非负整数
  42. PosInt: /^[1-9]\d*$/, // 正整数
  43. nonnegative: /^\d+(\.\d{1,2})?$/, // 非负数(最多两位小数)
  44. englishName: /^[^\u4E00-\u9FA5]+$/,
  45. },
  46. }
  47. const hostKeys = [
  48. 'HostWs',
  49. 'Host80',
  50. 'Host00',
  51. 'Host85',
  52. 'Host04',
  53. 'Host90',
  54. 'HostShop',
  55. 'HostShopImg',
  56. 'Host87',
  57. 'Host05',
  58. 'HostEnter',
  59. ] as const
  60. type HostKey = (typeof hostKeys)[number]
  61. const config: Record<string, unknown> = {
  62. ...staticConfig,
  63. get ho() {
  64. return getDomainParts().ho
  65. },
  66. get host() {
  67. return getDomainParts().ho
  68. },
  69. }
  70. hostKeys.forEach((key) => {
  71. Object.defineProperty(config, key, {
  72. enumerable: true,
  73. configurable: true,
  74. get() {
  75. return buildHostUrls()[key as HostKey]
  76. },
  77. })
  78. })
  79. export default config as typeof staticConfig & Record<HostKey, string> & {
  80. ho: string
  81. host: string
  82. }