2025-04-24 17:03:28 +08:00

114 lines
3.7 KiB
TypeScript

import SKLocalUtil from "../util/SKLocalUtil";
import SKNetTask from "./SKNetTask";
export default class Report {
static shared: Report = new Report();
static platform: string;
static version: string;
static cache: string[] = [];
static isDebug: boolean = true;
private static _mac: string;
private static _roleId: number;
// 獲得Mac地址
static get mac(): string {
if (!this._mac) {
this._mac = SKLocalUtil.stringForKey("mac");
if (this._mac.length < 1) {
let aimu = ['a', 'A', 'b', 'B', 'c', 'C', 'd', 'D', 'e', 'E', 'f', 'F', 'g', 'G', 'h', 'H', 'i', 'I', 'j', 'J', 'k', 'K', 'l', 'L', 'm', 'M', 'n', 'N', 'o', 'O', 'p', 'P', 'q', 'Q', 'r', 'R', 's', 'S', 't', 'T', 'w', 'W', 'v', 'V', 'u', 'U', 'x', 'X', 'y', 'Y', 'z', 'Z', 'Z'];
let temp = '';
for (let i = 0; i < 13; i++) {
let index = Math.floor(Math.random() * aimu.length);
if (index == aimu.length) {
index = aimu.length - 1;
}
temp += aimu[index];
}
temp += Math.ceil(Math.random() * 800000) + 100000;
temp += Date.now();
this._mac = temp;
SKLocalUtil.setString(this._mac, "mac");
}
}
return this._mac;
}
// 獲得角色索引
static set roleId(value: number) {
if (this._roleId == value) {
return;
}
this._roleId = value;
if (this._roleId > 0) {
SKLocalUtil.setFloat(Report.roleId, "report_role_id");
}
}
static get roleId(): number {
if (!this._roleId) {
this._roleId = SKLocalUtil.floatForKey("report_role_id", 0);
}
return this._roleId;
}
// 提交日誌
static report(info: string) {
if(!Report.isDebug){
return;
}
// 過濾相同消息
if (Report.cache.indexOf(info) != -1) {
return;
}
Report.cache.push(info);
cc.warn(info);
this.shared.report(info);
}
private report(info: string) {
let params: any = {
roleId: Report.roleId,
msg: info,
platform: Report.platform,
version: Report.version
};
SKNetTask.postWithJson("/report", params, (code, result) => {
});
}
constructor() {
Report.platform = "H5";
if (cc.sys.os == cc.sys.OS_ANDROID) {
Report.platform = "Android";
} else if (cc.sys.os == cc.sys.OS_IOS) {
Report.platform = "iOS";
}
Report.version = "";
let self = this;
if (cc.sys.isNative) {
let win: any = window;
win.__errorHandler = function (file: any, line: any, errorMessage: any, error: any) {
let stack = "";
if (error.stack) {
stack = error.stack;
} else {
stack = error;
}
let msg = `${errorMessage}[${file}:${line}]\n${stack}`;
self.report(msg);
};
} else if (cc.sys.isBrowser) {
window.onerror = (message: string, filename: string, lineno: number, colno: number, error: Error) => {
if(!Report.isDebug) {
let stack = "";
if (error.stack) {
stack = error.stack;
}
let msg = `${message},${filename}[${lineno}:${colno}]:\n${stack}`;
self.report(msg);
}
return false;
};
}
}
}