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