import { UAParser } from "ua-parser-js"; import { IDeviceInfo } from "./types"; const myUa = new UAParser(); const UNKNOWN = "UnKnown"; export function getTrackUaInfo(info?: Partial): IDeviceInfo { const ua = myUa.getResult(); return { os: ua.os.name ? `${ua.os.name || ""} ${ua.os.version || ""}` : UNKNOWN, device: ua.device.vendor || ua.device.model ? `${ua.device.vendor || ""} ${ua.device.model || ""}` : UNKNOWN, browser: ua.browser.name || ua.browser.version ? `${ua.browser.name || ""} ${ua.browser.version || ""} ${ ua.browser.major || "" }` : UNKNOWN, engine: ua.engine.name || ua.engine.version ? `${ua.engine.name || ""} ${ua.engine.version || ""}` : UNKNOWN, uuid: UNKNOWN, // 移动端专属,需要移动端初始化获取 buildVersion: UNKNOWN, // 移动端专属,需要移动端初始化获取 ...info, }; } export function getUA() { return myUa.getUA(); } export function isMobile() { return myUa.getDevice().type === "mobile"; } export function getMemory() { try { const memory = (performance as any).memory; /** * usedJSHeapSize: 当前已使用的JavaScript堆大小。 totalJSHeapSize: 当前JavaScript堆的总大小。 jsHeapSizeLimit: JavaScript堆的最大限制。 */ const obj = { jsHeapSizeLimit: memory.jsHeapSizeLimit, totalJSHeapSize: memory.totalJSHeapSize, usedJSHeapSize: memory.usedJSHeapSize, }; return JSON.stringify(obj); } catch (error) { return ""; } }