reportTrackFunMixin.js 3.62 KB
import { Util } from "@/common/util"
import { reportTrack } from "@api/resident"
export default {
  data() {
    return {
      prePagePath: "",
      track_timerFlag: false,
      track_batchId: null,
      track_page_view_flag: false,
      track_timer_out: null
    }
  },
  beforeRouteEnter(to, from, next) {
    next((vm) => {
      vm.prePagePath = from.path
    })
  },
  computed: {
    loginAccount() {
      return this.$store.state.d2admin.user.info.loginAccount
    }
  },

  methods: {
    setReportTrackData(
      eventType,
      eventCode = "",
      businessParam = "",
      batchId = ""
    ) {
      let recordList = localStorage.getItem("recordList"),
        route = this.$route,
        { loginAccout, uuid, userType } = this.loginAccount,
        { prePagePath } = this,
        item = {
          loginAccount: loginAccout,
          uuid,
          userType,
          systemSource: 1,
          eventTime: Util.dateFormate(Date.now(), "YYYY-MM-DD HH:mm:ss"),
          eventType,
          pagePath: route.path
        }
      if (!prePagePath) prePagePath = this.getPrePagePath(this)
      item.prePagePath = prePagePath || "/index"
      if (batchId) item.batchId = batchId
      if (eventCode) item.eventCode = eventCode
      if (businessParam) item.businessParam = JSON.stringify(businessParam)
      if (!recordList) recordList = [item]
      else {
        recordList = JSON.parse(recordList)
        recordList.push(item)
      }
      return
      localStorage.setItem("recordList", JSON.stringify(recordList))
      if (recordList.length > 9) {
        reportTrack({
          recordList
        }).then((res) => {
          if (res) localStorage.removeItem("recordList")
        })
      }
    },
    getRandomString(length = 30) {
      const characters =
        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
      let result = ""
      for (let i = 0; i < length; i++) {
        result += characters.charAt(
          Math.floor(Math.random() * characters.length)
        )
      }
      return result
    },
    onPageShow() {
      if (this.track_page_view_flag) return
      if (!this.track_batchId)
        this.track_batchId = `DJ_${this.getRandomString()}_${Date.now()}`
      this.track_timerFlag = true
      this.track_page_view_flag = true
      if (this.track_timer_out) {
        clearTimeout(this.track_timer_out)
        this.track_timer_out = null
      }
      setTimeout(() => {
        this.setBrowseTrackData()
      }, 300)
    },
    onPageHide() {
      this.track_timerFlag = false
      this.track_batchId = null
      this.track_page_view_flag = false
      clearTimeout(this.track_timer_out)
      this.track_timer_out = null
    },
    setBrowseTrackData() {
      try {
        if (!this.track_timerFlag || this.$route.path != "/qualityControlList")
          return
        else
          this.track_timer_out = setTimeout(() => {
            this.setBrowseTrackData()
          }, 10e3)
        this.setReportTrackData(
          2,
          this.track_event_code,
          this[this.track_businessParam_key],
          this.track_batchId
        )
      } catch (error) {
        console.log(error)
      }
    },
    getPrePagePath(_this) {
      let current = _this
      const visited = new Set()

      while (current) {
        if (visited.has(current)) {
          // 防止无限循环
          console.error("Detected circular reference in parent chain")
          return ""
        }
        visited.add(current)

        if (current.$parent && current.$parent.prePagePath) {
          return current.$parent.prePagePath
        }

        current = current.$parent
      }

      return ""
    }
  }
}