diff --git a/packages/uni-stat/src/index.js b/packages/uni-stat/src/index.js index e1f7efca000cd57cd29ea5256e5c34ae0eb3fc5a..fa266d0c6c4c2c3edceeb17feb05a57705d2f53a 100644 --- a/packages/uni-stat/src/index.js +++ b/packages/uni-stat/src/index.js @@ -3,15 +3,12 @@ const stat = Stat.getInstance() let isHide = false const lifecycle = { onLaunch(options) { - console.log('report onLaunch init') stat.report(options, this) }, onReady() { - console.log('report onReady init') stat.ready(this) }, onLoad(options) { - console.log('report onLoad init') stat.load(options, this) // 重写分享,获取分享上报事件 if (this.$scope && this.$scope.onShareAppMessage) { @@ -23,17 +20,14 @@ const lifecycle = { } }, onShow() { - console.log('report onShow init') isHide = false stat.show(this) }, onHide() { - console.log('report onHide init') isHide = true stat.hide(this) }, onUnload() { - console.log('report onUnload init') if (isHide) { isHide = false return @@ -41,29 +35,23 @@ const lifecycle = { stat.hide(this) }, onError(e) { - console.log('report onError init') stat.error(e) }, } function main() { - console.log('stat onload ----') - uni.onAppLaunch((options) => { - // 小程序平台此时也无法获取getApp,统一在options中传递一个app对象 - options.app.$vm.$.appContext.app.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) - // } - // } + if (process.env.NODE_ENV === 'development') { + uni.report = function (type, options) {} + } else { + uni.onAppLaunch((options) => { + stat.report(options) + // 小程序平台此时也无法获取getApp,统一在options中传递一个app对象 + options.app.$vm.$.appContext.app.mixin(lifecycle) + uni.report = function (type, options) { + stat.sendEvent(type, options) + } + }) + } } main() diff --git a/packages/uni-stat/src/parameter.js b/packages/uni-stat/src/parameter.js index d2d1444d853a529dca444ace1aa83db3682a0d60..405fea1c31653087c31f6810f288a19d8eedd296 100644 --- a/packages/uni-stat/src/parameter.js +++ b/packages/uni-stat/src/parameter.js @@ -244,18 +244,14 @@ export const getRoute = () => { if (getPlatformName() === 'bd') { return _self.$mp && _self.$mp.page.is } else { - return _self.route || (_self.$mp && _self.$mp.page.route) + return _self.route || (_self.$scope && _self.$scope.route) } } -export const getPageRoute = (self) => { - let route = getRoute() - let query = self._query - let str = - query && JSON.stringify(query) !== '{}' ? '?' + JSON.stringify(query) : '' - // clear - self._query = '' - return route + str +export const getPageRoute = (_this) => { + let pageVm = _this.self + let page = pageVm.$page || pageVm.$scope.$page + return page.fullPath === '/' ? page.route : page.fullPath } export const getPageTypes = (self) => { diff --git a/packages/uni-stat/src/stat.js b/packages/uni-stat/src/stat.js index 691a6c2889a6a0d40363eb4f8a81517c15c1125a..a11b52e1f66b06a5ac4ffba51d9fd0ab6e8a1c5a 100644 --- a/packages/uni-stat/src/stat.js +++ b/packages/uni-stat/src/stat.js @@ -25,8 +25,18 @@ import { } from './parameter' import { STAT_URL, STAT_VERSION, STAT_H5_URL, OPERATING_TIME } from './config' +let titleJsons = {} +// #ifdef MP +let pagesTitle = require('uni-pages?{"type":"style"}').default +pagesTitle = pagesTitle.pages +for (let i in pagesTitle) { + titleJsons[i] = pagesTitle[i].navigationBarTitleText || '' +} +// #endif +// #ifndef MP +titleJsons = process.env.UNI_STAT_TITLE_JSON +// #endif -const titleJsons = process.env.UNI_STAT_TITLE_JSON const statConfig = { appid: process.env.UNI_APP_ID, } @@ -80,6 +90,62 @@ class Util { sw: resultOptions.screenWidth, sh: resultOptions.screenHeight, } + // 注册拦截器 + let registerInterceptor = + typeof uni.addInterceptor === 'function' && + process.env.NODE_ENV !== 'development' + if (registerInterceptor) { + this.addInterceptorInit() + this.interceptLogin() + this.interceptShare(true) + this.interceptRequestPayment() + } + } + + addInterceptorInit() { + let self = this + uni.addInterceptor('setNavigationBarTitle', { + invoke(args) { + self._navigationBarTitle.page = args.title + }, + }) + } + + interceptLogin() { + let self = this + uni.addInterceptor('login', { + complete() { + self._login() + }, + }) + } + + interceptShare(type) { + let self = this + if (!type) { + self._share() + return + } + uni.addInterceptor('share', { + success() { + self._share() + }, + fail() { + self._share() + }, + }) + } + + interceptRequestPayment() { + let self = this + uni.addInterceptor('requestPayment', { + success() { + self._payment('pay_success') + }, + fail() { + self._payment('pay_fail') + }, + }) } getIsReportData() { @@ -119,12 +185,11 @@ class Util { _pageShow() { const route = getPageRoute(this) const routepath = getRoute(this) - this._navigationBarTitle.config = titleJsons[routepath] || '' - + this._navigationBarTitle.config = + (titleJsons && titleJsons[routepath]) || '' if (this.__licationShow) { getFirstTime() this.__licationShow = false - // console.log('这是 onLauch 之后执行的第一次 pageShow ,为下次记录时间做准备'); this._lastPageRoute = route return } @@ -146,7 +211,10 @@ class Util { if (!this.__licationHide) { getLastTime() const time = getResidenceTime('page') - const route = getPageRoute(this) + let route = getPageRoute(this) + if (!this._lastPageRoute) { + this._lastPageRoute = route + } this._sendPageRequest({ url: route, urlref: this._lastPageRoute, @@ -190,6 +258,8 @@ class Util { } _sendReportRequest(options) { this._navigationBarTitle.lt = '1' + this._navigationBarTitle.config = + (titleJsons && titleJsons[options.path]) || '' let query = options.query && JSON.stringify(options.query) !== '{}' ? '?' + JSON.stringify(options.query) @@ -316,7 +386,6 @@ class Util { requestData[data.lt] = [] } requestData[data.lt].push(data) - if (getPlatformName() === 'n') { uni.setStorageSync('__UNI__STAT__DATA', requestData) } @@ -378,11 +447,7 @@ class Util { url: STAT_URL, method: 'POST', data: optionsData, - success: () => { - // if (process.env.NODE_ENV === 'development') { - // console.log('stat request success'); - // } - }, + success: () => {}, fail: (e) => { if (++this._retry < 3) { setTimeout(() => { @@ -432,77 +497,16 @@ class Stat extends Util { constructor() { super() this.instance = null - // 注册拦截器 - if ( - typeof uni.addInterceptor === 'function' && - process.env.NODE_ENV !== 'development' - ) { - this.addInterceptorInit() - this.interceptLogin() - this.interceptShare(true) - this.interceptRequestPayment() - } - } - - addInterceptorInit() { - let self = this - uni.addInterceptor('setNavigationBarTitle', { - invoke(args) { - self._navigationBarTitle.page = args.title - }, - }) - } - - interceptLogin() { - let self = this - uni.addInterceptor('login', { - complete() { - self._login() - }, - }) - } - - interceptShare(type) { - let self = this - if (!type) { - self._share() - return - } - uni.addInterceptor('share', { - success() { - self._share() - }, - fail() { - self._share() - }, - }) - } - - interceptRequestPayment() { - let self = this - uni.addInterceptor('requestPayment', { - success() { - self._payment('pay_success') - }, - fail() { - self._payment('pay_fail') - }, - }) } report(options, self) { - this.self = self + // TODO 需要确认如果不用 $vm ,其他平台会不会出错 setPageResidenceTime() this.__licationShow = true this._sendReportRequest(options, true) } load(options, self) { - // if (!self.$scope && !self.$mp) { - // const page = getCurrentPages() - // console.log(); - // self.$scope = page[page.length - 1] - // } this.self = self this._query = options } @@ -515,13 +519,7 @@ class Stat extends Util { this._applicationShow(self) } } - - ready(self) { - // this.self = self; - // if (getPageTypes(self)) { - // this._pageShow(self); - // } - } + ready(self) {} hide(self) { this.self = self if (getPageTypes(self)) { @@ -535,7 +533,6 @@ class Stat extends Util { if (process.env.NODE_ENV === 'development') { console.info('当前运行环境为开发者工具,不上报数据。') } - // return; } let emVal = '' if (!em.message) {