mirror of
https://gitee.com/xiangheng/x_admin.git
synced 2025-10-10 18:40:17 +08:00
4 lines
5.7 KiB
JavaScript
4 lines
5.7 KiB
JavaScript
(function(a,n){typeof exports=="object"&&typeof module<"u"?n(exports):typeof define=="function"&&define.amd?define(["exports"],n):(a=typeof globalThis<"u"?globalThis:a||self,n(a.XErr={}))})(this,function(a){"use strict";var k=Object.defineProperty;var I=(a,n,h)=>n in a?k(a,n,{enumerable:!0,configurable:!0,writable:!0,value:h}):a[n]=h;var o=(a,n,h)=>I(a,typeof n!="symbol"?n+"":n,h);const n=[];for(let t=0;t<256;++t)n.push((t+256).toString(16).slice(1));function h(t,e=0){return(n[t[e+0]]+n[t[e+1]]+n[t[e+2]]+n[t[e+3]]+"-"+n[t[e+4]]+n[t[e+5]]+"-"+n[t[e+6]]+n[t[e+7]]+"-"+n[t[e+8]]+n[t[e+9]]+"-"+n[t[e+10]]+n[t[e+11]]+n[t[e+12]]+n[t[e+13]]+n[t[e+14]]+n[t[e+15]]).toLowerCase()}let m;const g=new Uint8Array(16);function y(){if(!m){if(typeof crypto>"u"||!crypto.getRandomValues)throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");m=crypto.getRandomValues.bind(crypto)}return m(g)}const d={};function _(t,e,i){let s;{const r=Date.now(),l=y();w(d,r,l),s=x(l,d.msecs,d.nsecs,d.clockseq,d.node,e,i)}return e?s:h(s)}function w(t,e,i){return t.msecs??(t.msecs=-1/0),t.nsecs??(t.nsecs=0),e===t.msecs?(t.nsecs++,t.nsecs>=1e4&&(t.node=void 0,t.nsecs=0)):e>t.msecs?t.nsecs=0:e<t.msecs&&(t.node=void 0),t.node||(t.node=i.slice(10,16),t.node[0]|=1,t.clockseq=(i[8]<<8|i[9])&16383),t.msecs=e,t}function x(t,e,i,s,r,l,c=0){l||(l=new Uint8Array(16),c=0),e??(e=Date.now()),i??(i=0),s??(s=(t[8]<<8|t[9])&16383),r??(r=t.slice(10,16)),e+=122192928e5;const u=((e&268435455)*1e4+i)%4294967296;l[c++]=u>>>24&255,l[c++]=u>>>16&255,l[c++]=u>>>8&255,l[c++]=u&255;const p=e/4294967296*1e4&268435455;l[c++]=p>>>8&255,l[c++]=p&255,l[c++]=p>>>24&15|16,l[c++]=p>>>16&255,l[c++]=s>>>8|128,l[c++]=s&255;for(let f=0;f<6;++f)l[c++]=r[f];return l}class S{constructor(e,i){o(this,"Dns","");o(this,"client_id","");o(this,"Pid","");o(this,"Uid","");o(this,"platform",null);o(this,"MessageList",[]);o(this,"timer",0);o(this,"lastTimeStamp",Date.now());o(this,"diffTime",1e3*10);o(this,"Push",e=>{var i;this.MessageList.push({...e,ProjectKey:this.Pid,ClientId:this.client_id}),this.MessageList.length>5?this.upload():(i=this.platform)==null||i.setCache("x_err_message_list",this.MessageList)});o(this,"uploadInfo",()=>{var e;if(this.Dns)try{(e=this.platform)==null||e.upload(this.Dns+"/admin/monitor_client/add",{ProjectKey:this.Pid,ClientId:this.client_id,UserId:this.Uid}).catch(i=>{})}catch{}});if(!e){console.error("props is null");return}if(!i){console.error("platform is null");return}if(this.platform=i,e.Dns&&e.Pid)this.Dns=e.Dns,this.Pid=e.Pid;else{console.error("props.Dns and props.Pid cannot be null");return}e.Uid&&(this.Uid=String(e.Uid)),this.setClientID(),this.SetUid(),this.getLocalMessage(),i.listen(s=>{console.log("listenCallback",s),this.Push(s)}),this.timer=setInterval(()=>{Date.now()-this.lastTimeStamp>=this.diffTime&&this.upload()},2e3)}SetUid(e){var i,s;if(e)this.Uid=String(e),(i=this.platform)==null||i.setCache("x_err_uid",this.Uid);else{const r=(s=this.platform)==null?void 0:s.getCache("x_err_uid");r&&(this.Uid=r)}this.uploadInfo()}setClientID(){var i,s;const e=(i=this.platform)==null?void 0:i.getCache("x_err_client_id");e?this.client_id=e:(this.client_id=_(),(s=this.platform)==null||s.setCache("x_err_client_id",this.client_id))}getLocalMessage(){var i;let e=(i=this.platform)==null?void 0:i.getCache("x_err_message_list");e?this.MessageList=e:this.MessageList=[]}upload(){var e;if(this.Dns&&this.MessageList.length){this.lastTimeStamp=Date.now();try{(e=this.platform)==null||e.upload(this.Dns+"/admin/monitor_error/add",this.MessageList).catch(i=>{}).then(()=>{var i;this.MessageList=[],(i=this.platform)==null||i.delCache("x_err_message_list")})}catch{}}}unListen(){var e;clearInterval(this.timer),(e=this.platform)==null||e.unListen()}}class v{constructor(e){o(this,"props");o(this,"listenError",e=>{var s;console.error([e]);let i=e.target;i!=null&&i.localName?(i==null?void 0:i.localName)==="img"||(i==null?void 0:i.localName)==="script"?this.callback({Type:"resources",EventType:i==null?void 0:i.localName,Path:i.src,Message:"",Stack:"",Height:0}):(i==null?void 0:i.localName)==="link"&&this.callback({Type:"resources",EventType:i==null?void 0:i.localName,Path:i.href}):this.callback({Type:"error",EventType:e.type,Path:window.location.href,Message:e.message,Stack:this.handleStack(((s=e.error)==null?void 0:s.stack)||""),...this.getEnvInfo()})});o(this,"unhandledrejection",e=>{var i,s;console.error(e),e&&typeof e.reason=="string"?this.callback({Type:"error",EventType:e.type,Path:window.location.href,Message:e.reason,Stack:"",...this.getEnvInfo()}):e&&typeof e.reason=="object"&&this.callback({Type:"error",EventType:e.type,Path:window.location.href,Message:((i=e.reason)==null?void 0:i.message)||"",Stack:this.handleStack(((s=e.reason)==null?void 0:s.stack)||""),...this.getEnvInfo()})});this.props={...e}}upload(e,i){return new Promise(s=>{try{let r=new Image;r.onload=function(l){var c=l;c.preventDefault()},r.onerror=function(){},r.src=e+"?data="+encodeURIComponent(JSON.stringify(i)),s()}catch{s()}})}setCache(e,i){localStorage.setItem(e,JSON.stringify(i))}getCache(e){try{let i=localStorage.getItem(e);return i?JSON.parse(i):null}catch{return null}}delCache(e){localStorage.removeItem(e)}getEnvInfo(){const e={Height:0,Width:0};return window&&(e.Height=window.innerHeight||0,e.Width=window.innerWidth||0),e}callback(e){}handleStack(e){let i=[];return e&&e.split(`
|
|
`).map((s,r)=>{r<4&&i.push(s)}),i.join(`
|
|
`)}listen(e){this.callback=e,window.addEventListener("unhandledrejection",this.unhandledrejection),window.addEventListener("error",this.listenError,!0)}unListen(){this.callback=()=>{},window.removeEventListener("error",this.listenError),window.removeEventListener("unhandledrejection",this.unhandledrejection)}}a.Base=S,a.Web=v,Object.defineProperty(a,Symbol.toStringTag,{value:"Module"})});
|