bigdatautilApi.js 3.19 KB
import { util } from 'utils'
import dayjs from 'dayjs'

// 埋点上报
const commonUrl = '/rop-scrm-bigdatautil/router'
// 心跳检测
const heartbeatUrl = '/rop-scrm-shopmall/router'
// 离开埋点url
const leaveUrl = '/rop-scrm-bigdatautil/data/report'

// 第一次进入时间
let firstEnter = true
const enter_time = new Date().format('yyyy-MM-dd hh:mm:ss')

/** 埋点 */
/**
 * @param {*} id 活动id
 * @param {*} view 动作类型 // 1浏览 4分享 5离开页面
 * @param {*} share_channel // 分享类型(1:转发,2:朋友圈)
 * @param {*} url // 基础路径
 * @param {*} method 方法地址
 * @param {*} leave 是否离开事件 默认是false
 * @returns
 */
const reportTrack = (id, view = 1, share_channel = '', url = commonUrl, method = 'entmicro.api.customercollect.reportCustomerList', leave = false) => {
  const userInfo = util.getUserInfo()
  const params = util.getLocalStorage('wx_scrm_link_params') || {}
  const ISWXWORK = util.getLocalStorage('is_wxwork')
  if (!userInfo) return
  if (ISWXWORK) {
    if (userInfo.loginList.length && userInfo.loginList[0].phone) userInfo.phone = userInfo.loginList[0].phone
  }
  // 处理分享时间格式(1.YYYY-MM-DD HH:mm:ss,2.时间戳)
  let sharedTime
  if (params.sharedTime) {
    if (!params.sharedTime.includes('-')) {
      sharedTime = dayjs(+params.sharedTime).format('YYYY-MM-DD HH:mm:ss')
    } else {
      sharedTime = decodeURIComponent(params.sharedTime)
    }
  } else {
    sharedTime = ''
  }
  let data = {
    staff_id: (params && params.staffId) || null,
    staff_uuid: (params && params.shareStaffUuid) || null,
    user_phone: (params && params.userPhone) || null,
    create_time: firstEnter ? enter_time : new Date().format('yyyy-MM-dd hh:mm:ss'), // 离开时间
    enter_time, // 进入时间
    item_type: 3,
    customer_phone: userInfo.phone || null,
    action_type: view,
    item_id: id,
    business_type: 1,
    add_way: 104,
    merchant_id: (params && params.merchantId) || null,
    share_channel: share_channel ? share_channel : null,
    sharedTime: sharedTime ? sharedTime : null
  }
  if (view == 1) {
    data.open_id = userInfo.openid || userInfo.uuid
    data.user_open_id = (params && params.openId) || null
  }
  if (view == 4) data.user_open_id = userInfo.openid || userInfo.uuid
  // 数据上报兼容
  data = {
    // method: 'entmicro.api.customercollect.reportCustomerList',
    method: method ? method : null,
    jsonList: [JSON.stringify(data)]
  }
  firstEnter = false

  // 是否离开事件
  if (leave) {
    if (navigator.sendBeacon) {
      const params = JSON.stringify(data)
      return navigator.sendBeacon(url, params)
    } else {
      return window.$HTTP({
        url,
        data
      })
    }
  } else {
    return window.$HTTP({
      url,
      data
    })
  }
}

// 心跳检测
const heartbeat = (id, view = 5, share_channel = '', url = heartbeatUrl, method = 'scrm.api.shopmall.customer.browse.records.heart') => {
  reportTrack(id, view, share_channel, url, method)
}

//离开事件数据上报
const leaveReportTrack = (id, view = 5) => {
  // console.log('离开事件数据上报')
  reportTrack(id, view, '', leaveUrl, '', true)
}

export default {
  reportTrack,
  heartbeat,
  leaveReportTrack
}