// 频道使用统计管理 window.ChannelStats = { // 初始化 init: function() { console.log('[stats] 初始化'); this.bindEvents(); }, // 绑定事件 bindEvents: function() { if (!window.EventBus) return; // 监听模块加载,开始计时 EventBus.on('module:loaded', (data) => { if (data && data.module) { this.recordView(data.module); } }); // 监听模块卸载,结束计时 EventBus.on('module:unloaded', (data) => { if (data && data.module) { if (window.ChannelPreferences) { ChannelPreferences.stopTiming(data.module); } } }); // 监听页面关闭,停止所有计时 window.addEventListener('beforeunload', () => { if (window.ChannelPreferences && window.ChannelPreferences.timing) { Object.keys(ChannelPreferences.timing).forEach(moduleId => { ChannelPreferences.stopTiming(moduleId); }); } }); }, // 记录模块访问 recordView: function(moduleId) { if (!window.ChannelPreferences) return; // 记录使用次数和开始计时 ChannelPreferences.recordUsage(moduleId); // 发送事件 if (window.EventBus) { EventBus.emit('stats:recorded', { module: moduleId, time: Date.now() }); } }, // 获取统计报告 getStatsReport: function() { if (!window.ChannelPreferences) return {}; const stats = ChannelPreferences.getStats(); const modules = window.ModuleRegistry ? ModuleRegistry.list() : []; // 格式化统计数据 const report = { totalViews: 0, mostUsed: null, lastUsed: null, moduleDetails: {} }; let maxCount = 0; let lastTime = 0; modules.forEach(moduleId => { const moduleStats = stats[moduleId] || { count: 0, lastUsed: null, totalTime: 0 }; report.moduleDetails[moduleId] = { count: moduleStats.count, lastUsed: moduleStats.lastUsed ? new Date(moduleStats.lastUsed).toLocaleString() : '从未使用', totalTime: moduleStats.totalTime ? this.formatTime(moduleStats.totalTime) : '0秒' }; report.totalViews += moduleStats.count; if (moduleStats.count > maxCount) { maxCount = moduleStats.count; report.mostUsed = moduleId; } if (moduleStats.lastUsed && moduleStats.lastUsed > lastTime) { lastTime = moduleStats.lastUsed; report.lastUsed = moduleId; } }); return report; }, // 格式化时间(秒 -> 可读格式) formatTime: function(seconds) { if (seconds < 60) return `${Math.round(seconds)}秒`; if (seconds < 3600) { const minutes = Math.floor(seconds / 60); const secs = Math.round(seconds % 60); return `${minutes}分${secs}秒`; } const hours = Math.floor(seconds / 3600); const minutes = Math.floor((seconds % 3600) / 60); return `${hours}小时${minutes}分`; }, // 渲染统计面板 renderStatsPanel: function(container) { if (!container) return; const report = this.getStatsReport(); const modules = window.ModuleRegistry ? ModuleRegistry.list() : []; let html = `
| 模块 | 访问次数 | 累计使用时长 | 最后使用 |
|---|---|---|---|
| ${this.getModuleName(moduleId)} | ${detail.count} | ${detail.totalTime} | ${detail.lastUsed} |