Files
Pet3/miniprogram/pages/history/history.js
2026-06-25 18:24:29 +08:00

44 lines
1.4 KiB
JavaScript

// pages/history/history — 5 遛狗历史 + 手动补记
const { call } = require('../../utils/cloud.js');
const store = require('../../utils/store.js');
const { formatDurationCN, formatDistance, toDateStr } = require('../../utils/format.js');
Page({
data: { groups: [], dogs: [] },
onShow() { this.load(); },
async load() {
const dogs = store.getState().dogs || [];
this.setData({ dogs });
const res = await call('getHistory', {});
if (!res.ok) return;
this.setData({ groups: this.groupByWeek(res.data.walks) });
},
// 本周 / 更早
groupByWeek(walks) {
const now = new Date();
const dow = now.getDay() || 7; // 周一=1
const weekStart = new Date(now.getFullYear(), now.getMonth(), now.getDate() - (dow - 1)).getTime();
const thisWeek = [];
const earlier = [];
walks.forEach((w) => {
const item = {
_id: w._id,
title: `${toDateStr(w.startAt)} · ${(w.dogNames || []).join('、')}`,
sub: `${formatDurationCN(w.duration)} · ${formatDistance(w.distance)}`,
isManual: w.isManual,
};
(w.startAt >= weekStart ? thisWeek : earlier).push(item);
});
const groups = [];
if (thisWeek.length) groups.push({ label: '本周', items: thisWeek });
if (earlier.length) groups.push({ label: '更早', items: earlier });
return groups;
},
openAdd() { this.selectComponent('#addSheet').open(); },
onSaved() { this.load(); },
});