feat: 添加按日期查询用电记录功能,支持切换查看历史用电趋势

1. 新增/api/records-by-date后端接口,支持按日期查询详细用电记录并计算当日用量和费用
2. 前端新增日期选择器,支持切换查看指定日期的用电趋势
3. 更新README文档,补充功能说明、配置项和接口文档
4. 新增.trae目录到gitignore
5. 引入IconPack图标库和dayjs日期处理库
This commit is contained in:
EchoZenith
2026-05-24 22:38:32 +08:00
parent 467928f2e2
commit f0b6f9e8dd
5 changed files with 108 additions and 21 deletions

View File

@@ -353,6 +353,36 @@ app.get('/api/current', requireAuth, (req, res) => {
});
});
app.get('/api/records-by-date', requireAuth, (req, res) => {
const dateStr = req.query.date;
if (!dateStr) return res.json({ success: false, message: '缺少 date 参数' });
const dayBefore = new Date(dateStr);
dayBefore.setDate(dayBefore.getDate() - 1);
const dayBeforeStr = getLocalDateStr(dayBefore);
const records = getRecordsByDate(dateStr);
const dayBeforeRecords = getRecordsByDate(dayBeforeStr);
const prevLast = dayBeforeRecords.length > 0 ? dayBeforeRecords[dayBeforeRecords.length - 1] : null;
const sorted = [...records].sort((a, b) => a.timestamp - b.timestamp);
let usage = 0;
let cost = 0;
if (sorted.length >= 1) {
const base = prevLast || sorted[0];
usage = Math.max(0, Math.round((base.surplus - sorted[sorted.length - 1].surplus) * 100) / 100);
cost = Math.max(0, Math.round((base.amount - sorted[sorted.length - 1].amount) * 100) / 100);
}
res.json({
success: true,
records: sorted,
prevLastRecord: prevLast,
usage,
cost,
});
});
app.get('/api/history', requireAuth, (req, res) => {
const allRecords = getAllRecords();
const { dailyRecords } = calculateDailyUsage(allRecords);