stat.js 4.2 KB
Newer Older
1
import Report from './report.js'
2 3 4 5 6 7 8

import { set_page_residence_time } from '../utils/pageTime.js'
import {
  get_page_types,
  get_platform_name,
  get_space,
  is_debug,
9 10
  is_push_clientid,
  is_page_report,
11
} from '../utils/pageInfo.js'
12
import { dbSet } from '../utils/db.js'
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
class Stat extends Report {
  static getInstance() {
    if (!uni.__stat_instance) {
      uni.__stat_instance = new Stat()
    }

    // 2.0 init 服务空间
    if (__STAT_VERSION__ === '2') {
      let space = get_space(uniCloud.config)
      if (!uni.__stat_uniCloud_space) {
        //   判断不为空对象
        if (space && Object.keys(space).length !== 0) {
          let spaceData = {
            provider: space.provider,
            spaceId: space.spaceId,
28
            clientSecret: space.clientSecret,
29
          }
30
          if (space.endpoint) {
31 32 33 34 35 36 37 38
            spaceData.endpoint = space.endpoint
          }
          uni.__stat_uniCloud_space = uniCloud.init(spaceData)
          // console.log(
          //   '=== 当前绑定的统计服务空间spaceId:' +
          //     uni.__stat_uniCloud_space.config.spaceId
          // )
        } else {
39
          console.error('应用未关联服务空间,请在uniCloud目录右键关联服务空间')
40 41 42 43 44 45 46 47 48 49
        }
      }
    }

    return uni.__stat_instance
  }
  constructor() {
    super()
  }

50 51 52 53
  /**
   * 获取推送id
   */
  pushEvent(options) {
54 55
    const ClientID = is_push_clientid()
    if (uni.getPushClientId && ClientID) {
56 57 58 59 60
      uni.getPushClientId({
        success: (res) => {
          const cid = res.cid || false
          //  只有获取到才会上传
          if (cid) {
61
            this.sendPushRequest(options, cid)
62 63 64 65 66 67
          }
        },
      })
    }
  }

68 69 70 71 72 73 74 75 76
  /**
   * 进入应用
   * @param {Object} options 页面参数
   * @param {Object} self	当前页面实例
   */
  launch(options, self) {
    // 初始化页面停留时间  start
    let residence_time = set_page_residence_time()
    this.__licationShow = true
77
    dbSet('__launch_options', options)
78
    // 应用初始上报参数为1
79
    options.cst = 1
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    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') {
98 99 100 101
      const isPageReport = is_page_report()
      if (isPageReport) {
        this.pageShow(self)
      }
102 103 104
    }

    // #ifdef VUE3
105
    if (get_platform_name() === 'h5' || get_platform_name() === 'n') {
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
      if (get_page_types(self) === 'app') {
        this.appShow()
      }
    }
    // #endif

    // #ifndef VUE3
    if (get_page_types(self) === 'app') {
      this.appShow()
    }
    // #endif
  }

  hide(self) {
    this.self = self
    if (get_page_types(self) === 'page') {
122 123 124 125
      const isPageReport = is_page_report()
      if (isPageReport) {
        this.pageHide(self)
      }
126
    }
127

128
    // #ifdef VUE3
129
    if (get_platform_name() === 'h5' || get_platform_name() === 'n') {
130 131 132 133 134
      if (get_page_types(self) === 'app') {
        this.appHide()
      }
    }
    // #endif
135

136 137 138 139 140 141
    // #ifndef VUE3
    if (get_page_types(self) === 'app') {
      this.appHide()
    }
    // #endif
  }
142

143 144
  error(em) {
    // 开发工具内不上报错误
145 146 147 148 149 150
    // if (this._platform === 'devtools') {
    //   if (process.env.NODE_ENV === 'development') {
    //     console.info('当前运行环境为开发者工具,不上报数据。')
    //     return
    //   }
    // }
151 152 153 154 155 156
    let emVal = ''
    if (!em.message) {
      emVal = JSON.stringify(em)
    } else {
      emVal = em.stack
    }
157 158 159

    let route = ''
    try {
160 161
      route = get_route()
    } catch (e) {
162 163 164 165
      // 未获取到页面路径
      route = ''
    }

166
    let options = {
167 168 169
      ak: this.statData.ak,
      uuid: this.statData.uuid,
      p: this.statData.p,
170
      lt: '31',
171
      url: route,
172 173 174 175 176 177 178 179 180 181 182 183
      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),
    }
    this.request(options)
  }
}
184
export default Stat