ctc-track-plugin
Version:
uniapp 小程序埋点劫持
2 lines • 9.27 kB
JavaScript
var C=Object.defineProperty;var H=Object.getOwnPropertyDescriptor;var x=Object.getOwnPropertyNames;var j=Object.prototype.hasOwnProperty;var Y=(t,e)=>{for(var n in e)C(t,n,{get:e[n],enumerable:!0})},G=(t,e,n,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of x(e))!j.call(t,s)&&s!==n&&C(t,s,{get:()=>e[s],enumerable:!(r=H(e,s))||r.enumerable});return t};var W=t=>G(C({},"__esModule",{value:!0}),t);var ie={};Y(ie,{default:()=>oe,handleInitConfigParams:()=>B});module.exports=W(ie);var d=require("vue");var f=class{static genUrl(e,n){if(n&&typeof n=="object"){let r=[],s=[","];for(let a in n)if(Object.prototype.hasOwnProperty.call(n,a)){let o=n[a],u=`${a}=${encodeURIComponent(o||"")}`;if(o&&typeof o=="string"){for(let g of s)if(o.includes(g)){let l=new RegExp(`${encodeURIComponent(g)}`,"g");u=u.replace(l,g)}}r.push(u)}r.length>0&&(e=e+"?"+r.join("&"))}return e}};function T(){let t=getCurrentPages(),e;if(t&&t.length>0){let n=t[t.length-1];e={fullPath:f.genUrl(n.route,n.options),route:n.route,options:n.options}}return e}var w=require("js-md5"),c={DEFAULT_BATCH_SIZE:10,DEFAULT_INTERVAL_TIME:1e4,DEFAULT_RETRY_COUNT:3,DEFAULT_ACTIVE_SWITCH:1,DEFAULT_AUTO_SWITCH:1,CLIENT_TYPE:"2",RANDOM_MIN:1e3,RANDOM_MAX:9e3,HTTP_NOT_FOUND:404,HTTP_BAD_GATEWAY:502,REQUEST_SUCCESSFUL:0,CONFIG_API_PATH:"/event-api/event/config/configManage",REPORT_API_PATH:"/event-api/event/dataReport",IGNORED_PAGE_KEYS:["easycom","globalStyle","tabBar","pages"]},m,$={trackCount:1},z=()=>{let t=new Date,e=t.getFullYear(),n=String(t.getMonth()+1).padStart(2,"0"),r=String(t.getDate()).padStart(2,"0"),s=String(t.getHours()).padStart(2,"0"),a=String(t.getMinutes()).padStart(2,"0"),o=String(t.getSeconds()).padStart(2,"0");return`${e}-${n}-${r} ${s}:${a}:${o}`},M=()=>{let t=uni.getSystemInfoSync().deviceId,e=new Date().getTime(),n=Math.floor(Math.random()*c.RANDOM_MAX)+c.RANDOM_MIN;return`${t}${e}${n}`},O=(t,e)=>{let n=M();return(0,w.md5)(`${t}${n}${e}`)},V=()=>`${M()}${$.trackCount}`,Z=/[^/]+$/,_=t=>{var e;return(e=t.match(Z))==null?void 0:e[0]},h=(t,e)=>{let n=e.pages.find(r=>r.path===t||_(r.path)===_(t));if(n)return n;for(let r in e){if(c.IGNORED_PAGE_KEYS.includes(r))continue;let s=e[r];for(let a of s){let o=a.pages.find(u=>`${a.root}/${u.path}`===t||_(u.path)===_(t));if(o)return o}}return null},E=(0,d.shallowRef)(c.DEFAULT_BATCH_SIZE),R=(0,d.shallowRef)(c.DEFAULT_INTERVAL_TIME),U=(0,d.shallowRef)(c.DEFAULT_RETRY_COUNT),N=(0,d.shallowRef)(c.DEFAULT_ACTIVE_SWITCH),p=(0,d.shallowRef)(c.DEFAULT_AUTO_SWITCH),L=()=>{var n;let t=uni.getStorageSync("user");if(!t)return console.log("\u672A\u83B7\u53D6\u5230user\u503C"),{jwt:null,usertype:"customer"};let e=((n=JSON.parse(t))==null?void 0:n.userInfo)??{jwt:null,usertype:"customer"};return{jwt:e.jwt,usertype:e.usertype}},k=(t,e,n)=>{let r=t?`${t}${e}${n}`:`${e}${n}`;return(0,w.md5)(r)},K=t=>{var n;if(console.log(t,"config response"),t.statusCode===c.HTTP_NOT_FOUND||t.statusCode===c.HTTP_BAD_GATEWAY){i.error("\u83B7\u53D6\u57CB\u70B9\u914D\u7F6E\u53C2\u6570\u5931\u8D25");return}if(t.data.status!==c.REQUEST_SUCCESSFUL){i.error("\u83B7\u53D6\u57CB\u70B9\u914D\u7F6E\u53C2\u6570\u5931\u8D25",t.data.msg);return}let e=(n=t.data)==null?void 0:n.data;e&&(E.value=e.batchSize??c.DEFAULT_BATCH_SIZE,R.value=e.intervalTime??c.DEFAULT_INTERVAL_TIME,U.value=e.retryCount??c.DEFAULT_RETRY_COUNT,N.value=e.activeSwitch??c.DEFAULT_ACTIVE_SWITCH,p.value=e.autoSwitch??c.DEFAULT_AUTO_SWITCH)},B=(t={})=>{if(!m){i.error("\u57CB\u70B9URL\u672A\u914D\u7F6E\uFF0C\u65E0\u6CD5\u83B7\u53D6\u914D\u7F6E\u53C2\u6570");return}let e=Math.floor(new Date().getTime()/1e3),{jwt:n,usertype:r}=L(),s=(t==null?void 0:t.usertype)??r,o={sign:k(n,e,s),timestamp:e,usertype:s};n&&(o.Authorization=n),uni.request({url:`${m}${c.CONFIG_API_PATH}`,method:"POST",data:{clientType:c.CLIENT_TYPE,device:uni.getSystemInfoSync().deviceId},header:o,success:K,fail:()=>console.log("\u83B7\u53D6\u57CB\u70B9\u914D\u7F6E\u53C2\u6570\u5931\u8D25")})},i={success:(t,e)=>{console.log(`[\u57CB\u70B9\u6210\u529F] ${t}`,e||"")},error:(t,e)=>{console.error(`[\u57CB\u70B9\u9519\u8BEF] ${t}`,e||"")},info:(t,e)=>{console.log(`[\u57CB\u70B9\u4FE1\u606F] ${t}`,e||"")}},Q=t=>t!==c.HTTP_NOT_FOUND&&t!==c.HTTP_BAD_GATEWAY,D=()=>m?`${m}${c.REPORT_API_PATH}`:(i.error("\u57CB\u70B9URL\u672A\u914D\u7F6E\uFF0C\u65E0\u6CD5\u751F\u6210\u4E0A\u62A5URL"),""),X=(t,e)=>{if(!D()){i.error("\u65E0\u6CD5\u83B7\u53D6\u4E0A\u62A5URL\uFF0C\u53D6\u6D88\u57CB\u70B9\u6570\u636E\u53D1\u9001");return}let r=Math.floor(new Date().getTime()/1e3),{jwt:s,usertype:a}=L(),o=(e==null?void 0:e.usertype)??a,g={sign:k(s,r,o),timestamp:r,usertype:o};s&&(g.Authorization=s),uni.request({header:g,method:"POST",url:D(),data:t,success:l=>{Q(l.statusCode)?i.success("\u57CB\u70B9\u4E8B\u4EF6\u63D0\u4EA4\u6210\u529F",l):i.error("\u57CB\u70B9\u4E8B\u4EF6\u63D0\u4EA4\u5931\u8D25",`\u72B6\u6001\u7801: ${l.statusCode}`)},fail:l=>{i.error("\u57CB\u70B9\u4E8B\u4EF6\u63D0\u4EA4\u5931\u8D25",l)}})},J=()=>new Promise((t,e)=>{uni.getNetworkType({success({networkType:n}){t(n)},fail:n=>{e(n)}})}),q=()=>{var r;let t=uni.getStorageSync("user");if(!t)return i.info("\u672A\u83B7\u53D6\u5230user\u503C"),{userid:null,userType:null};let{userid:e,userType:n}=((r=JSON.parse(t))==null?void 0:r.userInfo)??{userid:null,userType:null};return{userid:e,userType:n}},ee=()=>{let{deviceId:t,uniPlatform:e,osName:n,osVersion:r,deviceModel:s,deviceBrand:a,ua:o}=uni.getSystemInfoSync();return{device_id:t,platform:e,os:n,osVersion:r,model:s,brand:a,ua:o,terminal_type:e==="mp-weixin"?"\u5FAE\u4FE1\u5C0F\u7A0B\u5E8F":"h5\u9875\u9762"}},te=async(t={})=>{let e=T(),n=await J(),{userid:r,userType:s}=q(),a=ee();return{event_timestamp:z(),page_url:e==null?void 0:e.route,extra_params:JSON.stringify({}),user_id:r,user_type:s,network_type:n,session_id:V(),...a,...t}},ne=t=>{if(typeof t=="object"&&t!==null){let{event_id:e,...n}=t;return{event_id:e,extra_params:JSON.stringify(n)}}return{event_id:t}},re=t=>{if(t.content)return{is_element:!1};let e=T();return{is_element:!0,element_id:null,element_type:t.tag,element_name:`${e==null?void 0:e.route}_${t.text}`}},b=class{batch=[];timer=null;batchSize;interval;retryCount;constructor(e,n,r){i.info("\u521D\u59CB\u5316BatchProcessor",{batchSize:e,intervalTime:n,retryCount:r}),this.batchSize=e,this.interval=n,this.retryCount=r}async sendBatch(e={}){if(this.batch.length===0)return;let n=this.retryCount,r=!1;for(;n>0&&!r;)try{i.info("\u53D1\u9001\u6279\u6B21\u6570\u636E",`${this.batch.length}\u6761\u8BB0\u5F55`),X({eventList:this.batch},e),r=!0,this.batch=[]}catch(s){n--,n===0&&i.error("\u6279\u6B21\u91CD\u8BD5\u540E\u4ECD\u5931\u8D25",s)}}async addToBatch(e,n={}){let r=await te(n),s=re(e),a=e.content?ne(e.content):{};this.batch.push({...r,...s,...a}),this.batch.length>=this.batchSize?(i.info("\u6279\u6B21\u5927\u5C0F\u8FBE\u5230\u9608\u503C\uFF0C\u7ACB\u5373\u53D1\u9001",`${this.batch.length}/${this.batchSize}`),await this.sendBatch(n)):this.timer||(i.info("\u542F\u52A8\u5B9A\u65F6\u5668",`${this.interval}\u79D2\u540E\u53D1\u9001`),this.timer=setTimeout(async()=>{await this.sendBatch(n),this.timer=null},this.interval*1e3))}},y;(0,d.watchEffect)(()=>{(E.value||R.value||U.value)&&(y=new b(E.value,R.value,U.value))});var se=t=>t.content&&N.value===0?(i.info("\u4E3B\u52A8\u4E0A\u62A5\u5F00\u5173\u5DF2\u5173\u95ED"),!1):!t.content&&p.value===0?(i.info("\u81EA\u52A8\u4E0A\u62A5\u5F00\u5173\u5DF2\u5173\u95ED"),!1):!0,P=async(t,e,n={})=>{se(t)&&($.trackCount++,await(y==null?void 0:y.addToBatch(t,{...n,event_type:e,id:O(e,new Date().getTime())})))},I=(t,e,n,r={})=>{var s,a,o;return{content:{event_id:(r==null?void 0:r.event_id)||((s=t==null?void 0:t.style)==null?void 0:s.navigationBarTitleText)},pageOptions:{page_id:(a=t==null?void 0:t.style)==null?void 0:a.navigationBarTitleText,refer_page_id:(o=e==null?void 0:e.style)==null?void 0:o.navigationBarTitleText,refer_page_url:n,...r,event_type:"page_view",id:O("page_view",new Date().getTime())}}},ae={install(t,e,n){e.baseUrl||i.error("\u8BF7\u4F20\u5165baseUrl\u53C2\u6570"),m=e.baseUrl,B(e);let r=async a=>{if(!m){i.error("\u57CB\u70B9URL\u672A\u914D\u7F6E\uFF0C\u8BF7\u5148\u4F20\u5165baseUrl\u53C2\u6570");return}i.info("\u624B\u52A8/\u81EA\u52A8\u57CB\u70B9\u89E6\u53D1",a),await P(a,"click",e)},s=async(a,o={},u)=>{if(!m){i.error("\u57CB\u70B9URL\u672A\u914D\u7F6E\uFF0C\u8BF7\u5148\u4F20\u5165baseUrl\u53C2\u6570");return}if(p.value===0&&u){i.info("\u81EA\u52A8\u4E0A\u62A5\u5F00\u5173\u5DF2\u5173\u95ED\uFF0C\u8DF3\u8FC7\u9875\u9762\u57CB\u70B9");return}let g=getCurrentPages(),l=g[g.length-1].route,S=h(l,n),v=h(a.slice(1),n),{content:A,pageOptions:F}=I(v,S,l||"",{...e,...o,page_url:a});await P({content:A},"page_view",F),i.success("\u9875\u9762\u8DF3\u8F6C\u57CB\u70B9\u6210\u529F",a)};t.config.globalProperties.sendMd=r,t.config.globalProperties.sendPageMd=s,uni.addInterceptor("navigateTo",{invoke(a){i.info("\u9875\u9762\u8DF3\u8F6C\u62E6\u622A",a)},success(a){if(p.value===0)return;let o=getCurrentPages(),u=T(),g=h(u==null?void 0:u.route,n),l=o[o.length-2].route,S=h(l,n),{content:v,pageOptions:A}=I(g,S,l||"",e);P({content:v},"page_view",A),i.success("\u9875\u9762\u8DF3\u8F6C\u57CB\u70B9\u6210\u529F",a)}})}},oe=ae;0&&(module.exports={handleInitConfigParams});
//# sourceMappingURL=bury.cjs.map