|
|
|
@ -1,8 +1,11 @@ |
|
|
|
// app.js
|
|
|
|
const api = require('./utils/api'); |
|
|
|
|
|
|
|
App({ |
|
|
|
globalData: { |
|
|
|
userInfo: null, |
|
|
|
currentTab: 'index' |
|
|
|
currentTab: 'index', |
|
|
|
sessionStartTime: null |
|
|
|
}, |
|
|
|
|
|
|
|
// 事件总线,用于页面间通信
|
|
|
|
@ -33,15 +36,41 @@ App({ |
|
|
|
this.globalData.currentTab = tabName; |
|
|
|
}, |
|
|
|
|
|
|
|
// 记录用户踪迹
|
|
|
|
recordUserTrace(action, additionalData = {}) { |
|
|
|
const traceData = { |
|
|
|
action: action, |
|
|
|
timestamp: new Date().toISOString(), |
|
|
|
...additionalData |
|
|
|
}; |
|
|
|
api.addUserTrace(traceData); |
|
|
|
}, |
|
|
|
|
|
|
|
onLaunch() { |
|
|
|
console.log('App launched'); |
|
|
|
// 记录用户进入小程序
|
|
|
|
this.recordUserTrace('app_launch'); |
|
|
|
this.globalData.sessionStartTime = new Date().toISOString(); |
|
|
|
}, |
|
|
|
|
|
|
|
onShow() { |
|
|
|
console.log('App shown'); |
|
|
|
// 记录用户显示小程序
|
|
|
|
this.recordUserTrace('app_show'); |
|
|
|
if (!this.globalData.sessionStartTime) { |
|
|
|
this.globalData.sessionStartTime = new Date().toISOString(); |
|
|
|
} |
|
|
|
}, |
|
|
|
|
|
|
|
onHide() { |
|
|
|
console.log('App hidden'); |
|
|
|
// 记录用户隐藏小程序,包含会话时长
|
|
|
|
const sessionEndTime = new Date().toISOString(); |
|
|
|
this.recordUserTrace('app_hide', { |
|
|
|
sessionStartTime: this.globalData.sessionStartTime, |
|
|
|
sessionEndTime: sessionEndTime, |
|
|
|
sessionDuration: new Date(sessionEndTime) - new Date(this.globalData.sessionStartTime) |
|
|
|
}); |
|
|
|
this.globalData.sessionStartTime = null; |
|
|
|
} |
|
|
|
}); |
|
|
|
|