Commit 77a09347 by Sixong.Zhu

update monitor

parent 0e5692b2
Showing with 115 additions and 0 deletions
import Axios from "axios";
import { UniplatSdk } from "uniplat-sdk";
import { ImEnvironment } from "../model";
export const enum Product {
Default = 'default',
QqxbWeixin = 'qqxb-weixin',
QqxbApp = 'qqxb-app',
Fulibao = 'fulibao',
HrManager = 'hr-manager',
Hrs100 = 'hrs100',
HrsApp = 'hrs-app',
BiJie = 'bi-jie',
Cashier = 'cashier',
Uniplat = "uniplat",
}
const enum ProductTable {
Default = "",
Login = "login",
Error = "error",
}
export interface SdkMonitorOption {
userAgent?: boolean;
envir: ImEnvironment;
product: Product;
}
class WebMonitor {
private key = "";
private envir = ImEnvironment.Dev;
private product = Product.Default;
private readonly url = "https://pre-hrs-monitor.hrs100.com";
public updateKey(key: string) {
this.key = key;
return this;
}
private buildHeaders() {
return {
headers: { authorization: "cdd0a34e-f537-4e5b-808e-2ba06af21845" },
};
}
private enable() {
return this.envir === ImEnvironment.Dev;
}
private envirString() {
if (this.envir === ImEnvironment.Dev) {
return "[Dev]";
}
if (this.envir === ImEnvironment.Stage) {
return "[Stage]";
}
return "";
}
public log(msg: any) {
return Axios.post(
this.url,
{
type: ProductTable.Login,
product: this.product,
msg: `${this.envirString()} ${msg} `,
key: this.key,
},
this.buildHeaders()
);
}
public error(msg: any) {
return Axios.post(
this.url,
{
type: ProductTable.Error,
product: this.product,
msg: `${this.envirString()} ${msg} `,
key: this.key,
},
this.buildHeaders()
);
}
public useSdk(sdk: UniplatSdk, options: SdkMonitorOption) {
this.envir = options.envir;
this.product = options.product;
sdk.events.addUniversalErrorResponseCallback((r) => {
if (this.enable()) {
const msg: string[] = [];
msg.push(`URL: ${decodeURIComponent(r.config.url)}`);
msg.push(`Token: ${sdk.global.jwtToken}`);
const header = r.config.headers;
if (header) {
msg.push(`CurrentOrg: ${header.CurrentOrg}`);
msg.push(`Scenes: ${header.Scenes}`);
}
msg.push(
`Exception: ${((r.data.error as string) || "").substring(
0,
500
)}`
);
options &&
options.userAgent &&
msg.push(`UserAgent: ${window.navigator.userAgent}`);
r && r.config && this.error(msg.join("\n"));
}
});
}
}
export const monitor = new WebMonitor();
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment