import { objToStr } from '../util'; import { version } from '../../package.json'; import * as urllib from 'urllib'; // interface logItemRequired { // code: number; // msg: string; // } // type logItem = Required; interface Option { bmAppid?: string; roleId?: string; sprintId?: string; env?: string; beforeRequest?: Function; } const serverUrl = 'https://mdap.alipay.com/loggw/dwcookieLogGet.do'; export default class { private bmAppid: string; private roleId: string; private eventId: string = '102022'; // PC 端监控事件 private sprintId: string; private env: string; private beforeRequest?: Function; constructor(opts: Option = {}) { this.bmAppid = opts.bmAppid || ''; this.roleId = opts.roleId || ''; this.sprintId = opts.sprintId || ''; this.env = opts.env || ''; this.beforeRequest = opts.beforeRequest; } /** * 上报信息 * @param logItem */ async log(logItem = {}) { const logData = this.parseItem(logItem); if (!logData.length) { return; } return this.sendRequest(logData); } // private async detectHash(hash: string) { // const url = `https://dataservice.alipayobjects.com/alertserver/hash/${hash}`; // const { status } = await urllib.request(url); // return status === 200 || status === 304; // } /** * 将上报值添加上必要的参数,并转化为日志格式 * @param item */ private parseItem(item: any): string[] { if (!item || typeof item !== 'object') { return []; } // 处理日期 const dt = new Date(); // 时间格式补零 const fillDate = num => (num < 10 ? `0${num}` : `${num}`); // 处理用户ID const userId = item.roleId || this.roleId || ''; delete item.roleId; const param4: any = { ...item, bm_appid: this.bmAppid, }; if (this.sprintId) param4.bm_sid = this.sprintId; if (this.env) param4.env = this.env; param4.tracert_ver = version; const data = [ 'D-AE', // 1. 日志头 `${dt.getFullYear()}-${fillDate(dt.getMonth() + 1)}-${fillDate(dt.getDate())} ${fillDate( dt.getHours(), )}:${fillDate(dt.getMinutes())}:${fillDate(dt.getSeconds())}:${dt.getMilliseconds()}`, // 2. 客户端日志时间 '', // this.clientId, // 3. 客户端ID '', // 4. 客户端版本 '2', // 5. 日志版本 '', // 6. 终端ID '', // sessionId, // 7. 会话ID userId, // 8. 用户ID '1000', // 9. 采样率 this.eventId, // 10. 事件ID 'H5behavior', // 11. bizType '2', // 12. logLevel默认值 '', // 13. pageId '', // 14. abtestId '', // 15. ucid用例编号 '', // 16. 补丁版本 '', // 17. 操作系统类别 '', // 18. 操作系统版本 '', // 19. 网络类型 '', // 20. 手机型号 '', // 21. 内部版本号 '', // 22. 渠道号 '', // 23. UTDID '', // 24. 语言 '', // 25. 制造商 '', // 26. 分辨率 '', // 27. appId '', // 28. 进程名 '', // 29. 手机属性 '', // 30. UA '', // 31. 用户信息预留 '', // 32. 序列id '', // 33. 业务额外参数,两平台保持一致 '', // 34. 扩展1 系统信息 '', // 35. 扩展2 '', // 36. 扩展3 ]; data.push(objToStr(param4, '^', true)); return data; } /** * 发送 post 请求 * @param data 日志格式的数据 */ private async sendRequest(data: string[]) { const sendData = `data=${encodeURIComponent(data.join())}&time=${new Date().getTime()}`; const options = { method: 'POST', content: sendData, headers: { 'Sec-Fetch-Mode': 'cors', 'Content-type': 'application/x-www-form-urlencoded', }, } as any; if (typeof this.beforeRequest === 'function') { this.beforeRequest(options); } return urllib.request(serverUrl, options); } }