cg-utilities
Version:
This is a simple utility package
2 lines (1 loc) • 9.83 kB
JavaScript
(function(u,d){"use strict";function m(e,t){return Array.isArray(e)?e.reduce((n,o)=>d(n,{precision:4}).add(o).value,0):t===void 0?e:d(e,{precision:4}).add(t).value}function g(e,t){if(Array.isArray(e)){let n=0;return e.forEach(o=>{n?n=d(n,{precision:4}).subtract(o).value:n=o}),n}return t===void 0?e:d(e,{precision:4}).subtract(t).value}function w(e,t){if(Array.isArray(e)){let n=0;return e.forEach(o=>{n?n=d(n,{precision:4}).multiply(o).value:n=o}),n}return t===void 0?e:d(e,{precision:4}).multiply(t).value}function y(e,t){if(Array.isArray(e)){let n=0;return e.forEach(o=>{n?n=d(n,{precision:4}).divide(o).value:n=o}),n}return t===void 0?e:d(e,{precision:4}).divide(t).value}const l={DELTA:2654435769,base64EncodeChars:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".split(""),base64DecodeChars:[-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63,52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,-1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1],btoa(e){let t,n,o,r,F,i,c;for(n=o=0,r=e.length,F=r%3,r=r-F,i=r/3<<2,F>0&&(i+=4),t=new Array(i);n<r;)c=e.charCodeAt(n++)<<16|e.charCodeAt(n++)<<8|e.charCodeAt(n++),t[o++]=l.base64EncodeChars[c>>18]+l.base64EncodeChars[c>>12&63]+l.base64EncodeChars[c>>6&63]+l.base64EncodeChars[c&63];return F===1?(c=e.charCodeAt(n++),t[o++]=`${l.base64EncodeChars[c>>2]+l.base64EncodeChars[(c&3)<<4]}==`):F===2&&(c=e.charCodeAt(n++)<<8|e.charCodeAt(n++),t[o++]=`${l.base64EncodeChars[c>>10]+l.base64EncodeChars[c>>4&63]+l.base64EncodeChars[(c&15)<<2]}=`),t.join("")},atob(e){let t,n,o,r,F,i,c,a,s,h;if(c=e.length,c%4!==0||/[^ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789\+\/\=]/.test(e))return"";for(e.charAt(c-2)==="="?a=1:e.charAt(c-1)==="="?a=2:a=0,s=c,a>0&&(s-=4),s=(s>>2)*3+a,h=new Array(s),F=i=0;F<c&&(t=l.base64DecodeChars[e.charCodeAt(F++)],!(t===-1||(n=l.base64DecodeChars[e.charCodeAt(F++)],n===-1)||(h[i++]=String.fromCharCode(t<<2|(n&48)>>4),o=l.base64DecodeChars[e.charCodeAt(F++)],o===-1)||(h[i++]=String.fromCharCode((n&15)<<4|(o&60)>>2),r=l.base64DecodeChars[e.charCodeAt(F++)],r===-1)));)h[i++]=String.fromCharCode((o&3)<<6|r);return h.join("")},toBinaryString(e,t){const n=e.length;let o=n<<2;if(t){const F=e[n-1];if(o-=4,F<o-3||F>o)return null;o=F}for(let F=0;F<n;F++)e[F]=String.fromCharCode(e[F]&255,e[F]>>>8&255,e[F]>>>16&255,e[F]>>>24&255);const r=e.join("");return t?r.substring(0,o):r},toUint32Array(e,t){const n=e.length;let o=n>>2;n&3&&++o;let r;t?(r=new Array(o+1),r[o]=n):r=new Array(o);for(let F=0;F<n;++F)r[F>>2]|=e.charCodeAt(F)<<((F&3)<<3);return r},int32(e){return e&4294967295},mx(e,t,n,o,r,F){return(n>>>5^t<<2)+(t>>>3^n<<4)^(e^t)+(F[o&3^r]^n)},fixk(e){return e.length<4&&(e.length=4),e},encryptUint32Array(e,t){const n=e.length,o=n-1;let r,F,i,c,a,s;for(F=e[o],i=0,s=Math.floor(6+52/n)|0;s>0;--s){for(i=l.int32(i+l.DELTA),c=i>>>2&3,a=0;a<o;++a)r=e[a+1],F=e[a]=l.int32(e[a]+l.mx(i,r,F,a,c,t));r=e[0],F=e[o]=l.int32(e[o]+l.mx(i,r,F,o,c,t))}return e},decryptUint32Array(e,t){const n=e.length,o=n-1;let r,F,i,c,a,s;for(r=e[0],s=Math.floor(6+52/n),i=l.int32(s*l.DELTA);i!==0;i=l.int32(i-l.DELTA)){for(c=i>>>2&3,a=o;a>0;--a)F=e[a-1],r=e[a]=l.int32(e[a]-l.mx(i,r,F,a,c,t));F=e[o],r=e[0]=l.int32(e[0]-l.mx(i,r,F,0,c,t))}return e},utf8Encode(e){if(/^[\x00-\x7F]*$/.test(e))return e;const t=[],n=e.length;for(let o=0,r=0;o<n;++o,++r){const F=e.charCodeAt(o);if(F<128)t[r]=e.charAt(o);else if(F<2048)t[r]=String.fromCharCode(192|F>>6,128|F&63);else if(F<55296||F>57343)t[r]=String.fromCharCode(224|F>>12,128|F>>6&63,128|F&63);else{if(o+1<n){const i=e.charCodeAt(o+1);if(F<56320&&i>=56320&&i<=57343){const c=((F&1023)<<10|i&1023)+65536;t[r]=String.fromCharCode(240|c>>18&63,128|c>>12&63,128|c>>6&63,128|c&63),++o;continue}}throw new Error("Malformed string")}}return t.join("")},utf8DecodeShortString(e,t){const n=new Array(t);let o=0,r=0;for(let F=e.length;o<t&&r<F;o++){const i=e.charCodeAt(r++);switch(i>>4){case 0:case 1:case 2:case 3:case 4:case 5:case 6:case 7:n[o]=i;break;case 12:case 13:if(r<F)n[o]=(i&31)<<6|e.charCodeAt(r++)&63;else throw new Error("Unfinished UTF-8 octet sequence");break;case 14:if(r+1<F)n[o]=(i&15)<<12|(e.charCodeAt(r++)&63)<<6|e.charCodeAt(r++)&63;else throw new Error("Unfinished UTF-8 octet sequence");break;case 15:if(r+2<F){const c=((i&7)<<18|(e.charCodeAt(r++)&63)<<12|(e.charCodeAt(r++)&63)<<6|e.charCodeAt(r++)&63)-65536;if(c>=0&&c<=1048575)n[o++]=c>>10&1023|55296,n[o]=c&1023|56320;else throw new Error(`Character outside valid Unicode range: 0x${c.toString(16)}`)}else throw new Error("Unfinished UTF-8 octet sequence");break;default:throw new Error(`Bad UTF-8 encoding 0x${i.toString(16)}`)}}return o<t&&(n.length=o),String.fromCharCode.apply(String,n)},utf8DecodeLongString(e,t){const n=[],o=new Array(32768);let r=0,F=0;for(let i=e.length;r<t&&F<i;r++){const c=e.charCodeAt(F++);switch(c>>4){case 0:case 1:case 2:case 3:case 4:case 5:case 6:case 7:o[r]=c;break;case 12:case 13:if(F<i)o[r]=(c&31)<<6|e.charCodeAt(F++)&63;else throw new Error("Unfinished UTF-8 octet sequence");break;case 14:if(F+1<i)o[r]=(c&15)<<12|(e.charCodeAt(F++)&63)<<6|e.charCodeAt(F++)&63;else throw new Error("Unfinished UTF-8 octet sequence");break;case 15:if(F+2<i){const a=((c&7)<<18|(e.charCodeAt(F++)&63)<<12|(e.charCodeAt(F++)&63)<<6|e.charCodeAt(F++)&63)-65536;if(a>=0&&a<=1048575)o[r++]=a>>10&1023|55296,o[r]=a&1023|56320;else throw new Error(`Character outside valid Unicode range: 0x${a.toString(16)}`)}else throw new Error("Unfinished UTF-8 octet sequence");break;default:throw new Error(`Bad UTF-8 encoding 0x${c.toString(16)}`)}if(r>=32767-1){const a=r+1;o.length=a,n[n.length]=String.fromCharCode.apply(String,o),t-=a,r=-1}}return r>0&&(o.length=r,n[n.length]=String.fromCharCode.apply(String,o)),n.join("")},utf8Decode(e,t){return(t==null||t<0)&&(t=e.length),t===0?"":/^[\x00-\x7F]*$/.test(e)||!/^[\x00-\xFF]*$/.test(e)?t===e.length?e:e.substr(0,t):t<32767?l.utf8DecodeShortString(e,t):l.utf8DecodeLongString(e,t)},encrypt(e,t){return e==null||e.length===0?e:(e=l.utf8Encode(e),t=l.utf8Encode(t),l.toBinaryString(l.encryptUint32Array(l.toUint32Array(e,!0),l.fixk(l.toUint32Array(t,!1))),!1))},decrypt(e,t){return e==null||e.length===0?e:(t=l.utf8Encode(t),l.utf8Decode(l.toBinaryString(l.decryptUint32Array(l.toUint32Array(e,!1),l.fixk(l.toUint32Array(t,!1))),!0)))}};function p(e,t){return l.btoa(l.encrypt(e,t))}function E(e,t){return e==null||e.length===0?e:l.decrypt(l.atob(e),t)}function f(e={},t){const n=[];for(const F in e){const i=e[F];i!=null&&n.push(`${F}=${encodeURIComponent(i)}`)}const o=t.value;t.showConsole&&console.log("加密前",n.join("&"));const r=p(n.join("&"),o);return t.showConsole&&console.log("加密后",r),r}function S(e,t){const n=E(e,t.value);return JSON.parse(n)}function x(e,t){let n,o,r,F,i,c,a,s;for(n=e.length&3,o=e.length-n,r=t,i=3432918353,c=461845907,s=0;s<o;)a=e.charCodeAt(s)&255|(e.charCodeAt(++s)&255)<<8|(e.charCodeAt(++s)&255)<<16|(e.charCodeAt(++s)&255)<<24,++s,a=(a&65535)*i+(((a>>>16)*i&65535)<<16)&4294967295,a=a<<15|a>>>17,a=(a&65535)*c+(((a>>>16)*c&65535)<<16)&4294967295,r^=a,r=r<<13|r>>>19,F=(r&65535)*5+(((r>>>16)*5&65535)<<16)&4294967295,r=(F&65535)+27492+(((F>>>16)+58964&65535)<<16);switch(a=0,n){case 3:a^=(e.charCodeAt(s+2)&255)<<16;case 2:a^=(e.charCodeAt(s+1)&255)<<8;case 1:a^=e.charCodeAt(s)&255,a=(a&65535)*i+(((a>>>16)*i&65535)<<16)&4294967295,a=a<<15|a>>>17,a=(a&65535)*c+(((a>>>16)*c&65535)<<16)&4294967295,r^=a}return r^=e.length,r^=r>>>16,r=(r&65535)*2246822507+(((r>>>16)*2246822507&65535)<<16)&4294967295,r^=r>>>13,r=(r&65535)*3266489909+(((r>>>16)*3266489909&65535)<<16)&4294967295,r^=r>>>16,r>>>0}function D(e,t,n,o,r){const F=o;let i="",c="";return e.toLocaleString()==="get"?(i=n.lastIndexOf(".json")>-1?n:C(t)?`/${n}&${C(t)}`:`/${n}`,i.includes("//")&&(i=i.replace("//","/")),c=r.value+F+i):e.toLocaleString()==="post"&&(i=`/${n}`,i.includes("//")&&(i=i.replace("//","/")),c=f(t,r)+r.value+F+i),c+="1.0.0",r.showConsole&&(console.groupCollapsed(`🔑🔑签名参数--${n}🔑🔑`),console.log("nonce加密前的字符串",c),console.log(`nonce------- ${r.murmurhash_key||36}`,x(c,r.murmurhash_key||36)),console.groupEnd()),x(c,r.murmurhash_key||36)}function C(e={}){const t=[];for(const n in e){const o=e[n];o!=null&&t.push(`${n}=${o}`)}return t.join("&")}function A(e){const t=e||location.origin+"/date";return new Promise(n=>{const o=new XMLHttpRequest;o.open("head",t,!0),o.send(null),o.timeout=15e3,o.onreadystatechange=()=>{if(o.readyState===4){const r=o.getResponseHeader("Date"),F=new Date(r),i=new Date().getTime(),c=F.getTime()-i,a=new Date(new Date().getTime()+c);localStorage.setItem("__serverTime",a.toUTCString()),n(a.toUTCString())}}})}function T(e){return new Promise(t=>{const n=localStorage.getItem("__serverTime");n?t(n):A(e).then(o=>{t(o)})})}function U(e){if(e===void 0)return{};for(const t in e)(e[t]===""||e[t]===void 0||e[t]===null)&&delete e[t];return e}async function b(e,t){const n=new Date().getTime();$(e);const o=t["X-Ca-Timestamp"],r=D(e.method,U(e.data||e.params),e.url,o,t);return e.headers["request-startTime"]=n,e.headers.d=t.d,e.headers.v="1.0.0",e.headers.u=performance.now(),e.headers.lang="zh-CN",e.headers["X-Ca-Timestamp"]=o,e.headers["X-Ca-Nonce"]=r,(e.method==="post"||e.method==="put")&&(e.data=f(e.data,t)),{}}function $(e){return e.url.indexOf("?")===-1?e.url+="?r="+new Date().getTime():e.url+="&r="+new Date().getTime(),e}u.add=m,u.decryptQuery=S,u.divide=y,u.getServerTime=T,u.multiply=w,u.setServerTime=A,u.setXxetaConfig=b,u.subtract=g,Object.defineProperty(u,Symbol.toStringTag,{value:"Module"})})(this["cg-utils"]=this["cg-utils"]||{},currencyjs);