/** * * * * * * */ function fromCodePointSimplified(...codePoints) { return codePoints.reduce((str, cp) => { if (cp > 0xFFFF) { // 转换为代理对 cp -= 0x10000; return str + String.fromCharCode((cp >> 10) + 0xD800) + String.fromCharCode((cp & 0x3FF) + 0xDC00); } else { return str + String.fromCharCode(cp); } }, ''); } async function sha256(message) { // 转换为ArrayBuffer const msgBuffer = new TextEncoder().encode(message); // 使用SubtleCrypto接口生成哈希 const hashBuffer = await crypto.subtle.digest('SHA-256', msgBuffer); // 转换为十六进制字符串 const hashArray = Array.from(new Uint8Array(hashBuffer)); const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join(''); return hashHex; } function guid() { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c){ var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8); return v.toString(16); }); } const uuid=guid().replace("-","").replace("-","").replace("-","").replace("-",""); // "a1ca0f7b-51bd-4bf3-a5d5-6a74f6adc1c7"; sessionStorage.setItem("bsf", 'asdfghjklzxcvbnmqwertyuiopASDFGHJKLZXCVBNMQWERTYUIOP1234567890') const timeStamp = new Date().getTime(); function formatTimestamp(timestamp) { const date = new Date(timestamp); const year = date.getFullYear(); const month = ('0' + (date.getMonth() + 1)).slice(-2); const day = ('0' + date.getDate()).slice(-2); return `${year}-${month}-${day}`; } const yeeDate = formatTimestamp(timeStamp) function removeInvalidUtf8Chars(input) { const invalidUtf8Regex = /[\xC0-\xC1]|[\xF5-\xFF]|[\x80-\xBF]|([\xC2-\xF4][^\x80-\xBF])/g; return input.replace(invalidUtf8Regex, ''); } const _Base64 = { _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", encode:function(e) { let t = ""; let n, r, i, s, o, u, a; let f = 0; while (f < e.length) { n = e.charCodeAt(f++); r = e.charCodeAt(f++); i = e.charCodeAt(f++); s = n >> 2; o = (n & 3) << 4 | r >> 4; u = (r & 15) << 2 | i >> 6; a = i & 63; if (isNaN(r)) { u = a = 64 } else if (isNaN(i)) { a = 64 } t += this._keyStr.charAt(s) + this._keyStr.charAt(o) + this._keyStr.charAt(u) + this._keyStr.charAt(a) } return t }, decode:function(e) { let t = ""; let n, r, i; let s, o, u, a; let f = 0; e = e.replace('/[^A-Za-z0-9+/=]/g', "").toString() while (f < e.length) { s = this._keyStr.indexOf(e.charAt(f++)); o = this._keyStr.indexOf(e.charAt(f++)); u = this._keyStr.indexOf(e.charAt(f++)); a = this._keyStr.indexOf(e.charAt(f++)); n = s << 2 | o >> 4; r = (o & 15) << 4 | u >> 2; i = (u & 3) << 6 | a; t = t + String.fromCharCode(n); if (u != 64) { t += String.fromCharCode(r) } if (a != 64) { t += String.fromCharCode(i) } } return t }, _utf8_encode: function(e) { e = e.replace(/rn/g, "n"); let t = ""; for (let n = 0; n < e.length; n++) { let r = e.charCodeAt(n); if (r < 128) { t += String.fromCharCode(r) } else if (r > 127 && r < 2048) { t += String.fromCharCode(r >> 6 | 192); t += String.fromCharCode(r & 63 | 128) } else { t += String.fromCharCode(r >> 12 | 224); t += String.fromCharCode(r >> 6 & 63 | 128); t += String.fromCharCode(r & 63 | 128) } } return t }, _utf8_decode: function(e) { let t = ""; let n = 0; let r = c1 = c2 = 0; while (n < e.length) { r = e.charCodeAt(n); if (r < 128) { t += String.fromCharCode(r); n++ } else if (r > 191 && r < 224) { c2 = e.charCodeAt(n + 1); t += String.fromCharCode((r & 31) << 6 | c2 & 63); n += 2 } else { c2 = e.charCodeAt(n + 1); c3 = e.charCodeAt(n + 2); t += String.fromCharCode((r & 15) << 12 | (c2 & 63) << 6 | c3 & 63); n += 3 } } return t } } const UTF8 = { encode: function(str) { let rs = ''; for(let i of str) { const code = i.codePointAt(0); if(code < 128) { rs += i; } else if(code > 127 && code < 2048) { rs += String.fromCodePoint((code >> 6) | 192, (code & 63) | 128); } else if(code > 2047 && code < 65536) { rs += String.fromCodePoint((code >> 12) | 224, ((code >> 6) & 63) | 128, (code & 63) | 128); } else if(code > 65536 && code < 1114112) { rs += String.fromCodePoint((code >> 18) | 240, ((code >> 12) & 63) | 128, ((code >> 6) & 63) | 128, (code & 63) | 128); } } console.log('UTF8', rs); return rs; }, decode: function(str) { let rs = ''; for(let i = 0; i < str.length; i++) { const code = str[i]; if((240 & code) == 240) { const code1 = str[i + 1]; const code2 = str[i + 2]; const code3 = str[i + 3]; rs += fromCodePointSimplified(((code & 7) << 18) | ((code1 & 63) << 12) | ((code2 & 63) << 6) | (code3 & 63)); i += 3; } else if((224 & code) == 224) { const code1 = str[i + 1]; const code2 = str[i + 2]; rs += fromCodePointSimplified(((code & 15) << 12) | ((code1 & 63) << 6) | (code2 & 63)); i += 2; } else if((192 & code) == 192) { const code1 = str[i + 1]; rs += fromCodePointSimplified(((code & 31) << 6) | (code1 & 63)); i++; } else if((128 & code) == 0) { rs += fromCodePointSimplified(code); } } // console.log(rs); return rs; } };