const store = require('../../utils/store.js'); const api = require('../../utils/api.js'); function pad(n) { return n < 10 ? '0' + n : '' + n; } function fmtTime(iso) { if (!iso) return ''; const d = new Date(iso); const now = new Date(); const hm = pad(d.getHours()) + ':' + pad(d.getMinutes()); if (d.toDateString() === now.toDateString()) return '今天 ' + hm; const y = new Date(now.getTime() - 86400000); if (d.toDateString() === y.toDateString()) return '昨天 ' + hm; return d.getMonth() + 1 + '月' + d.getDate() + '日'; } function mapRecord(r) { return { icon: r.icon || '✍️', title: r.title, desc: fmtTime(r.occurred_at) + (r.description ? '|' + r.description : ''), }; } Page({ data: { pet: {}, timeline: [], sheetShow: false, sheetType: '', }, onLoad() { this._unsub = store.subscribe((pet) => { this.setData({ pet }); this.loadRecords(); }); }, onUnload() { if (this._unsub) this._unsub(); }, onShow() { if (typeof this.getTabBar === 'function' && this.getTabBar()) { this.getTabBar().setData({ selected: 2 }); } store .ready() .then(() => { this.setData({ pet: store.getPet() }); this.loadRecords(); }) .catch((e) => wx.showToast({ title: e.message || '加载失败', icon: 'none' })); }, loadRecords() { const id = store.currentPetId(); if (!id) return; api .getRecords(id) .then((records) => this.setData({ timeline: (records || []).map(mapRecord) })) .catch(() => {}); }, openSheet(e) { this.openSheetType(e.currentTarget.dataset.type); }, openSheetType(type) { this.setData({ sheetType: type, sheetShow: true }); }, closeSheet() { this.setData({ sheetShow: false }); }, onSheetSaved() { this.loadRecords(); }, onAddPet() { this.openSheetType('addPet'); }, onFab() { this.openSheetType('ai'); }, });