app-report.js 2.1 KB
import dataUtils from './data-utils.js';
import { MD5 } from '../vendors/crypto';
import api from './api.js';

const getDeviceInfo = function (app) {

  let systemInfo = dataUtils.getGlobalDataByKey('systemInfo')
  let device = {}

  let union_type = app.getUnion_type();
  console.log('union_type', union_type);
  device.ak = dataUtils.getConfig().reportParams.ak;
  device.udid = dataUtils.getGlobalDataByKey('udid');
  device.ch = union_type;
  device.os = "weixinapp";
  device.osv = systemInfo && systemInfo.version ? systemInfo.version : ''
  device.dm = systemInfo && systemInfo.model ? systemInfo.model.replace(' ', '_') : ''

  if (systemInfo.platform == 'ios') {
    //针对ios设备,特殊处理dm,转为iphone_7这种格式
    let start_num = device.dm.indexOf('<')
    let end_num = device.dm.indexOf('>')
    let deviceModel
    if (start_num != 0 && end_num != 0) {
      deviceModel = device.dm.substring(start_num + 1, end_num)
      if (deviceModel.length > 0) {
        deviceModel = deviceModel.replace(',', '_')
        device.dm = deviceModel
      }
    }
  }

  return device;
}


export const appReport = async function ({ reportType = 'hb', pt ='LIFECYCLE', pn='start', param={}}, app) {
  let deviceInfo = getDeviceInfo(app)
  if (!pt || !pn || !deviceInfo || !deviceInfo.dm) return;

  let ts = new Date().getTime() + '000000';

  let uid = dataUtils.getUid()
  uid = MD5(`${uid}`);

  let sid = dataUtils.getGlobalDataByKey('sid') || '';

  let networkType = await dataUtils.getNetworkType() || '0';

  let event = {}
  event.ps = '0',
  event.av = dataUtils.getConfig().apiParams.app_version,
  event.ab = dataUtils.getConfig().apiParams.app_build,
  event.ca = '0',
  event.net = networkType,
  event.sid = sid,
  event.uid = uid,
  event.ts = ts,
  event.pt = pt,
  event.pn = pn,
  event.param = param

  let events = [];
  events.push(event)

  //组装最终要上报的数据
  let parameters = {};
  parameters.type = reportType,
  parameters.device = deviceInfo,
  parameters.events = events,

    api.appReportData(parameters)

}