index.js 1.7 KB
Newer Older
1
import {
M
mehaotian 已提交
2
	get_platform_name,
3
	get_page_vm
M
mehaotian 已提交
4
} from './utils/pageInfo.js'
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 43 44 45 46 47
import Stat from './core/stat.js'
const stat = Stat.getInstance()

// 用于判断是隐藏页面还是卸载页面
let isHide = false

const lifecycle = {
	onLaunch(options) {
		// 进入应用上报数据
		stat.launch(options, 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)
	}
}


M
mehaotian 已提交
48 49
function main() {
	// console.log('--- 统计开启')
50 51 52
	if (process.env.NODE_ENV === 'development') {
		uni.report = function(type, options) {}
	} else {
M
mehaotian 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
		// #ifdef VUE3
		uni.onCreateVueApp((app) => {
			app.mixin(lifecycle)
			uni.report = function(type, options) {
				stat.sendEvent(type, options)
			}
		})
		
		if (get_platform_name() !== 'h5' && get_platform_name() !== 'n') {
			uni.onAppHide(() => {
				stat.appHide(get_page_vm());
			})
			uni.onAppShow(() => {
				stat.appShow(get_page_vm());
			})
		}
		// #endif
		
		// #ifndef VUE3
		const Vue = require('vue');
		(Vue.default || Vue).mixin(lifecycle);
		uni.report = function(type, options) {
		  stat.sendEvent(type, options);
		};
		// #endif
	}
79 80
}

M
mehaotian 已提交
81
main()