var storage = require('../../utils/storage') var util = require('../../utils/util') var themeMod = require('../../utils/theme') Page({ data: { theme: { primary: '#FF6B35', primaryLight: '#FF8C5A', primaryBg: '#FFF3ED', primaryRgb: '255,107,53' }, themeStyle: '', totalDuration: 0, totalDurationText: '', totalSessions: 0, maxDuration: 0, maxDurationText: '', currentYear: new Date().getFullYear(), currentMonth: new Date().getMonth() + 1, monthRecords: [], historyList: [], showDayDetail: false, dayDetail: {} }, onLoad: function () { this.refresh() }, onShow: function () { themeMod.applyThemeToPage(this) try { var tb = this.getTabBar() if (tb) tb.setData({ selected: 1 }) } catch (e) {} this.refresh() }, onPullDownRefresh: function () { this.refresh() wx.stopPullDownRefresh() }, refresh: function () { var stats = storage.getTotalStats() var monthKey = this.data.currentYear + '-' + (this.data.currentMonth < 10 ? '0' : '') + this.data.currentMonth var records = storage.getRecordsByMonth(monthKey) var allRecords = [] var all = storage.getRecords() Object.keys(all).forEach(function (key) { allRecords = allRecords.concat(all[key]) }) allRecords.sort(function (a, b) { return b.date.localeCompare(a.date) }) this.setData({ totalDuration: stats.totalDuration, totalDurationText: util.formatDuration(stats.totalDuration), totalSessions: stats.totalSessions, maxDuration: stats.maxDuration, maxDurationText: util.formatDuration(stats.maxDuration), monthRecords: records, historyList: allRecords.slice(0, 30) }) }, onPrevMonth: function () { var year = this.data.currentYear var month = this.data.currentMonth if (month === 1) { year-- month = 12 } else { month-- } this.setData({ currentYear: year, currentMonth: month }) this.refresh() }, onNextMonth: function () { var year = this.data.currentYear var month = this.data.currentMonth if (month === 12) { year++ month = 1 } else { month++ } this.setData({ currentYear: year, currentMonth: month }) this.refresh() }, onDayTap: function (e) { var detail = e.detail var dateStr = detail.year + '-' + (detail.month < 10 ? '0' : '') + detail.month + '-' + (detail.day < 10 ? '0' : '') + detail.day var allRecords = [] var all = storage.getRecords() Object.keys(all).forEach(function (key) { allRecords = allRecords.concat(all[key]) }) var sessions = allRecords.filter(function (r) { return r.date === dateStr }) var totalDur = 0 sessions.forEach(function (r) { totalDur += r.duration }) var calories = Math.round(totalDur * 0.068) this.setData({ showDayDetail: true, dayDetail: { date: dateStr, sessions: sessions.length, duration: totalDur, durationText: util.formatDuration(totalDur), calories: calories } }) }, onCloseDayDetail: function () { this.setData({ showDayDetail: false }) }, noop: function () {}, onDeleteRecord: function (e) { var id = e.currentTarget.dataset.id var self = this wx.showModal({ title: '删除记录', content: '确定要删除这条训练记录吗?', success: function (res) { if (res.confirm) { storage.deleteRecord(id) self.refresh() wx.showToast({ title: '已删除', icon: 'none', duration: 1200 }) } } }) } })