report.ts 4.34 KB
import storage from './loggerAndStorage.js'
import http from './request.js'
import Util from '../utils/helper.js'
import type { ReportOption } from '../types/report.js'
// 异步发送策略
class sendStrategy {
  dataHasSend = true
  dataHasChange = false
  syncStorage = false
  failTime = 0
  is_first_batch_write = false
  DEFAULT_SETTING!: ReportOption
  init(options: ReportOption) {
    this.DEFAULT_SETTING = options
    if (this.DEFAULT_SETTING.switch_start) {
      this.dataHasChange = true
      this.is_first_batch_write = true
      this.batchInterval()
    }
  }
  batchInterval() {
    const _this = this
    function loopWrite() {
      setTimeout(() => {
        _this.batchWrite()
        loopWrite()
      }, 500)
    }
    function loopSend() {
      setTimeout(() => {
        _this.batchSend()
        loopSend()
      }, 1000 * _this.DEFAULT_SETTING.send_time * Math.pow(2, _this.failTime))
    }
    loopWrite()
    loopSend()
  }
  batchWrite() {
    const _this = this
    if (this.dataHasChange) {
      if (this.is_first_batch_write) {
        this.is_first_batch_write = false
        setTimeout(() => {
          _this.batchSend()
        }, 1000)
      }
      this.dataHasChange = false
    }
  }
  batchSend(singeleID?: number) {
    if (this.dataHasSend) {
      let data = []
      if (singeleID) {
        data = storage.getTrack(0, true)
        data = data.filter(item => item.id == singeleID)
      } else {
        data = storage.getTrack(this.DEFAULT_SETTING.send_count, true)
        console.log(data, 'data')
      }
      if (data.length > 0) {
        this.dataHasSend = false
        this.webrequest({
          data,
          success: this.batchRemove.bind(this),
          fail: this.sendFail.bind(this)
        })
      }
    }
  }
  batchRemove(data: any) {
    // 删除数据
    storage.removeTrack(data)
    this.dataHasSend = true
    this.dataHasChange = true
    this.batchWrite()
    this.failTime = 0
  }
  sendFail(data: any) {
    this.dataHasSend = true
    this.failTime++
    storage.removeUploadData(data)
  }
  webrequest(option: { data: any; success: any; fail: any }) {
    if (option.data.length > 0) {
      console.log(option.data, new Date())
      if (this.DEFAULT_SETTING.post_way == 'image') {
        const upload_data = Util.handlerTrackData(option.data)
        storage.uploadTrack(option.data)
        const base_url = this.DEFAULT_SETTING.server_img_url
        const param = JSON.stringify(upload_data)
        const date = new Date().getTime()
        try {
          http.imagehttp.server_url = `${base_url}?d=${date}&data=${param}`
          http.imagehttp.successCallBack = function () {
            option.success(option.data)
            console.log('发送成功', new Date())
          }
          let _this = this
          http.imagehttp.errorCallback = function () {
            console.log('发送失败', new Date())
            option.fail(option.data)
            _this.useAjax(option)
          }
          http.imagehttp.start()
        } catch (error) {
          console.log(error)
        }
      } else if (this.DEFAULT_SETTING.post_way == 'ajax') {
        this.useAjax(option)
      }
    }
  }
  send(data: any) {
    if (data && this.DEFAULT_SETTING.switch_start) {
      this.dataHasChange = true
      const id = storage.setTrack(data)
      this.batchSend(id as unknown as number)
    }
  }
  useAjax(option: { data: any; success: any; fail: any }) {
    const url = this.DEFAULT_SETTING.server_url
    const upload_data = Util.handlerTrackData(option.data)
    storage.uploadTrack(option.data)
    const param = JSON.stringify(upload_data)
    let _this = this
    http.ajax({
      url,
      type: 'POST',
      data: { data: param },
      success: function (res) {
        option.success(option.data)
      },
      error: function (err) {
        option.fail(option.data)
        _this.useBeacon(option)
      }
    })
  }
  useBeacon(option: { data: any; success: any; fail?: any }) {
    const url = this.DEFAULT_SETTING.server_url
    const upload_data = Util.handlerTrackData(option.data)
    storage.uploadTrack(option.data)
    const param = JSON.stringify(upload_data)
    const blob = new Blob([param], { type: 'application/json' })
    http.beason.server_url = url
    http.beason.start(blob)
    http.beason.callback = function () {
      option.success(option.data)
    }
  }
}
export default new sendStrategy()