feat:架构代码优化
This commit is contained in:
@@ -3,12 +3,7 @@
|
||||
* 提供分享和下载打印事件的上报功能
|
||||
*/
|
||||
|
||||
// 生成唯一 UUID
|
||||
function generateUUID(): string {
|
||||
const timestamp = Date.now();
|
||||
const random = Math.floor(Math.random() * 1000000);
|
||||
return `${timestamp}-${random}`;
|
||||
}
|
||||
import { getAppUUID } from './uuid';
|
||||
|
||||
// 格式化时间为 yyyy-MM-dd HH:mm
|
||||
function formatTime(date: Date = new Date()): string {
|
||||
@@ -65,29 +60,24 @@ function getEventCount(eventName: string): number {
|
||||
}
|
||||
|
||||
/**
|
||||
* 埋点追踪器
|
||||
* 埋点追踪器(单例模式)
|
||||
*/
|
||||
class Tracker {
|
||||
private uuid: string;
|
||||
private static instance: Tracker | null = null;
|
||||
private openLog: boolean;
|
||||
|
||||
constructor() {
|
||||
// 初始化时获取或生成 UUID(持久化存储)
|
||||
this.uuid = this.getOrCreateUUID();
|
||||
private constructor() {
|
||||
this.openLog = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取或创建 UUID
|
||||
* 获取 Tracker 单例实例
|
||||
*/
|
||||
private getOrCreateUUID(): string {
|
||||
const uuidKey = 'tracker_uuid';
|
||||
let storedUUID = wx.getStorageSync(uuidKey);
|
||||
if (!storedUUID) {
|
||||
storedUUID = generateUUID();
|
||||
wx.setStorageSync(uuidKey, storedUUID);
|
||||
public static getInstance(): Tracker {
|
||||
if (!Tracker.instance) {
|
||||
Tracker.instance = new Tracker();
|
||||
}
|
||||
return storedUUID;
|
||||
return Tracker.instance;
|
||||
}
|
||||
|
||||
printLog(tip: string, message?: string | object): void {
|
||||
@@ -104,20 +94,34 @@ class Tracker {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成公共埋点属性
|
||||
* @param eventName 事件名称
|
||||
* @param pageName 页面名称
|
||||
* @param extraParams 额外的参数对象
|
||||
* @returns 包含公共属性和额外参数的埋点参数对象
|
||||
*/
|
||||
private getCommonEventParams(
|
||||
eventName: string,
|
||||
pageName: string,
|
||||
extraParams?: Record<string, any>,
|
||||
): Record<string, any> {
|
||||
return {
|
||||
count: getEventCount(eventName),
|
||||
date_time: formatTime(),
|
||||
uuid: getAppUUID(),
|
||||
page_name: pageName,
|
||||
...extraParams,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 上报分享点击事件
|
||||
* @param pageName 页面名称
|
||||
*/
|
||||
reportShare(pageName: string): void {
|
||||
try {
|
||||
const time = formatTime();
|
||||
const count = getEventCount('share_click');
|
||||
const params = {
|
||||
count,
|
||||
time,
|
||||
uuid: this.uuid,
|
||||
page_name: pageName,
|
||||
};
|
||||
const params = this.getCommonEventParams('share_click', pageName);
|
||||
|
||||
this.printLog('分享事件', params);
|
||||
wx.reportEvent('share_click', params);
|
||||
@@ -129,17 +133,13 @@ class Tracker {
|
||||
/**
|
||||
* 上报下载打印事件
|
||||
* @param pageName 页面名称
|
||||
* @param mode 模式(可选)
|
||||
*/
|
||||
reportDownload(pageName: string): void {
|
||||
reportDownload(pageName: string, mode?: string): void {
|
||||
try {
|
||||
const time = formatTime();
|
||||
const count = getEventCount('download');
|
||||
const params = {
|
||||
count,
|
||||
time,
|
||||
uuid: this.uuid,
|
||||
page_name: pageName,
|
||||
};
|
||||
const params = this.getCommonEventParams('download', pageName, {
|
||||
...(mode && { mode }),
|
||||
});
|
||||
|
||||
this.printLog('下载事件', params);
|
||||
wx.reportEvent('download', params);
|
||||
@@ -149,7 +149,7 @@ class Tracker {
|
||||
}
|
||||
}
|
||||
|
||||
// 创建并导出 tracker 实例
|
||||
const tracker = new Tracker();
|
||||
// 导出 Tracker 单例实例
|
||||
const tracker = Tracker.getInstance();
|
||||
|
||||
export default tracker;
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
const UUID_STORAGE_KEY = 'app_uuid';
|
||||
|
||||
/**
|
||||
* 生成一个随机十六进制字符
|
||||
*/
|
||||
function randomHexChar(): string {
|
||||
return Math.floor(Math.random() * 16).toString(16);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成符合 UUID v4 标准的 UUID
|
||||
* UUID v4 格式:xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
|
||||
* 其中:
|
||||
* - x 是任意十六进制数字
|
||||
* - 第 13 个字符必须是 '4'(表示版本 4)
|
||||
* - 第 17 个字符必须是 8, 9, a, 或 b 中的一个(表示变体)
|
||||
*
|
||||
* @returns 符合 UUID v4 标准的字符串
|
||||
*/
|
||||
export function generateUUID(): string {
|
||||
// UUID v4 格式:xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
|
||||
// 生成随机十六进制数字
|
||||
const chars: string[] = [];
|
||||
|
||||
// 生成 32 个十六进制字符
|
||||
for (let i = 0; i < 32; i++) {
|
||||
if (i === 12) {
|
||||
// 第 13 个字符必须是 '4'(版本号)
|
||||
chars[i] = '4';
|
||||
} else if (i === 16) {
|
||||
// 第 17 个字符必须是 8, 9, a, 或 b 中的一个(变体)
|
||||
const variant = ['8', '9', 'a', 'b'][Math.floor(Math.random() * 4)];
|
||||
chars[i] = variant;
|
||||
} else {
|
||||
chars[i] = randomHexChar();
|
||||
}
|
||||
}
|
||||
|
||||
// 按照 UUID 格式组合:xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
|
||||
return [
|
||||
chars.slice(0, 8).join(''),
|
||||
chars.slice(8, 12).join(''),
|
||||
chars.slice(12, 16).join(''),
|
||||
chars.slice(16, 20).join(''),
|
||||
chars.slice(20, 32).join(''),
|
||||
].join('-');
|
||||
}
|
||||
|
||||
export function setAppUUID(uuid: string): void {
|
||||
wx.setStorageSync(UUID_STORAGE_KEY, uuid);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取应用的 UUID
|
||||
* 优先从 globalData 获取,如果没有则从 localStorage 获取
|
||||
*/
|
||||
export function getAppUUID(): string {
|
||||
try {
|
||||
// 尝试从 globalData 获取
|
||||
const app = getApp<IAppOption>();
|
||||
if (app && app.globalData && app.globalData.uuid) {
|
||||
return app.globalData.uuid;
|
||||
}
|
||||
|
||||
// 如果 globalData 中没有,从 localStorage 获取
|
||||
const uuid = wx.getStorageSync(UUID_STORAGE_KEY);
|
||||
if (uuid) {
|
||||
return uuid;
|
||||
}
|
||||
|
||||
// 如果都没有,返回空字符串(这种情况不应该发生,因为 app.ts 会在启动时生成)
|
||||
console.warn('UUID 未找到,请确保应用已正确启动');
|
||||
return '';
|
||||
} catch (error) {
|
||||
console.error('获取 UUID 失败:', error);
|
||||
return '';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user