初始提交
This commit is contained in:
114
rg-09112127/js/accessTracker.js
Normal file
114
rg-09112127/js/accessTracker.js
Normal file
@@ -0,0 +1,114 @@
|
||||
/**
|
||||
* 访问跟踪器 - 记录用户访问行为
|
||||
*/
|
||||
class AccessTracker {
|
||||
constructor() {
|
||||
this.currentCourseId = null;
|
||||
this.currentGameId = null;
|
||||
this.accessStartTime = null;
|
||||
this.accessType = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 开始跟踪课程访问
|
||||
*/
|
||||
startCourseAccess(courseId) {
|
||||
this.currentCourseId = courseId;
|
||||
this.accessType = 'course';
|
||||
this.accessStartTime = Date.now();
|
||||
|
||||
console.log('开始跟踪课程访问:', courseId);
|
||||
|
||||
// 立即记录访问开始
|
||||
this.recordAccess(courseId, null, 'course', 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 开始跟踪游戏访问
|
||||
*/
|
||||
startGameAccess(gameId) {
|
||||
this.currentGameId = gameId;
|
||||
this.accessType = 'game';
|
||||
this.accessStartTime = Date.now();
|
||||
|
||||
console.log('开始跟踪游戏访问:', gameId);
|
||||
|
||||
// 立即记录访问开始
|
||||
this.recordAccess(null, gameId, 'game', 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 开始跟踪视频访问
|
||||
*/
|
||||
startVideoAccess(courseId, videoId) {
|
||||
this.currentCourseId = courseId;
|
||||
this.accessType = 'video';
|
||||
this.accessStartTime = Date.now();
|
||||
|
||||
console.log('开始跟踪视频访问:', courseId, videoId);
|
||||
|
||||
// 立即记录访问开始
|
||||
this.recordAccess(courseId, null, 'video', 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 结束访问跟踪
|
||||
*/
|
||||
endAccess() {
|
||||
if (!this.accessStartTime) return;
|
||||
|
||||
const duration = Math.floor((Date.now() - this.accessStartTime) / 1000);
|
||||
|
||||
console.log('结束访问跟踪:', {
|
||||
type: this.accessType,
|
||||
courseId: this.currentCourseId,
|
||||
gameId: this.currentGameId,
|
||||
duration: duration
|
||||
});
|
||||
|
||||
// 记录访问结束
|
||||
this.recordAccess(this.currentCourseId, this.currentGameId, this.accessType, duration);
|
||||
|
||||
// 重置状态
|
||||
this.reset();
|
||||
}
|
||||
|
||||
/**
|
||||
* 记录访问
|
||||
*/
|
||||
async recordAccess(courseId, gameId, accessType, durationSeconds) {
|
||||
try {
|
||||
await window.apiService.recordAccess(courseId, gameId, accessType, durationSeconds);
|
||||
} catch (error) {
|
||||
console.error('记录访问失败:', error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 重置跟踪状态
|
||||
*/
|
||||
reset() {
|
||||
this.currentCourseId = null;
|
||||
this.currentGameId = null;
|
||||
this.accessStartTime = null;
|
||||
this.accessType = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前访问时长
|
||||
*/
|
||||
getCurrentDuration() {
|
||||
if (!this.accessStartTime) return 0;
|
||||
return Math.floor((Date.now() - this.accessStartTime) / 1000);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否正在跟踪
|
||||
*/
|
||||
isTracking() {
|
||||
return this.accessStartTime !== null;
|
||||
}
|
||||
}
|
||||
|
||||
// 创建全局访问跟踪器实例
|
||||
window.accessTracker = new AccessTracker();
|
||||
Reference in New Issue
Block a user