yas.js 5.84 KB
import {LOG_EVENT_HOST} from '../libs/config';
import {UPLOAD_LOG} from '../libs/request';
import {getYHStorageSync} from '../utils/util';
import path2Name from '../router/path-to-name';
import {stringify} from '../vendors/query-string'

const YB_ENTER_FOREGROUND = 'YB_ENTER_FOREGROUND';  // 系统-程序切换置前台
const YB_ENTER_BACKGROUND = 'YB_ENTER_BACKGROUND';  // 系统-程序切换置后台
const YB_LAUNCH_APP = 'YB_LAUNCH_APP';              // 系统-启动小程序
const YB_AWAKE_MP = 'YB_AWAKE_MP';                  // 唤起事件,每次启动时,上报原始的页面路径,以对推广渠道数据进行监测
const YB_MAIN_TAB_C = 'YB_MAIN_TAB_C';              // 点击页面的tab时,点击时才上报,默认载入时不上报
const YB_PAGE_OPEN_L = 'YB_PAGE_OPEN_L';            // 小程序(所有)页面加载时
const YB_MY_LOGIN = 'YB_MY_LOGIN';                  // 当用户使用该登录方式登录成功时
const YB_REGISTER_SUCCESS = 'YB_REGISTER_SUCCESS';  // 用户注册成功
const YB_LUCK_DT_GDS_C = 'YB_LUCK_DT_GDS_C';        // 点击抽奖详情页底部的商品时
const YB_LUCK_DT_JOIN_C = 'YB_LUCK_DT_JOIN_C';      // 点击抽奖详情页底部的"0元参加抽奖"成功时

const pageOpen = data => {
  const _pageName = function(options, path) {
    let hasConf = path2Name[path];

    // 多入口页面依据参数设定上报页面名称
    if (hasConf && hasConf.multiEntry) {
      let pathName = '';

      Object.keys((hasConf.multiName || {})).forEach(key => {
        if (options[key]) {
          pathName = hasConf.multiName[key];
          return false;
        }
      });
      return pathName;
    }
    return hasConf ? hasConf.pathName : '';
  };

  const _pageParam = function(options = {}, fromPath) {
    if (!options) return '';

    let hasConf = path2Name[fromPath];
    if (hasConf && hasConf.multiEntry) {
      let param = '';

      Object.keys((hasConf.multiName || {})).forEach(key => {
        if (options[key]) {
          param = options[key] || '';
          return false;
        }
      });
      return param;
    }

    if (path2Name[fromPath] && path2Name[fromPath].paramKey) {
      return `${decodeURIComponent(options[path2Name[fromPath].paramKey])}` || '';
    }
    return stringify(options, '');
  };
  
  let pages = getCurrentPages();
  let currentPage = pages[pages.length - 1];
  let path = `${currentPage.route}`,
    options = currentPage.options || {}; // 拷贝options对象,用于获取当前页面参数

  // from page relevant
  let fromPage, fromPath, fromOptions;
  if (pages.length > 1) {
    fromPage = pages[pages.length - 2];
    fromPath = `${fromPage.route}`;
    fromOptions = fromPage.options || {};
  }

  return Object.assign(data, {
    PAGE_PATH: path,
    PAGE_PARAM: _pageParam(options, path),
    PAGE_NAME: _pageName(options, path),
    FROM_PAGE_NAME: _pageName(options, fromPath),
    FROM_PAGE_PARAM: options.fromPageParam || _pageParam(fromOptions, fromPath)
  });
};

const yasReport = function (eventId, data = {}, app) {
  if (eventId === YB_PAGE_OPEN_L) {
    data = pageOpen(data);
  }
  
  app = app || getApp();
  
  let ln; // 语言
  let os; // 系统类型
  let dm; // 设备型号
  let re; // 屏幕大小
  let osv;//系统版本
  let ak = 'yoholuck_mp';//
  let net = '0';
  let ts = new Date().getTime() + '';
  let uid = app && app.globalData && app.globalData.userInfo && app.globalData.userInfo.uid ? app.globalData.userInfo.uid : (getYHStorageSync('openID', 'analytics') || '');
  let udid = app && app.globalData && app.globalData.udid ? app.globalData.udid : '';
  let sid = app && app.globalData && app.globalData.sid ? app.globalData.sid : '';
  let cid = app && app.globalData && app.globalData.cid ? app.globalData.cid : '';

  let open_id = app && app.globalData && app.globalData.openID ? app.globalData.openID : getYHStorageSync('openID', 'analytics');
  let union_id = app && app.globalData && app.globalData.WXUnion_ID ? app.globalData.WXUnion_ID : getYHStorageSync('union_id', 'analytics');
  let ch = app && app.globalData && app.globalData.ch ? app.globalData.ch : '';

  wx.getSystemInfo({
    success: function (res) {
      //返回设备信息
      dm = res.model;
      ln = res.language;
      os = res.platform;
      osv = res.system;
      re = res.screenWidth + '*' + res.screenHeight;
    },
    complete: function () {
      wx.getNetworkType({
        success: function (res) {
          var networkType = res.networkType
          if (networkType === 'wifi') {
            net = '1';
          } else if (networkType === '2g') {
            net = '2';
          } else if (networkType === '3g') {
            net = '3';
          } else if (networkType === '4g') {
            net = '4';
          } else {
            net = '0';
          }
        },
        complete: function () {
          let union_type = app.getUnion_type();

          let statusParam = {'net': net, 'ln': ln};
          let deviceParam = {
            'res': re,
            'osv': osv,
            'os': os,
            'ak': ak,
            'dm': dm,
            'udid': udid,
            'ch': union_type !== '' ? union_type + "" : ch + ""
          };
          let userParam = data;
          userParam.C_ID = cid;
          userParam.UNION_ID = union_id;
          userParam.PV_ID = app.getPvid();
          let eventParam = [{'param': userParam, 'ts': ts, 'op': eventId, 'uid': uid, 'sid': sid}]
          let parameters = {'status': statusParam, 'device': deviceParam, 'events': eventParam}

          UPLOAD_LOG(LOG_EVENT_HOST, parameters)
            .then(function (data) {
            })
            .catch(function (error) {

            });
        }
      })
    }
  })
};

export {
  yasReport,
  YB_ENTER_FOREGROUND,
  YB_ENTER_BACKGROUND,
  YB_LAUNCH_APP,
  YB_PAGE_OPEN_L,
  YB_MAIN_TAB_C,
  YB_MY_LOGIN,
  YB_REGISTER_SUCCESS,
  YB_AWAKE_MP,
  YB_LUCK_DT_GDS_C,
  YB_LUCK_DT_JOIN_C
};