You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

76 lines
2.0 KiB

// app.js
const api = require('./utils/api');
App({
globalData: {
userInfo: null,
currentTab: 'index',
sessionStartTime: null
},
// 事件总线,用于页面间通信
eventBus: {
on(event, callback) {
if (!this.handlers) this.handlers = {};
if (!this.handlers[event]) this.handlers[event] = [];
this.handlers[event].push(callback);
},
off(event, callback) {
if (!this.handlers || !this.handlers[event]) return;
this.handlers[event] = this.handlers[event].filter(handler => handler !== callback);
},
emit(event, data) {
if (!this.handlers || !this.handlers[event]) return;
this.handlers[event].forEach(handler => {
try {
handler(data);
} catch (error) {
console.error('事件处理函数执行错误:', error);
}
});
}
},
// 更新当前选中的tab
updateCurrentTab(tabName) {
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;
}
});