sopApi.js 2.01 KB
import api from "../request/http"
import {
  getScene,
  sopTrackPage,
  sopTrackPageType
} from '~/utils/constantData'
/**
 * sop客户埋点上报
 */
const getDuration = page_show_time => (Date.now() - page_show_time) / 1e3
const reportClick = ({
  thisPage,
  options
}) => {
  thisPage.options = {
    ...options,
    ...thisPage.options
  }
  reportSopCustomer(thisPage, 'PAGE_CLICK')
}
const reportHide = ({
  thisPage
}) => {
  reportSopCustomer(thisPage, 'PAGE_STOP')
}
const reportSopCustomer = async (thisPage, event_type) => {
  const {
    options,
    route
  } = thisPage
  if (sopTrackPage.includes(route)) {
    if (!sopTrackPage.includes(wx.p.router.getPageInstance().route)) return
    if (options && options.scene) {
      const data = await getScene(options.scene)
      thisPage.options = data
      reportSopFun(thisPage, event_type)
    } else reportSopFun(thisPage, event_type)
  }
}
const reportSopFun = async (thisPage, event_type) => {
  const {
    options,
    route
  } = thisPage
  if (!options.sopId) return
  const userInfo = wx.p.storage.get('userInfo') || {},
    pageType = sopTrackPageType[route] || '',
    activityId = options.id || '',
    merchantId = options.merchantId || '',
    unionId = userInfo.unionId || '',
    sopId = options.sopId || '',
    extra = {
      pageType,
      activityId,
      unionId,
      merchantId,
      sopId,
    }
  if (event_type == 'PAGE_STOP') extra.time = getDuration(thisPage.page_show_time)
  const buryingPointList = [{
    customer_phone: userInfo.phone || '',
    add_way: 'SOP',
    sopId,
    // id_number: '',
    // org_user_id: '',
    // wechat_id: '',
    wechat_union_id: unionId,
    org_id: merchantId,
    system_source: 'scrm',
    platform: 'MimiProgram',
    event_type,
    event_time: new Date().format('yyyy-MM-dd hh:mm:ss'),
    extra: JSON.stringify(extra)
  }]
  api.post({
    method: 'marketing.api.sop.buryingPoint',
    projectName: 'material',
    buryingPointList
  }, true)
}
module.exports = {
  reportSopCustomer,
  reportClick,
  reportHide
}