config.uts 2.2 KB
Newer Older
M
mehaotian 已提交
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 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

import { UniStatOptions, UniStatCollectItemsOptions } from '../interface.uts'
// 访问开始即启动小程序,访问结束结分为:进入后台超过5min、在前台无任何操作超过30min、在新的来源打开小程序;
export const sys = uni.getSystemInfoSync()
export const sysAppBase = uni.getAppBaseInfo();
export const STAT_VERSION = sys.uniCompilerVersion

export const PAGE_PVER_TIME = 1800 // 页面在前台无操作结束访问时间 单位s
export const APP_PVER_TIME = 300 // 应用在后台结束访问时间 单位s
export const OPERATING_TIME = 10 // 数据上报时间 单位s
export const DIFF_TIME = 60 * 1000 * 60 * 24

class ConfigData {
	static __config_instance : ConfigData | null = null;

	public static getInstance() : ConfigData {
		if (ConfigData.__config_instance == null) {
			ConfigData.__config_instance = new ConfigData()
		}

		return ConfigData.__config_instance as ConfigData
	}
	private options : UniStatOptions | null = null
	private constructor() {
		// 私有构造函数,防止通过 new Singleton() 创建新实例
	}

	setOptions(options : UniStatOptions | null) {
		// 处理参数默认值
		// 判断是否存在 options 
		if (options == null) {
			options = {} as UniStatOptions
		}
		// 是否开启debug,默认false
		if (options.debug == null) {
			options.debug = false
		}
		// 上报周期,默认 10s 
		if (options.reportInterval == null) {
			options.reportInterval = 10
		}

		// 采集项配置
		if (options.collectItems == null) {
			options.collectItems = {} as UniStatCollectItemsOptions
		}
		let collectItems = options.collectItems as UniStatCollectItemsOptions
		// 是否开启推送,默认为false
		if (collectItems.uniPushClientID === null) {
			collectItems.uniPushClientID = false
		}

		// 是否开启页面采集,默认为 true
		if (collectItems.uniStatPageLog === null) {
			collectItems.uniStatPageLog = true
		}
		options.collectItems = collectItems

		this.options = options
	}
	getOptions() : UniStatOptions {
		// 如果没有参数,获取参数时设置为默认值
		if (this.options == null) {
			this.setOptions(null)
		}
		return this.options as UniStatOptions
	}
}

export const Config =  ConfigData.getInstance()