import { v4 as uuidv4 } from 'uuid' // import axios from 'axios' import { getDetector } from './ua' export const innertEvent = { /**订阅 */ on(key:string, fn: Function) { if (!this._list) { this._list = {}; } if (!this._list[key]) { this._list[key] = []; } this._list[key].push(fn); }, /**只订阅一次 */ once(key:string, fn: Function) { if (!this._list) { this._list = {}; } if (!this._list[key]) { this._list[key] = []; } if (this._list[key].indexOf(fn) === -1) { this._list[key].push(fn); } }, /**取消订阅 */ off(key: string) { if (!this._list) { this._list = {}; } if (!this._list[key]) { return; } delete this._list[key]; }, /**发布 */ trigger(key: string, data?: any) { const args = Array.prototype.slice.call(arguments); const arrFn:Function[] = this._list && this._list[key]; if (!arrFn || !arrFn.length) { return; } arrFn.forEach(fn=> { if (typeof fn === 'function') { fn.apply(this, args); } }) } } /**自定义window 事件 */ export const windowCustomEventTrigger = function(eventName: string) { const event = new CustomEvent(eventName, {detail: this}); window.dispatchEvent(event); } // 下载文件 export const downloadFile = function (filename: string, content: string) { const a = document.createElement('a') const blob = new Blob([content]) const url = window.URL.createObjectURL(blob) a.href = url a.download = filename a.click() window.URL.revokeObjectURL(url); } // 监听事件 export const on = function(obj: any, event: string, callFn: Function) { if(obj[event]) { const fn = obj[event]; obj[event] = function() { const args = Array.prototype.slice.call(arguments); callFn.apply(this, args); fn.apply(this, args); }; } else { obj[event] = function() { const args = Array.prototype.slice.call(arguments); callFn.apply(this, args); }; } } export enum keysEnum { webMonitorId = 'a', customerKey = 'b', userId = 'c', firstUserParam = 'd', secondUserParam = 'e', completeUrl = 'f', simpleUrl = 'g', happenTime = 'h', uploadType = 'i', projectVersion = 'j', // 项目版本号 pageKey = 'k', deviceName = 'l', os = 'm', browserName = 'n', browserVersion = 'o', monitorIp = 'p', country = 'q', province = 'r', city = 's', loadType = 't', loadTime = 'u', loadPage = 'v', domReady = 'w', redirect = 'x', lookupDomain = 'y', ttfb = 'z', request = 'A', loadEvent = 'B', appcache = 'C', unloadEvent = 'D', connect = 'E', behaviorType = 'da', className = 'G', placeholder = 'H', inputValue = 'I', tagName = 'L', innerText = 'M', infoType = 'O', errorMessage = 'P', errorStack = 'Q', browserInfo = 'R', httpUrl = 'S', status = 'T', statusText = 'U', statusResult = 'V', requestText = 'W', responseText = 'X', description = 'Y', screenInfo = 'Z', imgType = 'aa', elementType = 'ba', sourceUrl = 'ca', behaviorResult = 'ea', event = 'fa', referrer = 'referrer', newStatus = 'newStatus' } export enum UploadTypeEnum { CUSTOMER_PV="CUSTOMER_PV", STAY_TIME="STAY_TIME", CUS_LEAVE="CUS_LEAVE", LOAD_PAGE="LOAD_PAGE", HTTP_LOG="HTTP_LOG", JS_ERROR="JS_ERROR", // js异常 SCREEN_SHOT="SCREEN_SHOT", ELE_BEHAVIOR="ELE_BEHAVIOR", RESOURCE_LOAD="RESOURCE_LOAD", CUSTOMIZE_BEHAVIOR="CUSTOMIZE_BEHAVIOR", VIDEOS_EVENT="VIDEOS_EVENT", LAST_BROWSE_DATE="LAST_BROWSE_DATE", WM_PAGE_ENTRY_TIME="WM_PAGE_ENTRY_TIME", WM_VISIT_PAGE_COUNT="WM_VISIT_PAGE_COUNT", ON_ERROR= "on_error", CONSOLE_ERROR= "console_error", RESOURCE_ERROR= "resource_error", HTTP_ERROR= "http_error", NEW_CUSTOMER= "new_customer", INSTALL_COUNT= "install_count", } // webMonitorId: webfunny768 export const getCommon = function(webMonitorId: string, userId: string, projectVersion: string) { let customerKey = localStorage.getItem('collect-customerKey') || ''; let user = localStorage.getItem('collect-userId') || ''; let isNewUser = false if (!customerKey) { customerKey = uuidv4(); localStorage.setItem('collect-customerKey', customerKey); } if (!user) { isNewUser = true; user = userId; localStorage.setItem('collect-userId', userId); } /** * property = { browser: "chrome" browser_version: "89.0.4389.90" deviceModel: "" deviceOs: "windows" deviceOsVersion: "Win10" devicePlatform: "web" screen_height: 864 screen_width: 1536 } */ const property = getDetector(); const obj = { wmVersion: "webfunnyVersionFlag", [keysEnum.happenTime]: new Date().getTime(), [keysEnum.webMonitorId]: webMonitorId, [keysEnum.simpleUrl]: window.location.protocol + '//' +window.location.host + window.location.pathname, [keysEnum.completeUrl]: window.btoa(encodeURIComponent(window.location.href)), [keysEnum.customerKey]: customerKey, [keysEnum.userId]: user, [keysEnum.newStatus]: isNewUser ? 'o' : '', // 注意:这个表示新老用户,这个字段一定要有 //[keysEnum.firstUserParam]: 'dGFn', // 这个暂时不知道做什么 [keysEnum.projectVersion]: projectVersion, [keysEnum.pageKey]: customerKey, [keysEnum.deviceName]: property.devicePlatform, [keysEnum.browserVersion]: property.browser_version, [keysEnum.browserName]: property.browser, [keysEnum.os]: property.deviceOsVersion, [keysEnum.browserInfo]: `${JSON.stringify(property)}` }; return obj } // js异常 export const getJsError = function(data:any, common: any) { const obj = { [keysEnum.errorMessage]: window.btoa(data.errorMsg), [keysEnum.errorStack]: window.btoa(data.errorObj), [keysEnum.uploadType]: UploadTypeEnum.JS_ERROR, [keysEnum.infoType]: UploadTypeEnum.ON_ERROR, ...common }; return obj; } // http ajax请求异常 export const getHttpError = function(data: any, common: any) { const obj = { [keysEnum.uploadType]: UploadTypeEnum.HTTP_LOG, [keysEnum.status]: data.status, [keysEnum.statusText]: data.statusText, [keysEnum.responseText]: data.responseText, [keysEnum.httpUrl]: window.btoa(encodeURIComponent(data.responseURL)), [keysEnum.requestText]: data.request.body, [keysEnum.loadTime]: data.loadTime, [keysEnum.statusResult]: '请求返回', ...common } return obj; } // 资源加载异常 export const getResourceError = function(data: any, common: any) { const obj = { [keysEnum.uploadType]: UploadTypeEnum.RESOURCE_LOAD, [keysEnum.elementType]: data.typeName, [keysEnum.sourceUrl]: window.btoa(encodeURIComponent(data.sourceUrl)), ...common } return obj; } // pv export const getPv = function(data: any, common: any) { const obj = { [keysEnum.uploadType]: UploadTypeEnum.CUSTOMER_PV, [keysEnum.referrer]: window.btoa(encodeURIComponent(data.referrer)), ...common } return obj; } // 用户行为-click export const getBehavior = function(data: any, common: any) { const obj = { [keysEnum.uploadType]: UploadTypeEnum.ELE_BEHAVIOR, [keysEnum.tagName]: data.tagName, [keysEnum.behaviorType]: data.behaviorType, [keysEnum.innerText]: window.btoa(encodeURIComponent(data.innerText)), ...common } return obj; } export const send = function(list: any[]) { const str = this.formatData(list); fetch('//localhost:8011/server/upLog', { body: 'data=' + str, method: 'POST', mode: 'cors', headers: { 'content-type': 'application/x-www-form-urlencoded' } }); } export const formatData = function(list: any[]) { const obj = { logInfo: '' }; list.forEach(item => { obj.logInfo += JSON.stringify(item)+'$$$' }) return JSON.stringify(obj); } // 解析数据方法-测试用的 export const logParseJson = function(data: string) { const paramStr = data.replace(/": Script error\./g, "script error") const param = JSON.parse(paramStr) const { logInfo } = param if (!logInfo) { console.log(param, typeof param) } return logInfo.split("$$$") }