From 919c7d4da13fb6edabbdac880a1d45d6d41201dd Mon Sep 17 00:00:00 2001 From: mehaotian <490272692@qq.com> Date: Thu, 22 Aug 2019 01:21:08 +0800 Subject: [PATCH] =?UTF-8?q?fix(stat)=20:=20=E4=BF=AE=E5=A4=8D=E8=B7=AF?= =?UTF-8?q?=E7=94=B1=E9=94=99=E8=AF=AF=E9=97=AE=E9=A2=98=EF=BC=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/uni-stat/dist/index.js | 83 ++++++++++++++++---------- packages/uni-stat/package.json | 2 +- packages/uni-stat/src/parameter.js | 10 ++-- packages/uni-stat/src/stat.js | 93 +++++++++++++++++++----------- 4 files changed, 119 insertions(+), 69 deletions(-) diff --git a/packages/uni-stat/dist/index.js b/packages/uni-stat/dist/index.js index 9294a2e96..7259e501e 100644 --- a/packages/uni-stat/dist/index.js +++ b/packages/uni-stat/dist/index.js @@ -237,7 +237,7 @@ const getRoute = () => { if (getPlatformName() === 'bd') { return _self.$mp && _self.$mp.page.is; } else { - return _self.$scope && _self.$scope.route || _self.$mp && _self.$mp.page.route; + return (_self.$scope && _self.$scope.route) || (_self.$mp && _self.$mp.page.route); } }; @@ -252,14 +252,14 @@ const getPageRoute = (self) => { if (getPlatformName() === 'bd') { return _self.$mp && _self.$mp.page.is + str; } else { - return _self.$scope && _self.$scope.route + str || _self.$mp && _self.$mp.page.route + str; + return (_self.$scope && _self.$scope.route + str )|| (_self.$mp && _self.$mp.page.route + str); } }; const getPageTypes = (self) => { - if (self.mpType === 'page' || self.$mp && self.$mp.mpType === 'page') { - return true; - } + if (self.mpType === 'page' || (self.$mp && self.$mp.mpType === 'page') || self.$options.mpType === 'page') { + return true; + } return false; }; @@ -371,8 +371,9 @@ class Util { getLastTime(); const time = getResidenceTime(); getFirstTime(); + const route = getPageRoute(this); this._sendHideRequest({ - urlref: this._lastPageRoute, + urlref: route, urlref_ts: time.residenceTime }, type); } @@ -562,20 +563,32 @@ class Util { data.ttc = title.report; let requestData = this._reportingRequestData; + if (getPlatformName() === 'n') { + requestData = uni.getStorageSync('__UNI__STAT__DATA') || {}; + } if (!requestData[data.lt]) { - this._reportingRequestData[data.lt] = []; + requestData[data.lt] = []; + } + requestData[data.lt].push(data); + + if (getPlatformName() === 'n') { + uni.setStorageSync('__UNI__STAT__DATA', requestData); } - this._reportingRequestData[data.lt].push(data); if (getPageResidenceTime() < OPERATING_TIME && !type) { return } + let uniStatData = this._reportingRequestData; + if (getPlatformName() === 'n') { + uniStatData = uni.getStorageSync('__UNI__STAT__DATA'); + } // 时间超过,重新获取时间戳 setPageResidenceTime(); let firstArr = []; let contentArr = []; let lastArr = []; - for (let i in this._reportingRequestData) { - const rd = this._reportingRequestData[i]; + + for (let i in uniStatData) { + const rd = uniStatData[i]; rd.forEach((elm) => { const newData = getSplicing(elm); if (i === 0) { @@ -594,32 +607,40 @@ class Util { t: time, //发送请求时的时间戮 requests: JSON.stringify(firstArr), }; + this._reportingRequestData = {}; + if (getPlatformName() === 'n') { + uni.removeStorageSync('__UNI__STAT__DATA'); + } if (data.ut === 'h5') { this.imageRequest(optionsData); return } - uni.request({ - url: STAT_URL, - method: 'POST', - data: optionsData, - success: () => { - if (process.env.NODE_ENV === 'development') { - console.log('stat request success'); - } - }, - fail: (e) => { - if (process.env.NODE_ENV === 'development') { - console.log('stat request fail', e); - } - if (++this._retry < 3) { - setTimeout(() => { - this.request(data); - }, 1000); + + setTimeout(() => { + uni.request({ + url: STAT_URL, + method: 'POST', + data: optionsData, + success: () => { + if (process.env.NODE_ENV === 'development') { + console.log('stat request success'); + } + }, + fail: (e) => { + if (process.env.NODE_ENV === 'development') { + console.log('stat request fail', e); + } + if (++this._retry < 3) { + setTimeout(() => { + this.request(data); + }, 1000); + } } - } - }); + }); + }, 200); + } /** * h5 请求 @@ -722,6 +743,10 @@ class Stat extends Util { } load(options, self) { + if (!self.$scope && !self.$mp) { + const page = getCurrentPages(); + self.$scope = page[page.length - 1]; + } this.self = self; this._query = options; } diff --git a/packages/uni-stat/package.json b/packages/uni-stat/package.json index ca9d0fef4..16f55d24b 100644 --- a/packages/uni-stat/package.json +++ b/packages/uni-stat/package.json @@ -1,6 +1,6 @@ { "name": "@dcloudio/uni-stat", - "version": "0.0.108", + "version": "0.0.111", "description": "", "main": "dist/index.js", "scripts": { diff --git a/packages/uni-stat/src/parameter.js b/packages/uni-stat/src/parameter.js index 10ffb082b..81f1b7c26 100644 --- a/packages/uni-stat/src/parameter.js +++ b/packages/uni-stat/src/parameter.js @@ -236,7 +236,7 @@ export const getRoute = () => { if (getPlatformName() === 'bd') { return _self.$mp && _self.$mp.page.is; } else { - return _self.$scope && _self.$scope.route || _self.$mp && _self.$mp.page.route; + return (_self.$scope && _self.$scope.route) || (_self.$mp && _self.$mp.page.route); } }; @@ -251,14 +251,14 @@ export const getPageRoute = (self) => { if (getPlatformName() === 'bd') { return _self.$mp && _self.$mp.page.is + str; } else { - return _self.$scope && _self.$scope.route + str || _self.$mp && _self.$mp.page.route + str; + return (_self.$scope && _self.$scope.route + str )|| (_self.$mp && _self.$mp.page.route + str); } }; export const getPageTypes = (self) => { - if (self.mpType === 'page' || self.$mp && self.$mp.mpType === 'page') { - return true; - } + if (self.mpType === 'page' || (self.$mp && self.$mp.mpType === 'page') || self.$options.mpType === 'page') { + return true; + } return false; } diff --git a/packages/uni-stat/src/stat.js b/packages/uni-stat/src/stat.js index 08e4ba9d9..cce8a095e 100644 --- a/packages/uni-stat/src/stat.js +++ b/packages/uni-stat/src/stat.js @@ -106,8 +106,9 @@ class Util { getLastTime(); const time = getResidenceTime(); getFirstTime(); + const route = getPageRoute(this); this._sendHideRequest({ - urlref: this._lastPageRoute, + urlref: route, urlref_ts: time.residenceTime }, type) } @@ -297,64 +298,84 @@ class Util { data.ttc = title.report; let requestData = this._reportingRequestData; + if (getPlatformName() === 'n') { + requestData = uni.getStorageSync('__UNI__STAT__DATA') || {} + } if (!requestData[data.lt]) { - this._reportingRequestData[data.lt] = []; + requestData[data.lt] = []; + } + requestData[data.lt].push(data); + + if (getPlatformName() === 'n') { + uni.setStorageSync('__UNI__STAT__DATA', requestData) } - this._reportingRequestData[data.lt].push(data); if (getPageResidenceTime() < OPERATING_TIME && !type) { return } + let uniStatData = this._reportingRequestData + if (getPlatformName() === 'n') { + uniStatData = uni.getStorageSync('__UNI__STAT__DATA') + } // 时间超过,重新获取时间戳 setPageResidenceTime(); - let firstArr = [] - let contentArr = [] - let lastArr = [] - for (let i in this._reportingRequestData) { - const rd = this._reportingRequestData[i] + let firstArr = []; + let contentArr = []; + let lastArr = []; + + for (let i in uniStatData) { + const rd = uniStatData[i]; rd.forEach((elm) => { - const newData = getSplicing(elm) + const newData = getSplicing(elm); if (i === 0) { - firstArr.push(newData) + firstArr.push(newData); } else if (i === 3) { - lastArr.push(newData) + lastArr.push(newData); } else { - contentArr.push(newData) + contentArr.push(newData); } - }) + }); } - firstArr.push(...contentArr, ...lastArr) + firstArr.push(...contentArr, ...lastArr); let optionsData = { usv: STAT_VERSION, //统计 SDK 版本号 t: time, //发送请求时的时间戮 requests: JSON.stringify(firstArr), + }; + + this._reportingRequestData = {}; + if (getPlatformName() === 'n') { + uni.removeStorageSync('__UNI__STAT__DATA') } - this._reportingRequestData = {} if (data.ut === 'h5') { this.imageRequest(optionsData) return } - uni.request({ - url: STAT_URL, - method: 'POST', - data: optionsData, - success: () => { - if (process.env.NODE_ENV === 'development') { - console.log('stat request success'); - } - }, - fail: (e) => { - if (process.env.NODE_ENV === 'development') { - console.log('stat request fail', e); - } - if (++this._retry < 3) { - setTimeout(() => { - this.request(data); - }, 1000); + + setTimeout(() => { + uni.request({ + url: STAT_URL, + method: 'POST', + data: optionsData, + success: () => { + if (process.env.NODE_ENV === 'development') { + console.log('stat request success'); + } + }, + fail: (e) => { + if (process.env.NODE_ENV === 'development') { + console.log('stat request fail', e); + } + if (++this._retry < 3) { + setTimeout(() => { + this.request(data); + }, 1000); + } } - } - }); + }); + }, 200) + } /** * h5 请求 @@ -457,6 +478,10 @@ class Stat extends Util { } load(options, self) { + if (!self.$scope && !self.$mp) { + const page = getCurrentPages() + self.$scope = page[page.length - 1] + } this.self = self; this._query = options; } -- GitLab