config.uts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { UniStatOptions, UniStatCollectItemsOptions } from '../interface.uts'
  2. // 访问开始即启动小程序,访问结束结分为:进入后台超过5min、在前台无任何操作超过30min、在新的来源打开小程序;
  3. export const sys = uni.getSystemInfoSync()
  4. export const device = uni.getDeviceInfo()
  5. export const sysAppBase = uni.getAppBaseInfo();
  6. export const STAT_VERSION = sys.uniCompilerVersion
  7. export const PAGE_PVER_TIME = 1800 // 页面在前台无操作结束访问时间 单位s
  8. export const APP_PVER_TIME = 300 // 应用在后台结束访问时间 单位s
  9. export const OPERATING_TIME = 10 // 数据上报时间 单位s
  10. export const DIFF_TIME = 60 * 1000 * 60 * 24
  11. class ConfigData {
  12. static __config_instance : ConfigData | null = null;
  13. public static getInstance() : ConfigData {
  14. if (ConfigData.__config_instance == null) {
  15. ConfigData.__config_instance = new ConfigData()
  16. }
  17. return ConfigData.__config_instance as ConfigData
  18. }
  19. private options : UniStatOptions | null = null
  20. private constructor() {
  21. // 私有构造函数,防止通过 new Singleton() 创建新实例
  22. }
  23. setOptions(options : UniStatOptions | null) {
  24. // 处理参数默认值
  25. // 判断是否存在 options
  26. if (options == null) {
  27. options = {} as UniStatOptions
  28. }
  29. // 是否开启debug,默认false
  30. if (options.debug == null) {
  31. options.debug = false
  32. }
  33. // 上报周期,默认 10s
  34. if (options.reportInterval == null) {
  35. options.reportInterval = 10
  36. }
  37. // 采集项配置
  38. if (options.collectItems == null) {
  39. options.collectItems = {} as UniStatCollectItemsOptions
  40. }
  41. let collectItems = options.collectItems as UniStatCollectItemsOptions
  42. // 是否开启推送,默认为false
  43. if (collectItems.uniPushClientID === null) {
  44. collectItems.uniPushClientID = false
  45. }
  46. // 是否开启页面采集,默认为 true
  47. if (collectItems.uniStatPageLog === null) {
  48. collectItems.uniStatPageLog = true
  49. }
  50. options.collectItems = collectItems
  51. this.options = options
  52. }
  53. getOptions() : UniStatOptions {
  54. // 如果没有参数,获取参数时设置为默认值
  55. if (this.options == null) {
  56. this.setOptions(null)
  57. }
  58. return this.options as UniStatOptions
  59. }
  60. }
  61. export const Config = ConfigData.getInstance()