index.js 1.1 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
import Stat from './stat.js'
const stat = Stat.getInstance()
let isHide = false
const lifecycle = {
  onLaunch(options) {
    stat.report(options, this)
  },
  onReady() {
    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)
      }
    }
  },
  onShow() {
    isHide = false
    stat.show(this)
  },
  onHide() {
    isHide = true
    stat.hide(this)
  },
  onUnload() {
    if (isHide) {
      isHide = false
      return
    }
    stat.hide(this)
  },
  onError(e) {
    stat.error(e)
  },
}

function main() {
43 44 45
  if (process.env.NODE_ENV === 'development') {
    uni.report = function (type, options) {}
  } else {
46 47
    uni.onCreateVueApp((app) => {
      app.mixin(lifecycle)
48 49 50 51 52
      uni.report = function (type, options) {
        stat.sendEvent(type, options)
      }
    })
  }
fxy060608's avatar
fxy060608 已提交
53 54 55
}

main()