const store = require('../../utils/store.js'); const api = require('../../utils/api.js'); const { syncTabBar } = require('../../utils/tabbar.js'); Page({ data: { pet: {}, report: null, gainText: '+0.0', bill: null, summary: null, sheetShow: false, sheetType: '', }, onLoad() { this._unsub = store.subscribe((pet) => { this.setData({ pet }); if (!this._inited) return; // 首次由 onShow 加载 this.loadData(); }); }, onUnload() { if (this._unsub) this._unsub(); }, onShow() { syncTabBar(this); store .ready() .then(() => { this._inited = true; this.setData({ pet: store.getPet() }); this.loadData(); }) .catch((e) => wx.showToast({ title: e.message || '加载失败', icon: 'none' })); }, loadData() { const id = store.currentPetId(); if (!id) return; Promise.all([api.weeklyReport(id), api.getBill(id, 'month'), api.healthSummary(id)]) .then(([report, bill, summary]) => { const g = report.weight_gain || 0; this.setData({ report, gainText: (g >= 0 ? '+' : '') + g.toFixed(1), bill, summary, }); }) .catch(() => {}); }, openSheet(e) { this.setData({ sheetType: e.currentTarget.dataset.type, sheetShow: true }); }, closeSheet() { this.setData({ sheetShow: false }); }, onAddPet() { this.setData({ sheetType: 'addPet', sheetShow: true }); }, goHistory() { wx.navigateTo({ url: '/pages/history/history' }); }, onShareAppMessage() { // 带上宠物名,转发出去别人一眼知道是谁家的 const n = (this.data.pet && this.data.pet.name) || '我家毛孩子'; return { title: `${n} 的成长报告`, path: '/pages/report/report' }; }, onShareTimeline() { const n = (this.data.pet && this.data.pet.name) || '我家毛孩子'; return { title: `${n} 的成长报告` }; }, });