const store = require('../../utils/store.js'); const api = require('../../utils/api.js'); const { toastErr } = require('../../utils/ui.js'); // "2026-07-04" → "7月4日" function fmtMD(date) { if (!date) return ''; const p = date.split('-'); if (p.length < 3) return ''; return +p[1] + '月' + +p[2] + '日'; } Page({ data: { pet: {}, planView: 'timeline', plan: null, sheetShow: false, sheetType: '', }, onLoad() { this._unsub = store.subscribe((pet) => { this.setData({ pet }); if (!this._inited) return; // 首次由 onShow 加载 this.loadTimeline(); }); }, onUnload() { if (this._unsub) this._unsub(); }, onShow() { store .ready() .then(() => { this._inited = true; this.setData({ pet: store.getPet() }); this.loadTimeline(); }) .catch((e) => wx.showToast({ title: e.message || '加载失败', icon: 'none' })); }, loadTimeline() { const id = store.currentPetId(); if (!id) return; api .getPlan(id) .then((plan) => { if (plan && plan.tasks) { plan.tasks = plan.tasks.map((t) => ({ ...t, dateLabel: fmtMD(t.date) })); } this.setData({ plan }); }) .catch(() => this.setData({ plan: null })); }, toggleTimelineTask(e) { const id = e.currentTarget.dataset.id; api.togglePlanTask(id).then(() => this.loadTimeline()).catch((e) => toastErr(e, '操作失败')); }, openSheet(e) { const type = e.currentTarget.dataset.type; if (!type) return; // 无关联动作的计划项只可勾选,不弹层 this.setData({ sheetType: type, sheetShow: true }); }, closeSheet() { this.setData({ sheetShow: false }); }, onAddPet() { this.setData({ sheetType: 'addPet', sheetShow: true }); }, onShareAppMessage() { return { title: '我给毛孩子做了份养护计划', path: '/pages/plan/plan' }; }, onShareTimeline() { return { title: '我给毛孩子做了份养护计划' }; }, });