Files
sundynix-pets/pets-fe/pages/home/home.js
T
Blizzard 9b30bfaaff feat: 计划按自然月管理,加「应用到每天」和下月未排提醒
## 为什么不是「从计划开始日起 30 天」
上一版是那样做的,截图一看就不对:计划是月中建的,日历显示
「7月26日 — 8月25日」,既不像本月也不像下月,用户想问「这个月还剩什么」
根本对不上。改成自然月,日历就是一眼能认的月历。

能翻到下月 —— 否则「做下月计划」这件事本身做不了。

## 校验从「Day 上限」改成「日期」
PlanTask.Day 是相对计划开始日的,月份边界是相对今天的,两者会错开。
原来卡 Day<=30,计划建了几天之后下月末就换算成 Day=40 多,
用户在日历上明明看得到那格、点进去却存不了。

现在的规则:不早于计划开始日(下界放到开始日而不是本月 1 号——模板生成的
历史节点可能在上个月,那些要允许原地改),不晚于下月月底。

## 应用到每天
一天建一条,不做「一条 + repeat 字段读时展开」:勾选状态是按天的,
展开方案还得再开一张表记「哪天打过勾」,而直接建 N 条天然就有这个能力。

范围止于**这一天所在那个月**的月底,不跨月——「应用到每天」说的是这个月每天,
跨过去就变成用户没要求的事了。只有新增能选,已有的那条改成每天等于凭空
复制 N 份(用户想要的是「以后每天」,不是「把这条撒开」)。

## 首页那个提醒
GET /pets/:id/plan/month-status 返回本月件数/剩余、下月件数、need_next。

need_next 只在**月末最后 7 天**且下月为空时为真:月初就催「排下个月」太早,
用户会当成噪音。角标做在图标上而不是弹 toast——每次进首页弹一下很烦,
而这件事不紧急、看见就行。

副标题有优先级:「8 月计划还没排」压过「本月都做完了」,因为前者有时效。
四种状态实跑验过,包括让位关系。

## 顺手
pet-switch 加 show-add 属性。计划页只需要在既有宠物之间切,建档入口在首页
和「我的」就够了,到处摆一个「添加」会让这条 chip 变得很长。

本月/下月的切换用新的 .link-tabs 而不是挂在 .head-links 上:首页的
「管理 / 全部完成」也用 head-links,那是两个并列动作,不该有一个是灰的。

## 验证(预生产库实跑)
  月边界     本月末通过、下月末通过、下下月拒(报出具体日期)
  应用到每天  今天→本月末 2 条,没跨月
  month-status  下月清空 → need_next 翻 true;补一条 → 翻回 false
  四种副标题  下月未排 / 本月还有 N 件 / 本月都做完了 / 还没有安排,含优先级
  测试数据全部还原

上一条 cd 打错目录,导致「日期窗口」那次改动没写进文件、只有批量那半截进去了,
所以中间出现过一次 planWindow undefined。这次一并改对。

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-30 11:09:06 +08:00

222 lines
6.5 KiB
JavaScript

const store = require('../../utils/store.js');
const api = require('../../utils/api.js');
const { toastErr } = require('../../utils/ui.js');
const { syncTabBar } = require('../../utils/tabbar.js');
const recordTypes = require('../../utils/recordTypes.js');
const petmeta = require('../../utils/petmeta.js');
function mapTask(t) {
return { id: t.id, title: t.title, sub: t.description, priority: t.priority, done: t.done, sheet: t.sheet_type };
}
// 时间轴上的相对时间。首页关心的是「什么时候记的」,精确到分只在今天有意义
function fmtWhen(iso) {
if (!iso) return '';
const d = new Date(iso);
if (isNaN(d.getTime())) return '';
const p = (n) => (n < 10 ? '0' + n : '' + n);
const now = new Date();
const sameDay = d.toDateString() === now.toDateString();
if (sameDay) return '今天 ' + p(d.getHours()) + ':' + p(d.getMinutes());
const y = new Date(now.getTime() - 86400000);
if (d.toDateString() === y.toDateString()) return '昨天 ' + p(d.getHours()) + ':' + p(d.getMinutes());
return d.getMonth() + 1 + '月' + d.getDate() + '日';
}
const TL_PAGE = 10;
Page({
data: {
pet: {},
zodiac: '',
daysTogether: 0,
sexIcon: '',
sexTone: '',
tasks: [],
planHint: '',
needNextPlan: false,
monthCost: 0,
timeline: [],
tlPage: 1,
hasMore: false,
sheetShow: false,
sheetType: '',
},
onLoad() {
this._unsub = store.subscribe((pet) => {
this.applyPet(pet);
if (!this._inited) return;
this.loadAll();
});
},
onUnload() {
if (this._unsub) this._unsub();
},
onShow() {
syncTabBar(this);
recordTypes.load().catch(() => {});
store
.ready()
.then(() => {
this._inited = true;
this.applyPet(store.getPet());
this.loadAll();
})
.catch((e) => wx.showToast({ title: e.message || '加载失败', icon: 'none' }));
},
// 门面上那几样都是本地算的,不用等接口
applyPet(pet) {
const p = pet || {};
const badge = petmeta.genderBadge(p.gender);
this.setData({
pet: p,
zodiac: petmeta.zodiac(p.birthday),
daysTogether: petmeta.daysTogether(p),
sexIcon: badge.icon,
sexTone: badge.tone,
});
},
loadAll() {
this.loadTasks();
this.loadTimeline(1);
this.loadEntries();
},
loadTasks() {
const id = store.currentPetId();
if (!id) return;
api
.getTasks(id)
.then((tasks) => this.setData({ tasks: (tasks || []).map(mapTask) }))
.catch((e) => toastErr(e));
},
// 两个入口上的副标题:计划还剩几件、本月花了多少。
// 空着的话入口就是两个没有信息量的图标
loadEntries() {
const id = store.currentPetId();
if (!id) return;
// 副标题优先说「该排下月计划了」——那是有时效的事;
// 本月还剩几件是随时能看的,让位给它
api
.planMonthStatus(id)
.then((st) => {
const s = st || {};
this.setData({
needNextPlan: !!s.need_next,
planHint: s.need_next
? (s.next_label || '下月') + '计划还没排'
: s.this_undone > 0
? '本月还有 ' + s.this_undone + ' 件'
: s.this_month > 0
? '本月都做完了'
: '还没有安排',
});
})
.catch(() => this.setData({ needNextPlan: false, planHint: '' }));
api
.getBill(id, 'month')
.then((b) => this.setData({ monthCost: (b && b.total) || 0 }))
.catch(() => this.setData({ monthCost: 0 }));
},
loadTimeline(page) {
const id = store.currentPetId();
if (!id) return;
api
.getRecords(id, { page, pageSize: TL_PAGE })
.then((res) => {
const list = (res.list || []).map((r) => ({
id: r.id,
type: r.type,
tone: (recordTypes.get(r.type) || {}).tone || 'tone-1',
title: r.title,
description: r.description,
image: r.image_url || '',
timeText: fmtWhen(r.occurred_at),
}));
const merged = page === 1 ? list : this.data.timeline.concat(list);
this.setData({ timeline: merged, tlPage: page, hasMore: merged.length < (res.total || 0) });
})
.catch(() => {});
},
loadMore() {
if (this.data.hasMore) this.loadTimeline(this.data.tlPage + 1);
},
previewImage(e) {
const url = e.currentTarget.dataset.url;
if (url) wx.previewImage({ urls: [url], current: url });
},
onDeleteRecord(e) {
const id = e.currentTarget.dataset.id;
wx.showModal({
title: '删掉这条记录?',
content: '趋势和统计会跟着变。',
confirmColor: '#EE7D73',
success: (r) => {
if (!r.confirm) return;
api
.deleteRecord(id)
.then(() => this.loadTimeline(1))
.catch((err) => toastErr(err, '删除失败'));
},
});
},
onTapTask(e) {
const i = e.currentTarget.dataset.index;
const t = this.data.tasks[i];
// 任务关联了记录类型就直接去记那一笔
if (t.sheet) {
wx.navigateTo({ url: '/pages/addrecord/addrecord?type=' + t.sheet });
return;
}
api
.toggleTask(t.id)
.then((res) => {
const tasks = this.data.tasks.slice();
tasks[i] = Object.assign({}, tasks[i], { done: res.done });
this.setData({ tasks });
})
.catch((err) => toastErr(err, '操作失败'));
},
completeTasks() {
const id = store.currentPetId();
if (!id) return;
api
.completeAllTasks(id)
.then((tasks) => this.setData({ tasks: (tasks || []).map(mapTask) }))
.catch((e) => toastErr(e, '操作失败'));
},
goPlan() {
wx.navigateTo({ url: '/pages/plan/plan' });
},
goExpense() {
wx.navigateTo({ url: '/pages/expense/expense' });
},
goRecord() {
wx.navigateTo({ url: '/pages/record/record' });
},
onQuickRecord() {
wx.navigateTo({ url: '/pages/record/record' });
},
openSheet(e) {
this.setData({ sheetType: e.currentTarget.dataset.type, sheetShow: true });
},
closeSheet() {
this.setData({ sheetShow: false });
},
onSheetSave() {
this.loadTasks();
this.loadTimeline(1);
},
onAddPet() {
this.setData({ sheetType: 'addPet', sheetShow: true });
},
onHeroTap() {
this.setData({ sheetType: this.data.pet && this.data.pet.id ? 'editPet' : 'addPet', sheetShow: true });
},
onShareAppMessage() {
return { title: '肉垫计划 · 每天照顾好毛孩子', path: '/pages/home/home' };
},
onShareTimeline() {
return { title: '肉垫计划 · 每天照顾好毛孩子' };
},
});