jollychic-h5-logs
Version:
jollychic h5 logs
1 lines • 1.66 kB
JavaScript
/**
* Created by Barry on 2019/1/7
* h5-logs函数
*/
var Utils = {};
Utils.dataCollect = [];
Utils.event = 'reportEvent';
Utils.baseUrl = '';
Utils.baseApiUrl = '/api/chatCollector';
Utils.sendCount = 16; //多少条开始收集
Utils.reportEvent = function () {
var myDate = new Date();
var that = this;
var data = JSON.stringify({
event: that.event,
data: that.dataCollect,
time: myDate.getTime()
});
if (!navigator.sendBeacon || that.dataCollect.length === 0) {
return;
}
var url = that.baseUrl + that.baseApiUrl;
navigator.sendBeacon(url, data);
if (that.dataCollect && that.dataCollect.length > 0) {
that.dataCollect = [];
}
};
//错误收集
Utils.errorHandlerCollectFunction = function (info, event) {
var myDate = new Date();
var that = this;
if (!info) {
info = 'systemInfo';
}
if (!event) {
event = 'systemEvent'
}
var obj = {
event: event,
info: info,
time: myDate.getTime()
};
that.dataCollect.push(JSON.stringify(obj));
//大于这个阀值也开始发送数据
if (that.dataCollect.length >= that.sendCount) {
that.reportEvent();
}
};
//初始化
Utils.init = function (obj) {
var res = obj || {};
var event = res.event || null;
var baseUrl = res.baseUrl || null;
var baseApiUrl = res.baseApiUrl || null;
var sendCount = res.sendCount || null;
var that = this;
if (baseUrl) {
that.baseUrl = baseUrl;
}
if (baseApiUrl) {
that.baseApiUrl = baseApiUrl;
}
if (event) {
that.event = event;
}
if (sendCount) {
that.sendCount = sendCount;
}
//默认浏览器关闭开始收集
window.addEventListener("unload", function () {
that.reportEvent();
}, false);
};
export default Utils;