UNPKG

sd-data-weapp

Version:

data-weapp sdk for shuidihuzhu

2 lines (1 loc) 15.1 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t=t||self).SD_DATA=e()}(this,(function(){"use strict";var t=function(t,e){return t(e={exports:{}},e.exports),e.exports}((function(t){function e(a){return"function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?t.exports=e=function(t){return typeof t}:t.exports=e=function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},e(a)}t.exports=e}));var e,a=function(t,e,a){return e in t?Object.defineProperty(t,e,{value:a,enumerable:!0,configurable:!0,writable:!0}):t[e]=a,t};function o(t,e){var a=Object.keys(t);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(t);e&&(o=o.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),a.push.apply(a,o)}return a}function i(t){for(var e=1;e<arguments.length;e++){var i=null!=arguments[e]?arguments[e]:{};e%2?o(Object(i),!0).forEach((function(e){a(t,e,i[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(i)):o(Object(i)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(i,e))}))}return t}var n={usePlugin:!1,biz:"",appVersion:"v0.0.0",appType:0,tokenKey:"userInfo.sdToken",cryptoIdKey:"userInfo.cryptoUserId",openIdKey:"openId",appId:""},s="v2.4.3",r="v0.0.0",c="v0.0.0",h=19,u="https://log.shuidichou.com/log",l="https://ds.shuidihuzhu.com/api/hawkeye/experiment/query",p="sdCacheData",g=500,f=1e3,d=2e3,_="sdTrackVisitId",m="sdTrackVisitDv",v="sdTrackVisitTime",y=3e4,I=864e5,b="selfTag",S="userSourceId",P="shareSourceId",w="abTestDebug",C="adPositionId",T="adCreativeId",D="audience",k="skuId",x="envId",A="cepId",O={App:App,Page:Page,Component:Component},M=null,V=function(t){var e;try{e=wx.getStorageSync(t)}catch(t){e=!1,M._set("useLocalData",!1)}return e},j=function(t,e){try{wx.setStorageSync(t,e)}catch(t){M._set("useLocalData",!1)}},N=function(){for(var t="ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678",e=t.length,a="",o=h,i=0;i<o;i++)a+=t.charAt(Math.floor(Math.random()*e));return a+(new Date).getTime()},q=function(t){var e=[];for(var a in t)e.push(a+"="+t[a]);return e.join("&")},K=function(t){for(var e,a,o,i,n="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",s="",r=0,c=(t=String(t)).length,h=c%3;r<c;)(a=t.charCodeAt(r++))>255||(o=t.charCodeAt(r++))>255||(i=t.charCodeAt(r++)),s+=n.charAt((e=a<<16|o<<8|i)>>18&63)+n.charAt(e>>12&63)+n.charAt(e>>6&63)+n.charAt(63&e);return h?s.slice(0,h-3)+"===".substring(h):s},L={appLaunch:function(t){e.init(n,t)},appShow:function(t){Y.call(this,t),U.call(this),Q.call(this),it.call(this)},appHide:function(){j(v,(+new Date).toString())},pageLoad:function(){this.sdDataPageShow=0,Z.call(e)},pageShow:function(){this.sdDataPageShow?Z.call(e):this.sdDataPageShow++},pageScroll:$};function z(){var t=N();j(b,t),this._set("cookieId",t)}function B(){var t=this._get("cookieId").substring(h);isNaN(t)||+new Date-t>=I?this._set("isNew",0):this._set("isNew",1)}function R(){return j(p,{version:r,ab:{apps:{},debug:[]},data:[]}),V(p)}function F(){var t=this._get("visitDv");t++,this._set("visitDv",t),j(m,t.toString())}function H(t,e){var a=setTimeout((function(){e&&e(),a=null}),g);wx.request({url:this._get("url"),method:"POST",data:[t],header:{"content-type":"application/json"},complete:function(){a&&(clearTimeout(a),e&&e())}})}function E(t){var e=n[t],a=e.split(".")[0],o=e.split(".")[1],i=V(a);if(!i)return"";if(o)try{i=i[o]||""}catch(t){i=""}return i}function J(t,e){e=e||{};var a={biz:this._get("biz"),appId:at.call(this),appType:this._get("appType"),appVersion:this._get("appVersion"),version:s,wxSystemInfo:this._get("logSystemInfo"),op:t,opTime:+new Date,elementCode:+e.elementCode||0,authorizationV2:E("tokenKey"),openId:E("openIdKey"),fromPath:e.fromPath||this._get("pageMessage").fromPath||"",fromParams:e.fromParams||this._get("pageMessage").fromParams||{},toPath:e.toPath||this._get("pageMessage").toPath||"",toParams:e.toParams||this._get("pageMessage").toParams||{},isNew:this._get("isNew"),selfTag:this._get("cookieId"),userSourceId:this._get("userSourceId")||"",shareSourceId:this._get("shareSourceId")||"",channel:this._get("pageMessage").toParams&&this._get("pageMessage").toParams.channel||"",subChannel:this._get("pageMessage").toParams&&this._get("pageMessage").toParams.subchannel||"",channel_scene:this._get("channelOption").scene||"",channel_appId:this._get("channelOption").referrerInfo.appId||"",testCode:this._get("testCode"),visitDv:this._get("visitDv"),visitId:this._get("visitId"),actionType:this._get("actionType"),extInfo:e.extInfo||""},o=this._get("dataFromCookie");for(var i in o)void 0!==a[i]&&(a[i]=V(o[i])||"");return this._get("debug")&&console.log(a),a}function U(){V(m)?this._set("visitDv",+V(m)):(this._set("visitDv",0),j(m,"0")),V(_)?this._set("visitId",V(_)):(this._set("visitId",N()),j(_,this._get("visitId")))}function Q(){var t=V(v);t&&+new Date-t>=y&&(this._set("visitId",N()),this._set("visitDv",0),j(_,this._get("visitId")),j(m,"0")),j(v,(+new Date).toString())}function W(){var t=this,e=this._get("autoScrollTrack"),a=G().toPath,o=e&&e[a];if(o){var i={division10:[],division4:[],division3:[]},n=wx.createSelectorQuery();n.select("#".concat(o)).boundingClientRect(),n.selectViewport().boundingClientRect(),n.exec((function(e){if(!e[0])return console.warn("未找到容器节点,将不会上报scroll事件"),t._set("scrollLoad",!1);var a=e[0].height;for(var o in i)for(var n=+o.replace(/\D/g,""),s=0;s<n;s++)i[o].push({line:Math.ceil(a/n*(s+1)),isShow:!1});e[1]&&e[1].height&&t._set("winHeight",e[1].height),t._set("scrollStatus",i),t._set("scrollLoad",!0),$(-e[0].top)}))}}function G(){var t=getCurrentPages(),e={fromPath:"",fromParams:{},toPath:"",toParams:{}};return e.fromPath=t[t.length-2]?t[t.length-2].route||t[t.length-2].__route__:"",e.fromParams=t[t.length-2]?t[t.length-2].options:{},e.toPath=t[t.length-1]?t[t.length-1].route||t[t.length-1].__route__:"",e.toParams=t[t.length-1]?t[t.length-1].options:{},e}function X(t,a,o){var i=t[a],n=L[o];t[a]?t[a]=function(){n.apply(e,arguments),i.apply(this,arguments)}:t[a]=function(){n.apply(e,arguments)}}function Y(t){t.query=t.query||{},t.referrerInfo=t.referrerInfo||{},this._set("userSourceId",t.query[S]||""),this._set("shareSourceId",t.query[P]||""),this._set("channelOption",t)}function Z(){var t=this;F.call(this),this.track("page_enter"),this._set("pageLoad",!0),setTimeout((function(){W.call(t)}),d)}function $(t){var e=t&&t.scrollTop;if(!isNaN(e)&&M._get("pageLoad")&&M._get("scrollLoad")){var a=M._get("scrollStatus"),o=M._get("winHeight");for(var i in a)for(var n=0;n<a[i].length;n++)if(a[i][n].line<=e+o&&!a[i][n].isShow){var s={};s[i]=n+1,M.track("scroll",{extInfo:s,trackType:1}),a[i][n].isShow=!0,M._set("scrollStatus",a)}}}function tt(t,a,o){if(t.$SD_DATA=e,"mpvue"===n.usePlugin)return t;O[a].apply(this,o)}function et(t){try{X(t,"onLoad","pageLoad"),X(t,"onShow","pageShow"),X(t,"onPageScroll","pageScroll"),"function"==typeof t.onShareAppMessage&&function(t){var a=t.onShareAppMessage;t.onShareAppMessage=function(t){var o=N(),n=t.from,s=t.target||{dataset:{}};e._set("selfShareId",o);var r=a.call(this,t,o),c=r.path,h=q({userSourceId:E("cryptoIdKey"),shareSourceId:o});!s.dataset&&(s.dataset={});var u=s.dataset.ext||{},l=s.dataset.code||0;return-1===c.indexOf("?")?r.path="".concat(c,"?").concat(h):r.path="".concat(c,"&").concat(h),"menu"!==n&&s.dataset.ext||(u=r.ext||{}),delete r.ext,e.track("share",{elementCode:l,extInfo:i({shareId:o,shareTo:r.path,shareAction:n},u)}),r}}(t)}catch(t){}}function at(){var t=this._get("appId");if(t)return t;try{return t=wx.getAccountInfoSync().miniProgram.appId||"",this._set("appId",t),t}catch(t){return""}}function ot(){var t=this,e=[],a=this._get("conditionInfo");a.authorizationV2||(a.authorizationV2=E("tokenKey"),this._set("conditionInfo",a));var o={projectId:this._get("clientAppKey"),projectConfig:"",selfTag:this._get("cookieId"),debug:this._get("abDebug")||"",extInfo:JSON.stringify(a),platform:"weapp",systemInfo:JSON.stringify(this._get("getSystemInfo"))},i=this._get("expConditionMap");if(i[o.extInfo])return this._set("abConfigFinish",!0),nt.call(this,i[o.extInfo]);for(var n in o)e.push(n+"="+o[n]);var s=setTimeout((function(){JSON.stringify(t._get("conditionInfo"))===o.extInfo&&(t._set("isOverTime",!0),nt.call(t))}),f);wx.request({url:l,method:"POST",data:e.join("&"),header:{"content-type":"application/x-www-form-urlencoded"},complete:function(a){try{var n=V(p),r={reqParams:e.join("&"),errorNo:{}},c=null;try{r.errorNo=a.data,c=a.data.data.result}catch(t){r.errorNo=a}if(t.track("hawkeye_query",{elementCode:0,extInfo:r}),c&&(i[o.extInfo]=c),JSON.stringify(t._get("conditionInfo"))!==o.extInfo)return clearTimeout(s);t._set("abConfigFinish",!0),c&&(t._get("abDebug")?n.ab.debug=c||[]:n.ab.apps[t._get("clientAppKey")]=c,j(p,n)),t._get("isOverTime")||(clearTimeout(s),nt.call(t,c))}catch(e){nt.call(t)}}})}function it(){try{var t=wx.getSystemInfoSync()||{},e={os:t.system&&t.system.split(" ")[0]||"",os_v:t.system&&t.system.split(" ")[1]||"",d_i:t.brand||"",d_m:t.model||"",s_w:t.screenWidth||"",s_h:t.screenHeight||"",l:t.language||"",p:"wechat",sdk_v:s};this._set("logSystemInfo",t),this._set("getSystemInfo",e)}catch(t){}}function nt(t){var e=V(p),a=e.ab.apps[this._get("clientAppKey")]||[],o={},i=this._get("abDefaultVars");this._get("abDebug")&&(a=e.ab.debug||[],console.log("[SD_DATA] A/B Test-debug模式")),t&&(a=t);for(var n=0;n<a.length;n++)for(var s in a[n].extInfo)o[s]={value:a[n].extInfo[s],version:a[n].version,expId:a[n].experimentId};for(var r in i)void 0===o[r]&&(o[r]=i[r]);this._set("abVars",o),st.call(this)}function st(){var t=this,e=this._get("abVarsCallBack"),a=this._get("conditionCallBack"),o=this._get("abVars"),i=this._get("testCode");"function"==typeof a&&(a(),this._set("conditionCallBack",null)),e.forEach((function(e){if(void 0===o[e.vars])throw Error("[SD-DATA] "+e.vars+" is not define in hawkeye and abDefaultVars or not set appKey, please check it");if(o[e.vars].expId){var a={expId:o[e.vars].expId,version:o[e.vars].version};-1!==i.map((function(t){return t.expId===a.expId&&t.version===a.version})).indexOf(!0)||(i.push(a),t._set("testCode",i)),t.track("hawkeye",{elementCode:0},(function(){e.callback(o[e.vars].value)}))}else e.callback(o[e.vars])})),this._set("abVarsCallBack",[])}function rt(){var t={abDebug:!1,debug:!1,pageMessage:{},url:u,version:s,asyncTime:f,logSystemInfo:{},getSystemInfo:{},appId:"",appType:"",channelOption:{referrerInfo:{}},clientAppKey:"",conditionCallBack:null,abVarsCallBack:[],abConfigFinish:!1,isOverTime:!1,abDefaultVars:{},abVars:{},autoScrollTrack:{},scrollStatus:{},conditionInfo:{},expConditionMap:{},clientAdMsg:{},clientRtmMsg:{},winHeight:0,dataFromCookie:{},testCode:[],authorizationV2:"",pageLoad:!1,scrollLoad:!1,ajaxTimer:null,biz:"",subChannel:"",cookieId:"",userSourceId:"",shareSourceId:"",selfShareId:"",isNew:0,visitId:"",visitDv:0,actionType:"weapp"};this._set=function(e,a){t[e]=a},this._get=function(e){return t[e]}}return rt.setConfig=function(t){if(Object.assign(n,t),!e&&(e=new rt,!n.usePlugin))try{App=e.App,Page=e.Page,Component=e.Component,global.App=e.App,global.Page=e.Page,global.Component=e.Component}catch(t){}return e},rt.prototype={constructor:rt,App:function(t){return X(t,"onLaunch","appLaunch"),X(t,"onShow","appShow"),X(t,"onHide","appHide"),tt.call(this,t,"App",arguments)},Page:function(t){return et.call(this,t),tt.call(this,t,"Page",arguments)},Component:function(t){var e=t.methods||{};return et.call(this,e),tt.call(this,t,"Component",arguments)},init:function(e,a){if(!e||"object"!==t(e).toLocaleLowerCase())throw Error("[SD-DATA] Method init’s argument position 0’s type must be require object");var o={fromPath:"",fromParams:{},toPath:a.path,toParams:a.query},i=V(b),n=V(p);M=this,i?this._set("cookieId",i):z.call(this),n?n.version!==r&&R():R(),B.call(this),this._set("biz",e.biz||""),this._set("appType",e.appType||0),this._set("appVersion",e.appVersion),this._set("clientAppKey",e.appKey),this._set("conditionInfo",e.condition||{}),this._set("abDefaultVars",e.abDefaultVars),this._set("autoScrollTrack",e.autoScrollTrack),this._set("abDebug",a.query[w]||""),this._set("pageMessage",o),this._set("appId",e.appId),this._set("clientAdMsg",{skuId:a.query[k]||"",audience:a.query[D]||"",envId:a.query[x]||"",adCreativeId:a.query[T]||"",adPositionId:a.query[C]||""}),this._set("clientRtmMsg",{cepId:a.query[A]}),at.call(this),it.call(this),Y.call(this,a),U.call(this),Q.call(this),this.track("start"),e.appKey&&ot.call(this)},setAbCondition:function(t){var e=this;return new Promise((function(a,o){e._set("conditionCallBack",a),e._set("conditionInfo",t),e._set("isOverTime",!1),e._set("abConfigFinish",!1),ot.call(e)}))},track:function(t,e,a){if("page_enter"===t){var o=this._get("pageMessage"),i=G();this._get("visitDv")>1&&!i.fromPath&&(i.fromPath=o.toPath,i.fromParams=o.toParams),i.toParams&&i.toParams[C]&&i.toParams[T]&&this._set("clientAdMsg",{skuId:i.toParams[k],audience:i.toParams[D],envId:i.toParams[x],adCreativeId:i.toParams[T],adPositionId:i.toParams[C]}),i.toParams&&i.toParams[A]&&this._set("clientRtmMsg",{cepId:i.toParams[A]}),this._set("pageMessage",i)}if(e&&e.elementCode&&!/^[0-9]+.?[0-9]*$/.test(e.elementCode))throw Error("[SD-DATA]] The elementCode in the track method must be a number");var n=J.call(this,t,e);H.call(this,n,a),j(v,(+new Date).toString())},debug:function(){this._set("debug",!0)},getVars:function(t){var e=this,a=this._get("abVarsCallBack");return new Promise((function(o,i){a.push({callback:o,vars:t}),e._set("abVarsCallBack",a),(e._get("isOverTime")||e._get("abConfigFinish")||!e._get("clientAppKey"))&&st.call(e)}))},getWvMsg:function(){var t=["weapp",this._get("appType"),this._get("visitId"),this._get("visitDv"),this.getSelfTag(),s];try{return encodeURIComponent(t.join("-_-"))}catch(e){return t.join("-_-")}},getCommonMes:function(){var t=this._get("pageMessage").toParams||{},e=this._get("clientAdMsg"),a=this._get("clientRtmMsg"),o={v:c,p:"weapp",cm:{c:t.channel||"",sc:t.subchannel||"",v:this._get("visitId")+"-"+this._get("visitDv"),us:this._get("userSourceId")||"",ss:this._get("shareSourceId")||"",wx:""},tc:this._get("testCode"),ac:{c:e[T]||"",p:e[C]||"",s:e[k]||"",a:e[D]||"",e:e[x]||""},rc:{c:a[A]||""},tfc:{}};return K(JSON.stringify(o))},getSelfTag:function(){return this._get("cookieId")},getVersion:function(){return this._get("version")},isNewUser:function(){return!!this._get("isNew")},getShareSourceId:function(){return this._get("shareSourceId")},getUserSourceId:function(){return this._get("userSourceId")},getShareId:function(){return this._get("selfShareId")},createUuid:function(){return N()},getQRCodePath:function(t){var a=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},o=N(),n=q({userSourceId:E("cryptoIdKey"),shareSourceId:o});return t=-1===t.indexOf("?")?"".concat(t,"?").concat(n):"".concat(t,"&").concat(n),e.track("create_qrcode",{extInfo:i({shareId:o,shareTo:t},a)}),t}},rt}));