// components/manual-add-sheet — 手动补记弹层(docs/05 R7) // 用法:父页放 // this.selectComponent('#addSheet').open() const { call } = require('../../utils/cloud.js'); function pad(n) { return String(n).padStart(2, '0'); } Component({ properties: { dogs: { type: Array, value: [] }, }, data: { show: false, dogId: '', date: '', time: '18:00', durationMin: '', distance: '', submitting: false, }, methods: { open() { const dogs = this.data.dogs; const t = new Date(); const date = `${t.getFullYear()}-${pad(t.getMonth() + 1)}-${pad(t.getDate())}`; this.setData({ show: true, dogId: dogs[0] ? dogs[0]._id : '', date, time: '18:00', durationMin: '', distance: '', }); }, close() { this.setData({ show: false }); }, noop() {}, pickDog(e) { this.setData({ dogId: e.currentTarget.dataset.id }); }, onDate(e) { this.setData({ date: e.detail.value }); }, onTime(e) { this.setData({ time: e.detail.value }); }, onDuration(e) { this.setData({ durationMin: e.detail.value }); }, onDistance(e) { this.setData({ distance: e.detail.value }); }, async save() { const { dogId, date, time, durationMin, distance, submitting } = this.data; if (submitting) return; if (!dogId) return wx.showToast({ title: '选择狗狗', icon: 'none' }); if (!durationMin || Number(durationMin) <= 0) return wx.showToast({ title: '填写时长', icon: 'none' }); const startAt = new Date(`${date}T${time || '00:00'}:00`).getTime(); this.setData({ submitting: true }); const res = await call('walkManual', { dogId, startAt, duration: Math.round(Number(durationMin) * 60), distance: distance ? Number(distance) : null, }, { loading: true, loadingText: '保存中' }); this.setData({ submitting: false }); if (!res.ok) return; this.setData({ show: false }); wx.showToast({ title: '补记成功', icon: 'success' }); this.triggerEvent('saved'); }, }, });