stat.js 2.3 KB
Newer Older
1
import Report from './report.js'
M
mehaotian 已提交
2

3 4 5 6 7 8 9 10 11 12
import {
	set_page_residence_time
} from '../utils/pageTime.js'
import {
	get_page_types,
	get_platform_name
} from '../utils/pageInfo.js'

class Stat extends Report {
	static getInstance() {
M
mehaotian 已提交
13 14
		if (!uni.__stat_instance) {
			uni.__stat_instance = new Stat()
15
		}
M
mehaotian 已提交
16
		return uni.__stat_instance
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 48 49 50 51 52 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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
	}
	constructor() {
		super()
	}

	/**
	 * 进入应用
	 * @param {Object} options 页面参数
	 * @param {Object} self	当前页面实例
	 */
	launch(options, self) {
		// 初始化页面停留时间  start
		let residence_time =  set_page_residence_time()
		this.__licationShow = true
		this.sendReportRequest(options, true)
	}
	load(options, self) {
		this.self = self
		this._query = options
	}

	appHide(self){
		this.applicationHide(self, true)
	}

	appShow(self){
		this.applicationShow(self)
	}

	show(self) {
		this.self = self
		if (get_page_types(self) === 'page') {
			this.pageShow(self)
		}
		
		// #ifdef VUE3
		if (get_platform_name() !== 'h5' && get_platform_name() !== 'n') {
			if (get_page_types(self) === 'app') {
				this.appShow(self)
			}
		}
		// #endif
		
		// #ifndef VUE3
		if (get_page_types(self) === 'app') {
			this.appShow(self)
		}
		// #endif
		
	}

	hide(self) {
		this.self = self
		if (get_page_types(self) === 'page') {
			this.pageHide(self)
		}
		
		// #ifdef VUE3
		if (get_platform_name() !== 'h5' && get_platform_name() !== 'n') {
			if (get_page_types(self) === 'app') {
				this.appHide(self)
			}
		}
		// #endif
		
		// #ifndef VUE3
		if (get_page_types(self) === 'app') {
			this.appHide(self)
		}
		// #endif
		
	}

	error(em) {
		// 开发工具内不上报错误
		if (this._platform === 'devtools') {
			if (process.env.NODE_ENV === 'development') {
				console.info('当前运行环境为开发者工具,不上报数据。')
				return;
			}
		}
		let emVal = ''
		if (!em.message) {
			emVal = JSON.stringify(em)
		} else {
			emVal = em.stack
		}
		let options = {
			ak: this.statData.ak,
			uuid: this.statData.uuid,
			lt: '31',
			ut: this.statData.ut,
			ch: this.statData.ch,
			mpsdk: this.statData.mpsdk,
			mpv: this.statData.mpv,
			v: this.statData.v,
			em: emVal,
			usv: this.statData.usv,
			t: parseInt(new Date().getTime() / 1000),
			p: this.statData.p,
		}
		this.request(options)
	}
}
export default Stat