From cbe40e141c88e51dc9d765de36d3fb70766a4a26 Mon Sep 17 00:00:00 2001 From: mehaotian <490272692@qq.com> Date: Wed, 29 Apr 2020 20:01:57 +0800 Subject: [PATCH] =?UTF-8?q?fix(stat)=20:=20=E6=8A=A5=E6=95=B0=E5=89=8D?= =?UTF-8?q?=E5=92=A8=E8=AF=A2=E6=9C=8D=E5=8A=A1=E5=99=A8=EF=BC=8C=E6=98=AF?= =?UTF-8?q?=E5=90=A6=E9=95=BF=E4=B9=85=E6=9C=AA=E7=99=BB=E5=BD=95=EF=BC=8C?= =?UTF-8?q?=E8=8B=A5=E6=98=AF=EF=BC=8C=E5=88=99=E6=9C=AC=E5=9C=B0=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE24=E5=B0=8F=E6=97=B6=E5=86=85=E4=B8=8D=E4=B8=8A?= =?UTF-8?q?=E6=8A=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/uni-stat/dist/index.js | 186 ++++++++++++++++++++--------- packages/uni-stat/src/index.js | 34 +++--- packages/uni-stat/src/parameter.js | 103 +++++++++++++--- packages/uni-stat/src/stat.js | 53 ++++---- 4 files changed, 263 insertions(+), 113 deletions(-) diff --git a/packages/uni-stat/dist/index.js b/packages/uni-stat/dist/index.js index b157bc348..e99cc3db1 100644 --- a/packages/uni-stat/dist/index.js +++ b/packages/uni-stat/dist/index.js @@ -7,6 +7,7 @@ const PAGE_PVER_TIME = 1800; const APP_PVER_TIME = 300; const OPERATING_TIME = 10; +const statConfig = require('uni-stat-config').default || require('uni-stat-config'); const UUID_KEY = '__DC_STAT_UUID'; const UUID_VALUE = '__DC_UUID_VALUE'; @@ -82,12 +83,12 @@ const getPlatformName = () => { }; const getPackName = () => { - let packName = ''; - if (getPlatformName() === 'wx' || getPlatformName() === 'qq') { - // 兼容微信小程序低版本基础库 - if(uni.canIUse('getAccountInfoSync')){ - packName = uni.getAccountInfoSync().miniProgram.appId || ''; - } + let packName = ''; + if (getPlatformName() === 'wx' || getPlatformName() === 'qq') { + // 兼容微信小程序低版本基础库 + if (uni.canIUse('getAccountInfoSync')) { + packName = uni.getAccountInfoSync().miniProgram.appId || ''; + } } return packName }; @@ -255,22 +256,22 @@ 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') || self.$options.mpType === 'page') { - return true; - } + if (self.mpType === 'page' || (self.$mp && self.$mp.mpType === 'page') || self.$options.mpType === 'page') { + return true; + } return false; }; const calibration = (eventName, options) => { - // login 、 share 、pay_success 、pay_fail 、register 、title - if(!eventName){ - console.error(`uni.report 缺少 [eventName] 参数`); - return true + // login 、 share 、pay_success 、pay_fail 、register 、title + if (!eventName) { + console.error(`uni.report 缺少 [eventName] 参数`); + return true } if (typeof eventName !== 'string') { console.error(`uni.report [eventName] 参数类型错误,只能为 String 类型`); @@ -294,11 +295,78 @@ const calibration = (eventName, options) => { if (eventName === 'title' && typeof options !== 'string') { console.error('uni.report [eventName] 参数为 title 时,[options] 参数只能为 String 类型'); return true - } + } +}; + +const Report_Data_Time = 'Report_Data_Time'; + +const isReportData = () => { + return new Promise((resolve, reject) => { + let start_time = ''; + let end_time = new Date().getTime(); + let diff_time = 60 * 1000 * 60 * 24; + try { + start_time = uni.getStorageSync(Report_Data_Time); + } catch (e) { + start_time = ''; + } + + if (!start_time) { + uni.setStorageSync(Report_Data_Time, end_time); + start_time = end_time; + } + if ((end_time - start_time) > diff_time) { + requestData(({ + enable + }) => { + uni.setStorageSync(Report_Data_Time, end_time); + if (enable === 1) { + resolve(); + } + }); + } + }) +}; + +const Report_Status = 'Report_Status'; +const requestData = (done) => { + let formData = { + usv: STAT_VERSION, + conf: encodeURIComponent({ + ak: statConfig.appid + }) + }; + uni.request({ + url: STAT_URL, + method: 'GET', + data: formData, + success: (res) => { + const {data} = res; + if (data.ret === 0) { + typeof done === 'function' && done({ + enable: data.enable + }); + } + }, + fail: (e) => { + let report_status_code = 1; + try { + report_status_code = uni.getStorageSync(Report_Status); + } catch (e) { + report_status_code = 1; + } + if (report_status_code === 1) { + typeof done === 'function' && done({ + enable: res.enable + }); + } + // console.error('统计请求错误'); + } + }); }; const PagesJson = require('uni-pages?{"type":"style"}').default; -const statConfig = require('uni-stat-config').default || require('uni-stat-config'); +const statConfig$1 = require('uni-stat-config').default || require('uni-stat-config'); const resultOptions = uni.getSystemInfoSync(); @@ -328,7 +396,7 @@ class Util { uuid: getUuid(), ut: getPlatformName(), mpn: getPackName(), - ak: statConfig.appid, + ak: statConfig$1.appid, usv: STAT_VERSION, v: getVersion(), ch: getChannel(), @@ -352,7 +420,11 @@ class Util { }; } - + + getIsReportData(){ + return isReportData() + } + _applicationShow() { if (this.__licationHide) { getLastTime(); @@ -547,7 +619,7 @@ class Util { } getLocation() { - if (statConfig.getLocation) { + if (statConfig$1.getLocation) { uni.getLocation({ type: 'wgs84', geocode: true, @@ -641,27 +713,29 @@ class Util { } this._sendRequest(optionsData); } - _sendRequest(optionsData) { - uni.request({ - url: STAT_URL, - method: 'POST', - // header: { - // 'content-type': 'application/json' // 默认值 - // }, - data: optionsData, - success: () => { - // if (process.env.NODE_ENV === 'development') { - // console.log('stat request success'); - // } - }, - fail: (e) => { - if (++this._retry < 3) { - setTimeout(() => { - this._sendRequest(optionsData); - }, 1000); - } - } - }); + _sendRequest(optionsData) { + this.getIsReportData().then(()=>{ + uni.request({ + url: STAT_URL, + method: 'POST', + // header: { + // 'content-type': 'application/json' // 默认值 + // }, + data: optionsData, + success: () => { + // if (process.env.NODE_ENV === 'development') { + // console.log('stat request success'); + // } + }, + fail: (e) => { + if (++this._retry < 3) { + setTimeout(() => { + this._sendRequest(optionsData); + }, 1000); + } + } + }); + }); } /** * h5 请求 @@ -836,14 +910,14 @@ const lifecycle = { stat.ready(this); }, onLoad(options) { - stat.load(options, this); - // 重写分享,获取分享上报事件 - if (this.$scope && this.$scope.onShareAppMessage) { - let oldShareAppMessage = this.$scope.onShareAppMessage; - this.$scope.onShareAppMessage = function(options) { - stat.interceptShare(false); - return oldShareAppMessage.call(this, options) - }; + stat.load(options, this); + // 重写分享,获取分享上报事件 + if (this.$scope && this.$scope.onShareAppMessage) { + let oldShareAppMessage = this.$scope.onShareAppMessage; + this.$scope.onShareAppMessage = function(options) { + stat.interceptShare(false); + return oldShareAppMessage.call(this, options) + }; } }, onShow() { @@ -867,14 +941,14 @@ const lifecycle = { }; function main() { - if (process.env.NODE_ENV === 'development') { - uni.report = function(type, options) {}; - }else{ - const Vue = require('vue'); - (Vue.default || Vue).mixin(lifecycle); - uni.report = function(type, options) { - stat.sendEvent(type, options); - }; + if (process.env.NODE_ENV === 'development') { + uni.report = function(type, options) {}; + } else { + const Vue = require('vue'); + (Vue.default || Vue).mixin(lifecycle); + uni.report = function(type, options) { + stat.sendEvent(type, options); + }; } } diff --git a/packages/uni-stat/src/index.js b/packages/uni-stat/src/index.js index c5eb42fef..d2556c13b 100644 --- a/packages/uni-stat/src/index.js +++ b/packages/uni-stat/src/index.js @@ -1,4 +1,4 @@ -import Stat from './stat.js'; +import Stat from './stat.js'; const stat = Stat.getInstance(); let isHide = false const lifecycle = { @@ -9,14 +9,14 @@ const lifecycle = { stat.ready(this); }, onLoad(options) { - stat.load(options, this); - // 重写分享,获取分享上报事件 - if (this.$scope && this.$scope.onShareAppMessage) { - let oldShareAppMessage = this.$scope.onShareAppMessage; - this.$scope.onShareAppMessage = function(options) { - stat.interceptShare(false); - return oldShareAppMessage.call(this, options) - } + stat.load(options, this); + // 重写分享,获取分享上报事件 + if (this.$scope && this.$scope.onShareAppMessage) { + let oldShareAppMessage = this.$scope.onShareAppMessage; + this.$scope.onShareAppMessage = function(options) { + stat.interceptShare(false); + return oldShareAppMessage.call(this, options) + } } }, onShow() { @@ -40,14 +40,14 @@ const lifecycle = { } function main() { - if (process.env.NODE_ENV === 'development') { - uni.report = function(type, options) {}; - }else{ - const Vue = require('vue'); - (Vue.default || Vue).mixin(lifecycle); - uni.report = function(type, options) { - stat.sendEvent(type, options); - }; + if (process.env.NODE_ENV === 'development') { + uni.report = function(type, options) {}; + } else { + const Vue = require('vue'); + (Vue.default || Vue).mixin(lifecycle); + uni.report = function(type, options) { + stat.sendEvent(type, options); + }; } } diff --git a/packages/uni-stat/src/parameter.js b/packages/uni-stat/src/parameter.js index 1a4c8cdc5..0c05f3a83 100644 --- a/packages/uni-stat/src/parameter.js +++ b/packages/uni-stat/src/parameter.js @@ -1,8 +1,10 @@ import { PAGE_PVER_TIME, - APP_PVER_TIME + APP_PVER_TIME, + STAT_URL, + STAT_VERSION } from './config'; - +const statConfig = require('uni-stat-config').default || require('uni-stat-config'); const UUID_KEY = '__DC_STAT_UUID'; const UUID_VALUE = '__DC_UUID_VALUE'; @@ -78,12 +80,12 @@ export const getPlatformName = () => { } export const getPackName = () => { - let packName = '' - if (getPlatformName() === 'wx' || getPlatformName() === 'qq') { - // 兼容微信小程序低版本基础库 - if(uni.canIUse('getAccountInfoSync')){ - packName = uni.getAccountInfoSync().miniProgram.appId || ''; - } + let packName = '' + if (getPlatformName() === 'wx' || getPlatformName() === 'qq') { + // 兼容微信小程序低版本基础库 + if (uni.canIUse('getAccountInfoSync')) { + packName = uni.getAccountInfoSync().miniProgram.appId || ''; + } } return packName } @@ -254,22 +256,22 @@ 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') || self.$options.mpType === 'page') { - return true; - } + if (self.mpType === 'page' || (self.$mp && self.$mp.mpType === 'page') || self.$options.mpType === 'page') { + return true; + } return false; } export const calibration = (eventName, options) => { - // login 、 share 、pay_success 、pay_fail 、register 、title - if(!eventName){ - console.error(`uni.report 缺少 [eventName] 参数`); - return true + // login 、 share 、pay_success 、pay_fail 、register 、title + if (!eventName) { + console.error(`uni.report 缺少 [eventName] 参数`); + return true } if (typeof eventName !== 'string') { console.error(`uni.report [eventName] 参数类型错误,只能为 String 类型`); @@ -293,5 +295,72 @@ export const calibration = (eventName, options) => { if (eventName === 'title' && typeof options !== 'string') { console.error('uni.report [eventName] 参数为 title 时,[options] 参数只能为 String 类型'); return true - } + } +} + +const Report_Data_Time = 'Report_Data_Time' + +export const isReportData = () => { + return new Promise((resolve, reject) => { + let start_time = '' + let end_time = new Date().getTime() + let diff_time = 60 * 1000 * 60 * 24 + try { + start_time = uni.getStorageSync(Report_Data_Time) + } catch (e) { + start_time = '' + } + + if (!start_time) { + uni.setStorageSync(Report_Data_Time, end_time) + start_time = end_time + } + if ((end_time - start_time) > diff_time) { + requestData(({ + enable + }) => { + uni.setStorageSync(Report_Data_Time, end_time) + if (enable === 1) { + resolve(); + } + }); + } + }) +} + +const Report_Status = 'Report_Status' +const requestData = (done) => { + let formData = { + usv: STAT_VERSION, + conf: encodeURIComponent({ + ak: statConfig.appid + }) + } + uni.request({ + url: STAT_URL, + method: 'GET', + data: formData, + success: (res) => { + const {data} = res + if (data.ret === 0) { + typeof done === 'function' && done({ + enable: data.enable + }) + } + }, + fail: (e) => { + let report_status_code = 1 + try { + report_status_code = uni.getStorageSync(Report_Status) + } catch (e) { + report_status_code = 1 + } + if (report_status_code === 1) { + typeof done === 'function' && done({ + enable: res.enable + }) + } + // console.error('统计请求错误'); + } + }); } diff --git a/packages/uni-stat/src/stat.js b/packages/uni-stat/src/stat.js index bf5d1490a..cdfdf9880 100644 --- a/packages/uni-stat/src/stat.js +++ b/packages/uni-stat/src/stat.js @@ -22,7 +22,8 @@ import { getPageRoute, getRoute, getPageTypes, - calibration + calibration, + isReportData } from './parameter'; import { @@ -84,7 +85,11 @@ class Util { } } - + + getIsReportData(){ + return isReportData() + } + _applicationShow() { if (this.__licationHide) { getLastTime(); @@ -373,27 +378,29 @@ class Util { } this._sendRequest(optionsData) } - _sendRequest(optionsData) { - uni.request({ - url: STAT_URL, - method: 'POST', - // header: { - // 'content-type': 'application/json' // 默认值 - // }, - data: optionsData, - success: () => { - // if (process.env.NODE_ENV === 'development') { - // console.log('stat request success'); - // } - }, - fail: (e) => { - if (++this._retry < 3) { - setTimeout(() => { - this._sendRequest(optionsData); - }, 1000); - } - } - }); + _sendRequest(optionsData) { + this.getIsReportData().then(()=>{ + uni.request({ + url: STAT_URL, + method: 'POST', + // header: { + // 'content-type': 'application/json' // 默认值 + // }, + data: optionsData, + success: () => { + // if (process.env.NODE_ENV === 'development') { + // console.log('stat request success'); + // } + }, + fail: (e) => { + if (++this._retry < 3) { + setTimeout(() => { + this._sendRequest(optionsData); + }, 1000); + } + } + }); + }) } /** * h5 请求 -- GitLab