storage.html 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>cookie</title>
  7. <script>
  8. //简单加密
  9. const encrypt = (str, pwd) => {
  10. if (str == '') {
  11. return '';
  12. }
  13. str = encodeURIComponent(str);
  14. if (!pwd || pwd == '') {
  15. pwd = 'xbd4jl';
  16. }
  17. pwd = encodeURIComponent(pwd);
  18. if (pwd == '' || pwd.length <= 0) {
  19. return '';
  20. }
  21. var prand = '';
  22. for (var i = 0, len = pwd.length; i < len; i += 1) {
  23. prand += pwd.charCodeAt(i).toString();
  24. }
  25. var sPos = Math.floor(prand.length / 5);
  26. var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos * 2) + prand.charAt(sPos * 3) +
  27. prand.charAt(sPos * 4) + prand.charAt(sPos * 5));
  28. var incr = Math.ceil(pwd.length / 2);
  29. var modu = Math.pow(2, 31) - 1;
  30. if (mult < 2) {
  31. return '';
  32. }
  33. var salt = Math.round(Math.random() * 1000000000) % 100000000;
  34. prand += salt;
  35. while (prand.length > 10) {
  36. let left = prand;
  37. let num = 0;
  38. while (left.length > 0) {
  39. let temp = parseInt(left.substring(0, 10));
  40. num = num + temp;
  41. left = left.substring(10, left.length);
  42. }
  43. prand = num.toString();
  44. }
  45. prand = (mult * prand + incr) % modu;
  46. var encChr = '';
  47. var encStr = '';
  48. for (var i = 0, len = str.length; i < len; i += 1) {
  49. encChr = parseInt(str.charCodeAt(i) ^ Math.floor((prand / modu) * 255));
  50. if (encChr < 16) {
  51. encStr += '0' + encChr.toString(16);
  52. } else {
  53. encStr += encChr.toString(16);
  54. }
  55. prand = (mult * prand + incr) % modu;
  56. }
  57. salt = salt.toString(16);
  58. while (salt.length < 8) {
  59. salt = "0" + salt;
  60. }
  61. encStr += salt;
  62. return encStr;
  63. }
  64. //简单解密
  65. const decrypt = (str, pwd) => {
  66. if (str == '') {
  67. return '';
  68. }
  69. if (!pwd || pwd == '') {
  70. pwd = 'xbd4jl';
  71. }
  72. pwd = encodeURIComponent(pwd);
  73. if (str == undefined || str.length < 8) {
  74. return '';
  75. }
  76. if (pwd == undefined || pwd.length <= 0) {
  77. return '';
  78. }
  79. var prand = '';
  80. for (var i = 0, len = pwd.length; i < len; i += 1) {
  81. prand += pwd.charCodeAt(i).toString();
  82. }
  83. var sPos = Math.floor(prand.length / 5);
  84. // console.log(prand.length+"sPos____"+sPos+"prand"+prand);
  85. var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos * 2) + prand.charAt(sPos * 3) +
  86. prand.charAt(sPos * 4) + prand.charAt(sPos * 5));
  87. var incr = Math.round(pwd.length / 2);
  88. var modu = Math.pow(2, 31) - 1;
  89. var salt = parseInt(str.substring(str.length - 8, str.length), 16);
  90. str = str.substring(0, str.length - 8);
  91. // console.log("prand"+prand);
  92. prand += salt;
  93. // console.log("prand"+prand);
  94. while (prand.length > 10) {
  95. let left = prand;
  96. let num = 0;
  97. while (left.length > 0) {
  98. let temp = parseInt(left.substring(0, 10));
  99. num = num + temp;
  100. left = left.substring(10, left.length);
  101. }
  102. prand = num.toString();
  103. }
  104. // console.log("prand"+prand);
  105. prand = (mult * prand + incr) % modu;
  106. var encChr = '';
  107. var encStr = '';
  108. for (var i = 0, len = str.length; i < len; i += 2) {
  109. encChr = parseInt(parseInt(str.substring(i, i + 2), 16) ^ Math.floor((prand / modu) * 255));
  110. encStr += String.fromCharCode(encChr);
  111. prand = (mult * prand + incr) % modu;
  112. }
  113. return decodeURIComponent(encStr);
  114. }
  115. const LOCAL_VISTOR_ID = "LOCAL_VISTOR_ID";
  116. const head = "a6072637";
  117. const isenc = true;
  118. const localStorageGet = (key, defaultValue = "") => {
  119. let value = localStorage.getItem(key);
  120. if (value == null) value = defaultValue;
  121. else {
  122. if (value.startsWith(head)) {
  123. try {
  124. value = decrypt(value.substring(head.length));
  125. } catch (err) {
  126. //如果出错返回空
  127. value = "";
  128. }
  129. }
  130. }
  131. return value;
  132. }
  133. const localStorageSet = (key, value) => {
  134. if (isenc) {
  135. if (value != null && value !== "") {
  136. if (typeof value !== "string") value = value.toString();
  137. try {
  138. value = head + encrypt(value);
  139. } catch (err) { }
  140. }
  141. }
  142. localStorage.setItem(key, value);
  143. }
  144. /**
  145. * 生成 UUID v7 (基于时间排序)
  146. * 格式: 48位时间戳 | 4位版本(7) | 12位随机数 | 2位变体(10) | 62位随机数
  147. */
  148. function generateUUIDv7() {
  149. const bytes = new Uint8Array(16);
  150. // 1. 填充随机数 (16 字节)
  151. crypto.getRandomValues(bytes);
  152. // 2. 写入 48 位毫秒时间戳 (大端序)
  153. const now = Date.now();
  154. bytes[0] = (now / 0x10000000000) & 0xff;
  155. bytes[1] = (now / 0x100000000) & 0xff;
  156. bytes[2] = (now / 0x1000000) & 0xff;
  157. bytes[3] = (now / 0x10000) & 0xff;
  158. bytes[4] = (now / 0x100) & 0xff;
  159. bytes[5] = now & 0xff;
  160. // 3. 设置版本号 (Version 7)
  161. // 清除前 4 位并设为 0111 (0x70)
  162. bytes[6] = (bytes[6] & 0x0f) | 0x70;
  163. // 4. 设置变体号 (Variant 10xx)
  164. // 清除前 2 位并设为 10 (0x80)
  165. bytes[8] = (bytes[8] & 0x3f) | 0x80;
  166. // 5. 转换为 8-4-4-4-12 字符串格式
  167. return Array.from(bytes)
  168. .map((b, i) => {
  169. let s = b.toString(16).padStart(2, '0');
  170. // 在特定位置添加连字符
  171. if (i === 3 || i === 5 || i === 7 || i === 9) return s + '-';
  172. return s;
  173. })
  174. .join('');
  175. }
  176. function getRandomNumber(min, max) {
  177. return Math.floor(Math.random() * (max - min + 1)) + min;
  178. }
  179. window.hasFixexpire = false;
  180. function getCookie(key) {
  181. const cookie = document.cookie;
  182. const cookieArr = cookie.split(';');
  183. const cookieObj = {};
  184. cookieArr.forEach(item => {
  185. const [k, v] = item.split('=');
  186. const trimmedKey = k ? k.trim() : '';
  187. const trimmedValue = v ? v.trim() : '';
  188. if (trimmedKey && trimmedValue)
  189. cookieObj[trimmedKey] = trimmedValue;
  190. });
  191. if (!window.hasFixexpire) {
  192. if (cookieObj['winus_uuid']) {
  193. setCookie('winus_uuid', cookieObj['winus_uuid']);
  194. }
  195. if (cookieObj['us_h5_sourceurl_key']) {
  196. setCookie('us_h5_sourceurl_key', cookieObj['us_h5_sourceurl_key']);
  197. }
  198. window.hasFixexpire = true;
  199. }
  200. return key ? cookieObj[key] : cookieObj;
  201. };
  202. function setCookie(key, value, expires = 86400 * 1000) {
  203. const date = new Date();
  204. date.setTime(date.getTime() + expires);
  205. // document.cookie = `${key}=${value};path=/;domain=.${window.location.hostname.split('.').slice(-2).join('.')};expires=${date.toUTCString()};SameSite=None;Secure;`;
  206. document.cookie = `${key}=${value};path=/;domain=${window.location.hostname};expires=${date.toUTCString()};SameSite=None;Secure;`;
  207. };
  208. let bfp = localStorageGet(LOCAL_VISTOR_ID, "");
  209. if (!bfp) {
  210. localStorageSet(LOCAL_VISTOR_ID, generateUUIDv7());
  211. bfp = localStorageGet(LOCAL_VISTOR_ID, "")
  212. }
  213. function initCookie() {
  214. var uuid_key = 'winus_uuid';
  215. var md5val = getCookie(uuid_key);
  216. if (!md5val) {
  217. // 使用 UA + 时间戳 md5 , 保存到 cookie
  218. var val = bfp || generateUUIDv7();
  219. setCookie(uuid_key, val);
  220. md5val = getCookie(uuid_key);
  221. if (!md5val) {
  222. // 保存失败
  223. return {
  224. type: 'error',
  225. md5val: val
  226. };
  227. }
  228. } else {
  229. // 更新 cookie
  230. setCookie(uuid_key, md5val);
  231. }
  232. return {
  233. type: 'success',
  234. md5val: md5val
  235. };
  236. }
  237. window.addEventListener('message', function (e) {
  238. e.source.postMessage(initCookie(), e.origin);
  239. });
  240. </script>
  241. </head>
  242. <body>
  243. </body>
  244. </html>