bigdatautilApi.js 2.81 KB
import api from '~/utils/request/request'
import USERAPI from '~/api/usersApi/index'

// 埋点上报
const commonUrl = 'bigdatautil'
// 心跳检测
const heartbeatUrl = 'shopmall'
// 离开埋点url
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 = async (id, view = 1, share_channel = '', projectName = commonUrl, method = 'entmicro.api.customercollect.reportCustomerList', ) => {
  const userInfo = wx.$util.getUserInfo() || {},
    params = wx.$router.getPageInstance().options || {},
    {
      openId
    } = await USERAPI.getWXUserInfo()
  // 处理分享时间格式(1.YYYY-MM-DD HH:mm:ss,2.时间戳)
  let sharedTime
  if (params.sharedTime) {
    if (!params.sharedTime.includes('-')) {
      sharedTime = new Date(+params.sharedTime).format('yyyy-MM-dd hh:mm:ss')
    } else {
      sharedTime = decodeURIComponent(params.sharedTime)
    }
  } else {
    sharedTime = ''
  }
  let data = {
    open_id: openId || null,
    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,
    user_open_id: (params && params.openId) || null,
    share_channel: share_channel ? share_channel : null,
    sharedTime: sharedTime ? sharedTime : null,
    is_exclusive: (params && params.isExclusive) || null,
    exclusive_uuid: (params && params.exclusiveUuid) || null,
    share_staff_type: (params && params.shareStaffType) || null, //v3.1.0版本增加
    guest_no: wx.$Track.getGuestNo() //游客ID
  }
  if (view == 4) data.user_open_id = openId || ''
  // 数据上报兼容
  data = {
    method,
    jsonList: [JSON.stringify(data)],
    projectName
  }
  firstEnter = false

  // 是否离开事件
  return api.post({
    ...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)
}

export default {
  reportTrack,
  heartbeat,
  leaveReportTrack
}