reporter.js 1.34 KB
var cookies = require('../cookie');
var source = window._yohoAppName || 'unknown';

var reporter = {
    config: {
        reportUrl: '//badjs.yoho.cn/apm/yas2.gif'
    },
    stringify: function(list) {
        /*
            [{a1:1, a2:1}, {a1:2, a2:2}] 
            =>
            'a1::1$$a2::1**a1::2$$a2::2'
         */
        var data = [];

        for (var i = 0; i < list.length; i++) {
            var obj = list[i];
            var params = [];

            for (var prop in obj) {
                if (obj.hasOwnProperty(prop)) {
                    params.push(prop + '::' + obj[prop]);
                }
            }

            data.push(params.join('$$'));
        }

        return data.join('**');
    },
    report: function(str, cb) {
        if (!str) {
            return;
        }

        var imgElem = new Image();

        imgElem.src = this.config.reportUrl + '?s=' + source + '&l=' + str + '&t=' + (new Date()).getTime();

        cb && cb();
    },
    getUdid: function() {
        var udid = cookies('udid') || '';

        return udid || 0;
    },
    getUid: function() {
        var uid = (cookies('_UID') || '').split('::')[1];

        return uid || 0;
    },
    getReqId: function() {
        return cookies('docreqid') || 0;
    },
    getRoute: function() {
        return window._router || '';
    }
};

module.exports = reporter;