2026-01-16 15:09:19 +08:00
|
|
|
|
import { request } from "@/utils/request";
|
2026-01-09 11:24:40 +08:00
|
|
|
|
|
|
|
|
|
|
// 生成并缓存 device_id
|
|
|
|
|
|
function getOrCreateDeviceId() {
|
2026-01-16 15:09:19 +08:00
|
|
|
|
let deviceId = uni.getStorageSync("device_id");
|
2026-01-09 11:24:40 +08:00
|
|
|
|
if (!deviceId) {
|
2026-01-16 15:09:19 +08:00
|
|
|
|
deviceId = Date.now() + "_" + Math.random().toString(36).substr(2, 9);
|
|
|
|
|
|
uni.setStorageSync("device_id", deviceId);
|
2026-01-09 11:24:40 +08:00
|
|
|
|
}
|
2026-01-16 15:09:19 +08:00
|
|
|
|
return deviceId;
|
2026-01-09 11:24:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 获取网络类型
|
|
|
|
|
|
function getNetworkType() {
|
|
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
|
|
uni.getNetworkType({
|
2026-01-16 15:09:19 +08:00
|
|
|
|
success: (res) => resolve(res.networkType || "unknown"),
|
|
|
|
|
|
fail: () => resolve("unknown"),
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
2026-01-09 11:24:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 获取系统平台
|
|
|
|
|
|
function getSystemInfo() {
|
|
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
|
|
uni.getSystemInfo({
|
|
|
|
|
|
success: (res) => resolve(res),
|
2026-01-16 15:09:19 +08:00
|
|
|
|
fail: () => resolve({}),
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
2026-01-09 11:24:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 获取当前页面路径
|
|
|
|
|
|
function getCurrentPagePath() {
|
2026-01-16 15:09:19 +08:00
|
|
|
|
const pages = getCurrentPages();
|
|
|
|
|
|
const currentPage = pages[pages.length - 1];
|
|
|
|
|
|
return currentPage?.route || "";
|
2026-01-09 11:24:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function getMiniProgramVersion() {
|
2026-01-16 15:09:19 +08:00
|
|
|
|
const platform = process.env.UNI_PLATFORM;
|
|
|
|
|
|
let version = "";
|
|
|
|
|
|
let envVersion = "";
|
2026-01-09 11:24:40 +08:00
|
|
|
|
|
|
|
|
|
|
try {
|
2026-01-16 15:09:19 +08:00
|
|
|
|
if (platform === "mp-weixin") {
|
2026-01-09 11:24:40 +08:00
|
|
|
|
// 微信小程序
|
2026-01-16 15:09:19 +08:00
|
|
|
|
const info = wx.getAccountInfoSync();
|
|
|
|
|
|
version = info?.miniProgram?.version || "";
|
|
|
|
|
|
envVersion = info?.miniProgram?.envVersion || "";
|
|
|
|
|
|
} else if (platform === "mp-alipay") {
|
2026-01-09 11:24:40 +08:00
|
|
|
|
// 支付宝小程序
|
2026-01-16 15:09:19 +08:00
|
|
|
|
const info = my.getAppInfoSync();
|
|
|
|
|
|
version = info?.version || "";
|
|
|
|
|
|
} else if (platform === "mp-toutiao" || platform === "mp-jd") {
|
2026-01-09 11:24:40 +08:00
|
|
|
|
// 抖音/头条/京东小程序
|
2026-01-16 15:09:19 +08:00
|
|
|
|
const info = tt.getAccountInfoSync?.();
|
|
|
|
|
|
version = info?.miniProgram?.version || "";
|
|
|
|
|
|
envVersion = info?.miniProgram?.envVersion || "";
|
|
|
|
|
|
} else if (platform === "mp-baidu") {
|
2026-01-09 11:24:40 +08:00
|
|
|
|
// 百度小程序(无标准方法获取版本号)
|
2026-01-16 15:09:19 +08:00
|
|
|
|
version = ""; // 百度不支持获取版本号
|
2026-01-09 11:24:40 +08:00
|
|
|
|
} else {
|
2026-01-16 15:09:19 +08:00
|
|
|
|
version = "";
|
2026-01-09 11:24:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
} catch (e) {
|
2026-01-16 15:09:19 +08:00
|
|
|
|
version = "";
|
|
|
|
|
|
envVersion = "";
|
2026-01-09 11:24:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
2026-01-16 15:09:19 +08:00
|
|
|
|
platform, // 当前小程序平台
|
|
|
|
|
|
version, // 小程序版本号
|
|
|
|
|
|
envVersion, // develop / trial / release(微信等支持)
|
|
|
|
|
|
};
|
2026-01-09 11:24:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 构造埋点对象
|
2026-01-16 15:09:19 +08:00
|
|
|
|
async function buildEventData(
|
|
|
|
|
|
eventName,
|
|
|
|
|
|
eventType = "click",
|
|
|
|
|
|
customParams = {}
|
|
|
|
|
|
) {
|
|
|
|
|
|
const deviceId = getOrCreateDeviceId();
|
|
|
|
|
|
const systemInfo = await getSystemInfo();
|
|
|
|
|
|
const networkType = await getNetworkType();
|
2026-01-09 11:24:40 +08:00
|
|
|
|
// const location = await getLocation()
|
2026-01-16 15:09:19 +08:00
|
|
|
|
const appVersion = typeof plus !== "undefined" ? plus?.runtime?.version : "";
|
|
|
|
|
|
const { envVersion, platform, version } = getMiniProgramVersion();
|
2026-01-09 11:24:40 +08:00
|
|
|
|
|
|
|
|
|
|
return {
|
2026-01-16 15:09:19 +08:00
|
|
|
|
userId: uni.getStorageSync("user_id") || null,
|
2026-01-09 11:24:40 +08:00
|
|
|
|
deviceId: deviceId,
|
|
|
|
|
|
eventName: eventName,
|
|
|
|
|
|
eventType: eventType,
|
|
|
|
|
|
eventTime: new Date(),
|
|
|
|
|
|
page: getCurrentPagePath(),
|
|
|
|
|
|
elementId: customParams.element_id || null,
|
|
|
|
|
|
elementContent: customParams.element_content || null,
|
|
|
|
|
|
customParams: customParams,
|
|
|
|
|
|
networkType: networkType,
|
2026-01-16 15:09:19 +08:00
|
|
|
|
os: systemInfo.platform || "unknown",
|
|
|
|
|
|
appVersion: version || "unknown",
|
|
|
|
|
|
envVersion: envVersion || "",
|
|
|
|
|
|
platform: platform || "",
|
|
|
|
|
|
};
|
2026-01-09 11:24:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 发送埋点数据到服务器
|
2026-01-16 15:09:19 +08:00
|
|
|
|
async function sendTrack(eventName, eventType = "click", customParams = {}) {
|
2026-01-09 11:24:40 +08:00
|
|
|
|
const eventData = await buildEventData(eventName, eventType, customParams);
|
|
|
|
|
|
request({
|
2026-01-16 15:09:19 +08:00
|
|
|
|
url: "/api/common/tracking/create",
|
|
|
|
|
|
method: "POST",
|
|
|
|
|
|
data: eventData,
|
|
|
|
|
|
});
|
2026-01-09 11:24:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export default {
|
2026-01-16 15:09:19 +08:00
|
|
|
|
sendTrack,
|
|
|
|
|
|
};
|