logs.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //logs.js
  2. const util = require('../../utils/util.js')
  3. const app = getApp();
  4. Page({
  5. data: {
  6. logs: [],
  7. activeIndex: 0,
  8. dayList: [],
  9. list: [],
  10. sum: [
  11. {
  12. title: '今日番茄次数',
  13. val: '0'
  14. },
  15. {
  16. title: '累计番茄次数',
  17. val: '0'
  18. },
  19. {
  20. title: '今日专注时长',
  21. val: '0分钟'
  22. },
  23. {
  24. title: '累计专注时长',
  25. val: '0分钟'
  26. }
  27. ],
  28. cateArr: [
  29. {
  30. icon: 'work',
  31. text: '工作'
  32. },
  33. {
  34. icon: 'study',
  35. text: "学习",
  36. },
  37. {
  38. icon: 'think',
  39. text: '思考'
  40. },
  41. {
  42. icon: 'write',
  43. text: '写作'
  44. },
  45. {
  46. icon: 'sport',
  47. text: '运动'
  48. },
  49. {
  50. icon: 'read',
  51. text: "阅读"
  52. }
  53. ],
  54. containerStyle: '', // 新增动态样式属性
  55. theme: {} // 存储当前主题信息
  56. },
  57. onLoad: function () {
  58. this.setData({ theme: app.globalData.theme });
  59. this.registerThemeListener();
  60. this.updateThemeStyles();
  61. this.updateNavigationBarColor(); // 页面加载时更新导航栏颜色
  62. },
  63. onShow: function () {
  64. try {
  65. // 获取本地存储的日志数据
  66. var logs = wx.getStorageSync('logs') || [];
  67. console.log('获取到的日志数量:', logs.length);
  68. // 打印最后一条日志(用于调试)
  69. if (logs.length > 0) {
  70. console.log('最后一条日志:', logs[logs.length - 1]);
  71. }
  72. // 数据清洗:移除无效日志(可选,根据实际情况决定是否需要)
  73. const validLogs = logs.filter(log => {
  74. // 确保日志包含date和time字段,且格式正确
  75. return log.date && typeof log.date === 'string' &&
  76. !isNaN(parseInt(log.time));
  77. });
  78. // 如果有无效日志,更新存储
  79. if (validLogs.length !== logs.length) {
  80. wx.setStorageSync('logs', validLogs);
  81. console.log('已清理无效日志,有效日志数量:', validLogs.length);
  82. logs = validLogs;
  83. }
  84. // 初始化统计数据
  85. var day = 0; // 今日番茄次数
  86. var total = logs.length; // 累计番茄次数
  87. var dayTime = 0; // 今日专注时长(分钟)
  88. var totalTime = 0; // 累计专注时长(分钟)
  89. var dayList = [];
  90. // 提前计算今日日期字符串(YYYY-MM-DD格式)
  91. const todayDate = this.formatDate(new Date());
  92. // 遍历日志数据进行统计
  93. if (logs.length > 0) {
  94. for (var i = 0; i < logs.length; i++) {
  95. const log = logs[i];
  96. // 安全获取日志日期
  97. const logDateStr = (log.date || '').toString();
  98. // 安全获取日志时长(分钟)
  99. const logTime = parseInt(log.time) || 0;
  100. // 累计总时长
  101. totalTime += logTime;
  102. // 检查是否为今日记录
  103. if (logDateStr.substr(0, 10) === todayDate) {
  104. day += 1; // 今日次数+1
  105. dayTime += logTime; // 累计今日时长
  106. dayList.push(logs[i]);
  107. }
  108. }
  109. }
  110. // 更新页面数据
  111. this.setData({
  112. 'sum[0].val': day,
  113. 'sum[1].val': total,
  114. 'sum[2].val': dayTime + '分钟',
  115. 'sum[3].val': totalTime + '分钟',
  116. dayList: dayList,
  117. list: dayList
  118. });
  119. console.log('统计结果:', { day, total, dayTime, totalTime });
  120. } catch (error) {
  121. console.error('日志统计出错:', error);
  122. wx.showToast({
  123. title: '数据加载失败',
  124. icon: 'none',
  125. duration: 2000
  126. });
  127. }
  128. this.setData({ theme: app.globalData.theme });
  129. this.updateThemeStyles();
  130. this.updateNavigationBarColor(); // 页面显示时更新导航栏颜色
  131. },
  132. // 日期格式化工具函数(确保与日志记录时的格式一致)
  133. formatDate: function (date) {
  134. const year = date.getFullYear();
  135. const month = date.getMonth() + 1;
  136. const day = date.getDate();
  137. return `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')}`;
  138. },
  139. changeType: function (e) {
  140. var index = e.currentTarget.dataset.index;
  141. if (index == 0) {
  142. this.setData({
  143. list: this.data.dayList
  144. })
  145. } else if (index == 1) {
  146. var logs = wx.getStorageSync('logs') || [];
  147. this.setData({
  148. list: logs
  149. })
  150. }
  151. this.setData({
  152. activeIndex: index
  153. })
  154. //console.log(e.currentTarget.dataset.index);
  155. },
  156. registerThemeListener: function() {
  157. if (app.eventBus) {
  158. app.eventBus.on('themeChange', (themeData) => {
  159. console.log('接收到主题数据', themeData);
  160. this.setData({ theme: themeData });
  161. this.updateThemeStyles();
  162. this.updateNavigationBarColor(); // 主题变更时更新导航栏颜色
  163. });
  164. }
  165. },
  166. updateThemeStyles: function() {
  167. const { theme } = this.data;
  168. const { bgColor, textColor } = theme;
  169. this.setData({
  170. containerStyle: `background-color: ${bgColor}; color: ${textColor};`
  171. });
  172. },
  173. updateNavigationBarColor: function() {
  174. const { theme } = this.data;
  175. wx.setNavigationBarColor({
  176. frontColor: theme.textColor === '#333' ? '#000000' : '#ffffff',
  177. backgroundColor: theme.primaryColor
  178. });
  179. }
  180. })