//logs.js const util = require('../../utils/util.js') const app = getApp(); Page({ data: { logs: [], activeIndex: 0, dayList: [], list: [], sum: [ { title: '今日番茄次数', val: '0' }, { title: '累计番茄次数', val: '0' }, { title: '今日专注时长', val: '0分钟' }, { title: '累计专注时长', val: '0分钟' } ], cateArr: [ { icon: 'work', text: '工作' }, { icon: 'study', text: "学习", }, { icon: 'think', text: '思考' }, { icon: 'write', text: '写作' }, { icon: 'sport', text: '运动' }, { icon: 'read', text: "阅读" } ], containerStyle: '', // 新增动态样式属性 theme: {} // 存储当前主题信息 }, onLoad: function () { this.setData({ theme: app.globalData.theme }); this.registerThemeListener(); this.updateThemeStyles(); this.updateNavigationBarColor(); // 页面加载时更新导航栏颜色 }, onShow: function () { try { // 获取本地存储的日志数据 var logs = wx.getStorageSync('logs') || []; console.log('获取到的日志数量:', logs.length); // 打印最后一条日志(用于调试) if (logs.length > 0) { console.log('最后一条日志:', logs[logs.length - 1]); } // 数据清洗:移除无效日志(可选,根据实际情况决定是否需要) const validLogs = logs.filter(log => { // 确保日志包含date和time字段,且格式正确 return log.date && typeof log.date === 'string' && !isNaN(parseInt(log.time)); }); // 如果有无效日志,更新存储 if (validLogs.length !== logs.length) { wx.setStorageSync('logs', validLogs); console.log('已清理无效日志,有效日志数量:', validLogs.length); logs = validLogs; } // 初始化统计数据 var day = 0; // 今日番茄次数 var total = logs.length; // 累计番茄次数 var dayTime = 0; // 今日专注时长(分钟) var totalTime = 0; // 累计专注时长(分钟) var dayList = []; // 提前计算今日日期字符串(YYYY-MM-DD格式) const todayDate = this.formatDate(new Date()); // 遍历日志数据进行统计 if (logs.length > 0) { for (var i = 0; i < logs.length; i++) { const log = logs[i]; // 安全获取日志日期 const logDateStr = (log.date || '').toString(); // 安全获取日志时长(分钟) const logTime = parseInt(log.time) || 0; // 累计总时长 totalTime += logTime; // 检查是否为今日记录 if (logDateStr.substr(0, 10) === todayDate) { day += 1; // 今日次数+1 dayTime += logTime; // 累计今日时长 dayList.push(logs[i]); } } } // 更新页面数据 this.setData({ 'sum[0].val': day, 'sum[1].val': total, 'sum[2].val': dayTime + '分钟', 'sum[3].val': totalTime + '分钟', dayList: dayList, list: dayList }); console.log('统计结果:', { day, total, dayTime, totalTime }); } catch (error) { console.error('日志统计出错:', error); wx.showToast({ title: '数据加载失败', icon: 'none', duration: 2000 }); } this.setData({ theme: app.globalData.theme }); this.updateThemeStyles(); this.updateNavigationBarColor(); // 页面显示时更新导航栏颜色 }, // 日期格式化工具函数(确保与日志记录时的格式一致) formatDate: function (date) { const year = date.getFullYear(); const month = date.getMonth() + 1; const day = date.getDate(); return `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')}`; }, changeType: function (e) { var index = e.currentTarget.dataset.index; if (index == 0) { this.setData({ list: this.data.dayList }) } else if (index == 1) { var logs = wx.getStorageSync('logs') || []; this.setData({ list: logs }) } this.setData({ activeIndex: index }) //console.log(e.currentTarget.dataset.index); }, registerThemeListener: function() { if (app.eventBus) { app.eventBus.on('themeChange', (themeData) => { console.log('接收到主题数据', themeData); this.setData({ theme: themeData }); this.updateThemeStyles(); this.updateNavigationBarColor(); // 主题变更时更新导航栏颜色 }); } }, updateThemeStyles: function() { const { theme } = this.data; const { bgColor, textColor } = theme; this.setData({ containerStyle: `background-color: ${bgColor}; color: ${textColor};` }); }, updateNavigationBarColor: function() { const { theme } = this.data; wx.setNavigationBarColor({ frontColor: theme.textColor === '#333' ? '#000000' : '#ffffff', backgroundColor: theme.primaryColor }); } })