report-yas.js 2 KB
const axios = require('axios');
const _ = require('lodash');
const logger = global.yoho.logger;

module.exports = (req, res, next) => {
  if (req.path === '/xianyu' || req.path === '/xianyu/index' || req.path === '/xianyu/index/channel') {
    try {
      const json = { appop: 'XY_UFO_MAIN_START_SERVICE', param: {} };
      const xianyu = {
        isiOS: /\(i[^;]+;( U;)? CPU.+Mac OS X/i.test(req.get('User-Agent') || ''),
        isAndroid: /Android/i.test(req.get('User-Agent') || ''),
      };

      const appBaseLogs = {
        status: [
          {
            ca: '',
            lo: '',
            cy: 'CN',
            av: '',
            udid: req.cookies.udid,
            ln: '',
            ab: '',
            sid: '',
            net: '',
            la: ''
          }
        ],
        device: {
          res: '',
          ifa: '',
          mac: '',
          osv: '',
          ifv: '',
          ps: '',
          os: xianyu.isiOS ? 'ios' : 'android',
          ak: 'yoho_xianyu_' + (xianyu.isiOS ? 'ios' : 'android'),
          sv: '',
          ch: '',
          dm: '',
          tdid: '',
          afp: '',
          udid: req.cookies.udid
        },
        events: [{
          sid: '',
          uid: req.user.uid
        }]
      };

      let param = json.param;
      const srcId = req.cookies.SRC_ID || '';

      param.SRC_ID = srcId;

      let ev = appBaseLogs.events[0];

      let event = _.merge(ev, {
        ts: (new Date()).getTime().toString(),
        indx: 1,
        op: json.appop,
        param: param
      });

      let logs = _.merge(appBaseLogs, {
        events: [event]
      });

      const data = '_mlogs=' + encodeURIComponent(JSON.stringify(logs));

      axios.post('https://analytics.m.yohobuy.com/yas_mobile', data, {
        headers: {
          origin: 'https://xianyu.yohobuy.com/',
          referer: 'https://xianyu.yohobuy.com/',
          'content-type': 'application/x-www-form-urlencoded'
        }
      });
    } catch (e) {
      logger.error(e);
    }
  }

  next();
};