const request = require('../utils/request') Page({ data: { totalRecords: 0, firstRecordDate: '', consecutiveDays: 0 }, onShow() { this.updateStats(); }, updateStats() { const records = wx.getStorageSync('records') || []; const totalRecords = records.length; let firstRecordDate = ''; let consecutiveDays = 0; if (totalRecords > 0) { // 计算首次记账日期 const sortedRecords = [...records].sort((a, b) => new Date(a.date) - new Date(b.date)); const firstDate = new Date(sortedRecords[0].date); firstRecordDate = `${firstDate.getFullYear()}-${(firstDate.getMonth() + 1).toString().padStart(2, '0')}-${firstDate.getDate().toString().padStart(2, '0')}`; // 计算连续记账天数 consecutiveDays = this.calculateConsecutiveDays(records); } this.setData({ totalRecords, firstRecordDate, consecutiveDays }); }, calculateConsecutiveDays(records) { // 获取去重的日期列表 const dateSet = new Set(); records.forEach(record => { dateSet.add(record.date); }); const dateList = Array.from(dateSet).sort((a, b) => new Date(b) - new Date(a)); if (dateList.length === 0) { return 0; } // 检查是否包含今天 const today = new Date(); today.setHours(0, 0, 0, 0); const lastDate = new Date(dateList[0]); lastDate.setHours(0, 0, 0, 0); if (lastDate.getTime() !== today.getTime()) { return 0; } // 计算连续天数 let consecutiveDays = 1; for (let i = 0; i < dateList.length - 1; i++) { const currentDate = new Date(dateList[i]); currentDate.setHours(0, 0, 0, 0); const nextDate = new Date(dateList[i + 1]); nextDate.setHours(0, 0, 0, 0); // 检查是否连续(相差一天) const diffTime = currentDate.getTime() - nextDate.getTime(); const diffDays = diffTime / (1000 * 60 * 60 * 24); if (diffDays === 1) { consecutiveDays++; } else { break; } } return consecutiveDays; }, exportData() { const records = wx.getStorageSync('records') || []; if (records.length === 0) { wx.showToast({ title: '暂无数据可导出', icon: 'none', duration: 2000 }); return; } // 转换为CSV格式 let csvContent = "日期,类型,分类,金额,备注\n"; records.forEach(record => { const type = record.type === 'income' ? '收入' : '支出'; csvContent += `${record.date},${type},${record.categoryName},${record.amount},${record.note || ''}\n`; }); // 这里仅做演示,实际导出功能需要后端支持或使用微信的文件系统API wx.showModal({ title: '导出成功', content: '数据已导出为CSV格式', showCancel: false }); }, clearData() { wx.showModal({ title: '确认清空', content: '确定要清空所有记账数据吗?此操作不可恢复。', cancelText: '取消', confirmText: '确认', success: (res) => { if (res.confirm) { wx.setStorageSync('records', []); wx.showToast({ title: '数据已清空', icon: 'success', duration: 2000 }); this.updateStats(); } } }); }, showAbout() { wx.showModal({ title: '关于简易记账', content: '简易记账是一款简单实用的记账工具,帮助你记录和管理个人财务。', showCancel: false, confirmText: '我知道了' }); }, async outLogin() { wx.showModal({ title: '确认退出', content: '是否确认退出?', cancelText: '取消', confirmText: '确认', success: async (res) => { if (res.confirm) { const ress=await request.post('/app-api/member/auth/logout') console.log('resss:',ress) if(ress.code==0){ wx.showToast({ title: '退出登录成功', icon: 'success', duration: 2000 }); wx.redirectTo({ url: `/pages/login/login` }) } } } }); } })