| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>cookie</title>
- <script>
- //简单加密
- const encrypt = (str, pwd) => {
- if (str == '') {
- return '';
- }
- str = encodeURIComponent(str);
- if (!pwd || pwd == '') {
- pwd = 'xbd4jl';
- }
- pwd = encodeURIComponent(pwd);
- if (pwd == '' || pwd.length <= 0) {
- return '';
- }
- var prand = '';
- for (var i = 0, len = pwd.length; i < len; i += 1) {
- prand += pwd.charCodeAt(i).toString();
- }
- var sPos = Math.floor(prand.length / 5);
- var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos * 2) + prand.charAt(sPos * 3) +
- prand.charAt(sPos * 4) + prand.charAt(sPos * 5));
- var incr = Math.ceil(pwd.length / 2);
- var modu = Math.pow(2, 31) - 1;
- if (mult < 2) {
- return '';
- }
- var salt = Math.round(Math.random() * 1000000000) % 100000000;
- prand += salt;
- while (prand.length > 10) {
- let left = prand;
- let num = 0;
- while (left.length > 0) {
- let temp = parseInt(left.substring(0, 10));
- num = num + temp;
- left = left.substring(10, left.length);
- }
- prand = num.toString();
- }
- prand = (mult * prand + incr) % modu;
- var encChr = '';
- var encStr = '';
- for (var i = 0, len = str.length; i < len; i += 1) {
- encChr = parseInt(str.charCodeAt(i) ^ Math.floor((prand / modu) * 255));
- if (encChr < 16) {
- encStr += '0' + encChr.toString(16);
- } else {
- encStr += encChr.toString(16);
- }
- prand = (mult * prand + incr) % modu;
- }
- salt = salt.toString(16);
- while (salt.length < 8) {
- salt = "0" + salt;
- }
- encStr += salt;
- return encStr;
- }
- //简单解密
- const decrypt = (str, pwd) => {
- if (str == '') {
- return '';
- }
- if (!pwd || pwd == '') {
- pwd = 'xbd4jl';
- }
- pwd = encodeURIComponent(pwd);
- if (str == undefined || str.length < 8) {
- return '';
- }
- if (pwd == undefined || pwd.length <= 0) {
- return '';
- }
- var prand = '';
- for (var i = 0, len = pwd.length; i < len; i += 1) {
- prand += pwd.charCodeAt(i).toString();
- }
- var sPos = Math.floor(prand.length / 5);
- // console.log(prand.length+"sPos____"+sPos+"prand"+prand);
- var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos * 2) + prand.charAt(sPos * 3) +
- prand.charAt(sPos * 4) + prand.charAt(sPos * 5));
- var incr = Math.round(pwd.length / 2);
- var modu = Math.pow(2, 31) - 1;
- var salt = parseInt(str.substring(str.length - 8, str.length), 16);
- str = str.substring(0, str.length - 8);
- // console.log("prand"+prand);
- prand += salt;
- // console.log("prand"+prand);
- while (prand.length > 10) {
- let left = prand;
- let num = 0;
- while (left.length > 0) {
- let temp = parseInt(left.substring(0, 10));
- num = num + temp;
- left = left.substring(10, left.length);
- }
- prand = num.toString();
- }
- // console.log("prand"+prand);
- prand = (mult * prand + incr) % modu;
- var encChr = '';
- var encStr = '';
- for (var i = 0, len = str.length; i < len; i += 2) {
- encChr = parseInt(parseInt(str.substring(i, i + 2), 16) ^ Math.floor((prand / modu) * 255));
- encStr += String.fromCharCode(encChr);
- prand = (mult * prand + incr) % modu;
- }
- return decodeURIComponent(encStr);
- }
- const LOCAL_VISTOR_ID = "LOCAL_VISTOR_ID";
- const head = "a6072637";
- const isenc = true;
- const localStorageGet = (key, defaultValue = "") => {
- let value = localStorage.getItem(key);
- if (value == null) value = defaultValue;
- else {
- if (value.startsWith(head)) {
- try {
- value = decrypt(value.substring(head.length));
- } catch (err) {
- //如果出错返回空
- value = "";
- }
- }
- }
- return value;
- }
- const localStorageSet = (key, value) => {
- if (isenc) {
- if (value != null && value !== "") {
- if (typeof value !== "string") value = value.toString();
- try {
- value = head + encrypt(value);
- } catch (err) { }
- }
- }
- localStorage.setItem(key, value);
- }
- /**
- * 生成 UUID v7 (基于时间排序)
- * 格式: 48位时间戳 | 4位版本(7) | 12位随机数 | 2位变体(10) | 62位随机数
- */
- function generateUUIDv7() {
- const bytes = new Uint8Array(16);
- // 1. 填充随机数 (16 字节)
- crypto.getRandomValues(bytes);
- // 2. 写入 48 位毫秒时间戳 (大端序)
- const now = Date.now();
- bytes[0] = (now / 0x10000000000) & 0xff;
- bytes[1] = (now / 0x100000000) & 0xff;
- bytes[2] = (now / 0x1000000) & 0xff;
- bytes[3] = (now / 0x10000) & 0xff;
- bytes[4] = (now / 0x100) & 0xff;
- bytes[5] = now & 0xff;
- // 3. 设置版本号 (Version 7)
- // 清除前 4 位并设为 0111 (0x70)
- bytes[6] = (bytes[6] & 0x0f) | 0x70;
- // 4. 设置变体号 (Variant 10xx)
- // 清除前 2 位并设为 10 (0x80)
- bytes[8] = (bytes[8] & 0x3f) | 0x80;
- // 5. 转换为 8-4-4-4-12 字符串格式
- return Array.from(bytes)
- .map((b, i) => {
- let s = b.toString(16).padStart(2, '0');
- // 在特定位置添加连字符
- if (i === 3 || i === 5 || i === 7 || i === 9) return s + '-';
- return s;
- })
- .join('');
- }
- function getRandomNumber(min, max) {
- return Math.floor(Math.random() * (max - min + 1)) + min;
- }
- window.hasFixexpire = false;
- function getCookie(key) {
- const cookie = document.cookie;
- const cookieArr = cookie.split(';');
- const cookieObj = {};
- cookieArr.forEach(item => {
- const [k, v] = item.split('=');
- const trimmedKey = k ? k.trim() : '';
- const trimmedValue = v ? v.trim() : '';
- if (trimmedKey && trimmedValue)
- cookieObj[trimmedKey] = trimmedValue;
- });
- if (!window.hasFixexpire) {
- if (cookieObj['winus_uuid']) {
- setCookie('winus_uuid', cookieObj['winus_uuid']);
- }
- if (cookieObj['us_h5_sourceurl_key']) {
- setCookie('us_h5_sourceurl_key', cookieObj['us_h5_sourceurl_key']);
- }
- window.hasFixexpire = true;
- }
- return key ? cookieObj[key] : cookieObj;
- };
- function setCookie(key, value, expires = 86400 * 1000) {
- const date = new Date();
- date.setTime(date.getTime() + expires);
- // document.cookie = `${key}=${value};path=/;domain=.${window.location.hostname.split('.').slice(-2).join('.')};expires=${date.toUTCString()};SameSite=None;Secure;`;
- document.cookie = `${key}=${value};path=/;domain=${window.location.hostname};expires=${date.toUTCString()};SameSite=None;Secure;`;
- };
- let bfp = localStorageGet(LOCAL_VISTOR_ID, "");
- if (!bfp) {
- localStorageSet(LOCAL_VISTOR_ID, generateUUIDv7());
- bfp = localStorageGet(LOCAL_VISTOR_ID, "")
- }
- function initCookie() {
- var uuid_key = 'winus_uuid';
- var md5val = getCookie(uuid_key);
- if (!md5val) {
- // 使用 UA + 时间戳 md5 , 保存到 cookie
- var val = bfp || generateUUIDv7();
- setCookie(uuid_key, val);
- md5val = getCookie(uuid_key);
- if (!md5val) {
- // 保存失败
- return {
- type: 'error',
- md5val: val
- };
- }
- } else {
- // 更新 cookie
- setCookie(uuid_key, md5val);
- }
- return {
- type: 'success',
- md5val: md5val
- };
- }
- window.addEventListener('message', function (e) {
- e.source.postMessage(initCookie(), e.origin);
- });
- </script>
- </head>
- <body>
- </body>
- </html>
|