Helper.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import { sys } from 'cc';
  2. import { ANDROID, IOS } from 'cc/env';
  3. const STORAGE_ENCRYPTION_HEAD = 'a6072637';
  4. const STORAGE_ENCRYPTION_PASSWORD = 'xbd4jl';
  5. const STORAGE_ENCRYPTION_ENABLED = true;
  6. /**
  7. * 将数字格式化为财务展示格式:使用千位分隔符并保留两位小数。
  8. * 例如:1234567.8 -> 1,234,567.80。
  9. */
  10. export function formatCurrency(value: number, decimal = 0): string {
  11. if (!Number.isFinite(value)) value = 0;
  12. return value.toLocaleString('en-US', {
  13. minimumFractionDigits: decimal,
  14. maximumFractionDigits: decimal,
  15. useGrouping: true,
  16. });
  17. }
  18. export function formatSCNum(value: number) {
  19. return formatCurrency(value, 2);
  20. }
  21. export function formatGCNum(value: number) {
  22. return formatCurrency(value, 0);
  23. }
  24. /**
  25. * 读取本地数据,并兼容 game_web_us2 写入的加密格式。
  26. */
  27. export function localStorageGet<T = string>(key: string, defaultValue: T = '' as T): string | T {
  28. let value = sys.localStorage.getItem(key);
  29. if (value === null) return defaultValue;
  30. if (value.startsWith(STORAGE_ENCRYPTION_HEAD)) {
  31. try {
  32. value = decrypt(value.substring(STORAGE_ENCRYPTION_HEAD.length));
  33. } catch {
  34. // 与原项目保持一致:解密失败时返回空字符串。
  35. value = '';
  36. }
  37. }
  38. return value;
  39. }
  40. /**
  41. * 加密并保存本地数据。空字符串不加密,行为与 game_web_us2 保持一致。
  42. */
  43. export function localStorageSet(key: string, value: unknown): void {
  44. let storageValue = value;
  45. if (STORAGE_ENCRYPTION_ENABLED && storageValue !== null && storageValue !== '') {
  46. const plainValue = typeof storageValue === 'string' ? storageValue : String(storageValue);
  47. storageValue = plainValue;
  48. try {
  49. storageValue = STORAGE_ENCRYPTION_HEAD + encrypt(plainValue);
  50. } catch {
  51. // 加密失败时沿用原值保存,兼容原项目行为。
  52. }
  53. }
  54. sys.localStorage.setItem(key, String(storageValue));
  55. }
  56. export function localStorageRemove(key: string): void {
  57. sys.localStorage.removeItem(key);
  58. }
  59. function encrypt(value: string, password = STORAGE_ENCRYPTION_PASSWORD): string {
  60. if (value === '') return '';
  61. const source = encodeURIComponent(value);
  62. const encodedPassword = encodeURIComponent(password || STORAGE_ENCRYPTION_PASSWORD);
  63. if (encodedPassword.length === 0) return '';
  64. let randomSeed = '';
  65. for (let index = 0; index < encodedPassword.length; index += 1) {
  66. randomSeed += encodedPassword.charCodeAt(index).toString();
  67. }
  68. const seedPosition = Math.floor(randomSeed.length / 5);
  69. const multiplier = Number.parseInt(
  70. randomSeed.charAt(seedPosition)
  71. + randomSeed.charAt(seedPosition * 2)
  72. + randomSeed.charAt(seedPosition * 3)
  73. + randomSeed.charAt(seedPosition * 4)
  74. + randomSeed.charAt(seedPosition * 5),
  75. 10,
  76. );
  77. const increment = Math.ceil(encodedPassword.length / 2);
  78. const modulus = Math.pow(2, 31) - 1;
  79. if (multiplier < 2) return '';
  80. let salt = Math.round(Math.random() * 1_000_000_000) % 100_000_000;
  81. randomSeed += salt;
  82. randomSeed = reduceSeed(randomSeed);
  83. let pseudoRandom = (multiplier * Number(randomSeed) + increment) % modulus;
  84. let encrypted = '';
  85. for (let index = 0; index < source.length; index += 1) {
  86. const encryptedChar = source.charCodeAt(index) ^ Math.floor((pseudoRandom / modulus) * 255);
  87. encrypted += encryptedChar < 16
  88. ? `0${encryptedChar.toString(16)}`
  89. : encryptedChar.toString(16);
  90. pseudoRandom = (multiplier * pseudoRandom + increment) % modulus;
  91. }
  92. let saltHex = salt.toString(16);
  93. while (saltHex.length < 8) saltHex = `0${saltHex}`;
  94. return encrypted + saltHex;
  95. }
  96. function decrypt(value: string, password = STORAGE_ENCRYPTION_PASSWORD): string {
  97. if (value === '') return '';
  98. const encodedPassword = encodeURIComponent(password || STORAGE_ENCRYPTION_PASSWORD);
  99. if (value.length < 8 || encodedPassword.length === 0) return '';
  100. let randomSeed = '';
  101. for (let index = 0; index < encodedPassword.length; index += 1) {
  102. randomSeed += encodedPassword.charCodeAt(index).toString();
  103. }
  104. const seedPosition = Math.floor(randomSeed.length / 5);
  105. const multiplier = Number.parseInt(
  106. randomSeed.charAt(seedPosition)
  107. + randomSeed.charAt(seedPosition * 2)
  108. + randomSeed.charAt(seedPosition * 3)
  109. + randomSeed.charAt(seedPosition * 4)
  110. + randomSeed.charAt(seedPosition * 5),
  111. 10,
  112. );
  113. const increment = Math.round(encodedPassword.length / 2);
  114. const modulus = Math.pow(2, 31) - 1;
  115. const salt = Number.parseInt(value.substring(value.length - 8), 16);
  116. const encrypted = value.substring(0, value.length - 8);
  117. randomSeed += salt;
  118. randomSeed = reduceSeed(randomSeed);
  119. let pseudoRandom = (multiplier * Number(randomSeed) + increment) % modulus;
  120. let decrypted = '';
  121. for (let index = 0; index < encrypted.length; index += 2) {
  122. const decryptedChar = Number.parseInt(encrypted.substring(index, index + 2), 16)
  123. ^ Math.floor((pseudoRandom / modulus) * 255);
  124. decrypted += String.fromCharCode(decryptedChar);
  125. pseudoRandom = (multiplier * pseudoRandom + increment) % modulus;
  126. }
  127. return decodeURIComponent(decrypted);
  128. }
  129. function reduceSeed(seed: string): string {
  130. let result = seed;
  131. while (result.length > 10) {
  132. let remaining = result;
  133. let total = 0;
  134. while (remaining.length > 0) {
  135. total += Number.parseInt(remaining.substring(0, 10), 10);
  136. remaining = remaining.substring(10);
  137. }
  138. result = total.toString();
  139. }
  140. return result;
  141. }
  142. /**
  143. * 进入平台判定:0=普通浏览器 1=iOS壳包 2=Android壳包 3=PWA(桌面图标/TWA 进入)
  144. * 用于按来源差异化展示(如提现页官网引流提示:桌面图标进入的玩家不显示)。
  145. * 壳包判定优先于 PWA;TWA 判定内联实现,避免 helpers→UAUtils 循环依赖。
  146. */
  147. export function getEnterPlatform(): 0 | 1 | 2 | 3 {
  148. if (ANDROID) return 2;
  149. if (IOS) return 1;
  150. return 0;
  151. }
  152. export function createUUID() {
  153. const chars: string[] = [];
  154. const hexDigits = '0123456789abcdef';
  155. for (let index = 0; index < 36; index += 1) {
  156. chars[index] = hexDigits.charAt(
  157. Math.floor(Math.random() * 16),
  158. );
  159. }
  160. // UUID version 4:0100
  161. chars[14] = '4';
  162. // RFC variant:10xx
  163. const variant = (Number.parseInt(chars[19], 16) & 0x3) | 0x8;
  164. chars[19] = hexDigits.charAt(variant);
  165. chars[8] = '-';
  166. chars[13] = '-';
  167. chars[18] = '-';
  168. chars[23] = '-';
  169. return chars.join('');
  170. }
  171. export function getUrlParams(url: string) {
  172. const queryIndex = url.indexOf('?');
  173. const params = {};
  174. if (queryIndex >= 0) {
  175. url.slice(queryIndex + 1).split('&').forEach((entry) => {
  176. if (!entry) return;
  177. const separatorIndex = entry.indexOf('=');
  178. const rawKey = separatorIndex >= 0 ? entry.slice(0, separatorIndex) : entry;
  179. const rawValue = separatorIndex >= 0 ? entry.slice(separatorIndex + 1) : '';
  180. try {
  181. const key = decodeURIComponent(rawKey.replace(/\+/g, ' '));
  182. params[key] = decodeURIComponent(rawValue.replace(/\+/g, ' '));
  183. } catch {
  184. // Preserve malformed values instead of crashing the native callback.
  185. params[rawKey] = rawValue;
  186. }
  187. });
  188. }
  189. return params;
  190. }