index.js 3.39 KB
import USERAPI from "~/api/usersApi/index";
import Util from "~/utils/util";
Page({
  data: {
    strategyDetailObj: {},
  },

  async onLoad(options) {
    console.log(options);
    if (options && options.openId) {
      this.shareOpenId = options.openId
    }
    if (options && options.share_guest_no) {
      this.share_guest_no = options.share_guest_no
    }
    this.data.enter_time = new Date().format('yyyy-MM-dd hh:mm:ss')
    this.firstEnter = true
    await this.getStrategyDetail(options.id);
  },


  async getStrategyDetail(id) {
    wx.p.api.strategyApi
      .getStrategyDetail({
        id,
      })
      .then((res) => {
        Util.setNavigationBarTitle(res.strategyName);
        this.categoryName = res.categoryName
        this.setData({
          strategyDetailObj: res,
        }, () => {
          // 触发埋点
          this.heartbeatFn({
            id,
            item_type: 6,
            view: 1
          })
          this.handlerShareQuery()
        });
      });
  },

  /**添加埋点开始 */
  /**
   * 商品-活动id,
   * 物品类型item_type  1 商品  3活动,
   * view 动作类型  1浏览 4分享 5离开页面 6心跳
   * */
  async addTrack(query) {
    const { id, item_type, view } = query;
    const userInfo = wx.p.cache.userInfo.getUserInfo() || {},
      { openId } = await USERAPI.getWXUserInfo();
    const {
      merchantId
    } = wx.p.storage.get('merchant_info')

    let data = {
      open_id: openId || null,
      create_time: this.firstEnter
        ? this.data.enter_time
        : new Date().format("yyyy-MM-dd hh:mm:ss"), // 离开时间
      enter_time: this.data.enter_time, // 进入时间
      item_type,
      customer_phone: userInfo.phone || null,
      action_type: view,
      item_id: id,
      business_type: 1,
      add_way: 104,
      merchant_id: merchantId,
      guest_no: wx.p.$Track.getGuestNo(), //游客ID,
      user_open_id: this.shareOpenId || "",
      share_guest_no: this.share_guest_no || '',
      trade_classification: this.categoryName || '',
      association_classify_first_name:this.categoryName || '',
    };
    this.firstEnter = false;
    const jsonList = [JSON.stringify(data)];
    const res = await wx.p.api.shopmallApi.trackHttp({
      jsonList,
    });
  },
  heartbeatFn(query) {
    this.trackQuery = query;
    this.addTrack(query);
    this.timer = setInterval(() => {
      this.trackQuery.view = 6;
      this.addTrack(this.trackQuery);
    }, 60 * 1e3);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    if (this.timer) clearInterval(this.timer);
    this.trackQuery.view = 5;
    this.addTrack(this.trackQuery);
    // wx.$util.customerbehavior(2);
  },

  handlerShareQuery() {
    const {
      merchantId
    } = wx.p.storage.get('merchant_info')
    const userInfo = wx.p.cache.userInfo.getUserInfo();
    let { strategyDetailObj } = this.data,
      open_id = "", share_guest_no = wx.p.$Track.getGuestNo();

    if (userInfo) open_id = userInfo.openId;

    let path = `id=${strategyDetailObj.id}&openId=${open_id}&share_guest_no=${share_guest_no}`;
    if (merchantId) path = `${path}&merchantId=${merchantId}`;
    this.shareTitle = strategyDetailObj.strategyName;
    this.shareImage = strategyDetailObj.strategyImg
    this.shareQuery = path
    this.trackQuery.view = 4;
    this.shareFun = () => {
      this.addTrack(this.trackQuery);
    }

  },
  onShareAppMessage: function () {

  }
});