UNPKG

4.23 kBJavaScriptView Raw
1import{psLog as t,ROOM_SESSION_KEY as e,removeEndSlash as a}from"@huolala-tech/page-spy-base";import{getMPSDK as r,utilAPI as o}from"@huolala-tech/page-spy-mp-base";function n(t){return n="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},n(t)}function i(t){var e=function(t,e){if("object"!=n(t)||!t)return t;var a=t[Symbol.toPrimitive];if(void 0!==a){var r=a.call(t,e||"default");if("object"!=n(r))return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===e?String:Number)(t)}(t,"string");return"symbol"==n(e)?e:e+""}function s(t,e,a){return(e=i(e))in t?Object.defineProperty(t,e,{value:a,enumerable:!0,configurable:!0,writable:!0}):t[e]=a,t}class c{constructor(){s(this,"container",[])}add(t){return this.container.push(t),!0}clear(){this.container=[]}}const l=()=>{const t=o.getStorage(e);return null!=t&&t.address&&"string"==typeof t.address?t.address.slice(0,4):"--"},u={caredData:{console:!0,network:!0,storage:!0,system:!1,meta:!0},filename:()=>(new Date).toLocaleString()};class h{constructor(t){s(this,"enforce","pre"),s(this,"name","DataHarborPlugin"),s(this,"apiBase",""),s(this,"isPaused",!1),s(this,"$socketStore",null),s(this,"$pageSpyConfig",null),s(this,"client",null),this.$harborConfig={...u,...t},this.harbor=new c}async onInit(e){let{socketStore:r,config:o,client:n}=e;if(h.hasInited)return;h.hasInited=!0,this.$pageSpyConfig=o,this.$socketStore=r,this.client=n;const{api:i,enableSSL:s,offline:c}=o;if(c||i){const t=s?"https://":"http://";this.apiBase=a("".concat(t).concat(i))}else t.warn("Cannot upload log to PageSpy for miss 'api' configuration. See: ",o);this.$socketStore.addListener("public-data",(e=>{if(this.isPaused||!this.isCaredPublicData(e))return;const a=((t,e)=>({type:t,timestamp:Date.now(),data:e}))(e.type,e.data);this.harbor.add(a)||t.warn("[".concat(this.name,"] Fail to save data in harbor "),a)}))}onActionSheet(){return r(),[{text:"上传离线日志",action:async()=>{await this.upload()}}]}async upload(){var e;let a=!(arguments.length>0&&void 0!==arguments[0])||arguments[0];const o=r();o.showLoading({title:"正在上传离线日志..."});const{filename:n}=this.$harborConfig,{project:i="",title:s=""}=this.$pageSpyConfig||{},c={project:i,title:s,deviceId:l(),userAgent:null===(e=this.client)||void 0===e?void 0:e.getName()},u=[...this.harbor.container];u.push(this.makeMetaInfo());const h=((t,e)=>{const a=r(),o=a.getFileSystemManager(),n="".concat(a.env.USER_DATA_PATH,"/").concat(e.toString().replace(/[^\w]/g,"_"),".json");return o.writeFileSync(n,JSON.stringify(t),"utf8"),n})(u,n()),d="".concat(this.apiBase,"/api/v1/log/upload?").concat((p=c,Object.entries(p).map((t=>{let[e,a]=t;return"".concat(e,"=").concat(encodeURIComponent(a))})).join("&")));var p;try{var f;if(await(async e=>{let{url:a,path:o}=e;return new Promise(((e,n)=>{r().uploadFile({filePath:o,header:{"Content-Type":"multipart/form-data"},name:"log",url:a,success:t=>{if(200===t.statusCode){const a=JSON.parse(t.data);a.success?e(a):n(a)}},fail:e=>{t.warn("Upload failed",e),n(e)}})}))})({path:h,url:d}),a)this.harbor.clear(),null===(f=this.$socketStore)||void 0===f||f.dispatchEvent("harbor-clear",null);o.hideLoading(),o.showToast({title:"离线日志上传成功",icon:"success"})}catch(e){t.error(e),o.hideLoading(),o.showToast({icon:"error",title:"离线日志上传失败"})}o.getFileSystemManager().unlinkSync(h)}onReset(){this.harbor.clear(),h.hasInited=!1}pause(){this.isPaused=!0}resume(){this.isPaused=!1}reharbor(){var t;this.harbor.clear(),null===(t=this.$socketStore)||void 0===t||t.dispatchEvent("harbor-clear",null),this.isPaused&&(this.isPaused=!1)}isCaredPublicData(t){if(!t)return!1;const{type:e}=t,{caredData:a}=this.$harborConfig;switch(e){case"console":return!!a.console;case"storage":return!!a.storage;case"system":return!!a.system;case"network":return!!a.network;default:return!1}}makeMetaInfo(){var t;const e=null===(t=this.client)||void 0===t?void 0:t.makeClientInfoMsg();return{type:"meta",timestamp:Date.now(),data:e}}}s(h,"hasInited",!1);export{h as default};
2//# sourceMappingURL=index.min.js.map