first commit
This commit is contained in:
1
utils/common.js
Normal file
1
utils/common.js
Normal file
@@ -0,0 +1 @@
|
||||
|
||||
18
utils/date.js
Normal file
18
utils/date.js
Normal file
@@ -0,0 +1,18 @@
|
||||
export function formatDate(isoString, format = 'YYYY-MM-DD HH:mm:ss') {
|
||||
if (!isoString) return ''
|
||||
const date = new Date(isoString)
|
||||
if (isNaN(date.getTime())) return ''
|
||||
|
||||
const pad = (n) => n.toString().padStart(2, '0')
|
||||
|
||||
const map = {
|
||||
YYYY: date.getFullYear().toString(),
|
||||
MM: pad(date.getMonth() + 1),
|
||||
DD: pad(date.getDate()),
|
||||
HH: pad(date.getHours()),
|
||||
mm: pad(date.getMinutes()),
|
||||
ss: pad(date.getSeconds())
|
||||
}
|
||||
|
||||
return format.replace(/YYYY|MM|DD|HH|mm|ss/g, token => map[token])
|
||||
}
|
||||
30
utils/login.js
Normal file
30
utils/login.js
Normal file
@@ -0,0 +1,30 @@
|
||||
export const wxLogin = () => {
|
||||
return new Promise((resolve, reject) => {
|
||||
wx.login({
|
||||
success: res => {
|
||||
if (res.code) {
|
||||
resolve(res.code)
|
||||
} else {
|
||||
reject('登录失败:code为空')
|
||||
}
|
||||
},
|
||||
fail: err => {
|
||||
reject(err)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export const wxGetUserProfile = () => {
|
||||
return new Promise((resolve, reject) => {
|
||||
wx.getUserProfile({
|
||||
desc: '用于完善用户信息',
|
||||
success: res => {
|
||||
resolve(res)
|
||||
},
|
||||
fail: err => {
|
||||
reject(err)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
55
utils/request.js
Normal file
55
utils/request.js
Normal file
@@ -0,0 +1,55 @@
|
||||
// const BASE_URL = 'https://apis.lihailezzc.com'
|
||||
const BASE_URL = 'http://127.0.0.1:3999'
|
||||
export const request = (config = {}) => {
|
||||
const {
|
||||
url,
|
||||
method="GET",
|
||||
header={},
|
||||
data={}
|
||||
} = config
|
||||
|
||||
// 默认 header
|
||||
const defaultHeader = {
|
||||
'x-app-id': '68774dc2d7a1efe42086078a',
|
||||
};
|
||||
|
||||
// 合并 header(用户传入的覆盖默认的)
|
||||
const finalHeader = {
|
||||
...defaultHeader,
|
||||
...header
|
||||
};
|
||||
|
||||
return new Promise((reslove, reject) => {
|
||||
uni.request({
|
||||
url: BASE_URL + url,
|
||||
method,
|
||||
header: finalHeader,
|
||||
data,
|
||||
success: res => {
|
||||
if(res.statusCode === 200 || res.statusCode === 201) {
|
||||
if(res?.data?.code === 200) {
|
||||
reslove(res.data.data)
|
||||
} else {
|
||||
uni.showModal({
|
||||
title: "错误提示",
|
||||
content: res.data.msg || '',
|
||||
showCancel: false
|
||||
})
|
||||
reject(res.data)
|
||||
}
|
||||
} else {
|
||||
uni.showModal({
|
||||
title: "错误提示",
|
||||
content: res.data.errMsg,
|
||||
showCancel: false
|
||||
})
|
||||
reject(res.data)
|
||||
}
|
||||
},
|
||||
fail: err => {
|
||||
reject(err)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
}
|
||||
47
utils/system.js
Normal file
47
utils/system.js
Normal file
@@ -0,0 +1,47 @@
|
||||
const SYSTEM = uni.getSystemInfoSync()
|
||||
|
||||
|
||||
export const getStatusBarHeight = () => SYSTEM.statusBarHeight || 15
|
||||
export const getTitleBarHeight = () => {
|
||||
if(uni.getMenuButtonBoundingClientRect) {
|
||||
const { top, height } = uni.getMenuButtonBoundingClientRect()
|
||||
return height + (top - getStatusBarHeight()) * 2
|
||||
} else {
|
||||
return 40
|
||||
}
|
||||
}
|
||||
|
||||
export const getBavBarHeight = () => getStatusBarHeight() + getTitleBarHeight()
|
||||
|
||||
export const getLeftIconLeft = () => {
|
||||
// if(tt?.getMenuButtonBoundingClientRect) {
|
||||
// const { leftIcon: {left, width}} = tt.getMenuButtonBoundingClientRect
|
||||
// return left + parseInt(width) + 5
|
||||
// } else {
|
||||
// return 0
|
||||
// }
|
||||
// #ifdef MP-TOUTIAO
|
||||
const { leftIcon: {left, width}} = tt.getCustomButtonBoundingClientRect()
|
||||
return left + parseInt(width)
|
||||
// #endif
|
||||
// #ifndef MP-TOUTIAO
|
||||
return 0
|
||||
// #endif
|
||||
}
|
||||
|
||||
|
||||
export function getPlatformProvider() {
|
||||
const platform = process.env.UNI_PLATFORM
|
||||
return platform ?? 'mp-weixin'
|
||||
// const platform = uni.getSystemInfoSync().platform;
|
||||
// console.log(1111111, platform)
|
||||
// // H5 模拟器调试时使用 __wxConfig 环境变量判断
|
||||
// if (typeof __wxConfig !== 'undefined') return 'weixin';
|
||||
// if (platform === 'devtools') {
|
||||
// // 可以根据小程序环境变量判断
|
||||
// // 抖音开发工具会定义 tt 对象
|
||||
// return typeof tt !== 'undefined' ? 'toutiao' : 'weixin';
|
||||
// }
|
||||
|
||||
// return typeof tt !== 'undefined' ? 'toutiao' : 'weixin';
|
||||
}
|
||||
117
utils/track.js
Normal file
117
utils/track.js
Normal file
@@ -0,0 +1,117 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user