Files
Blizzard 6fd4977f46 feat(fe): 建档挑方案、计划页套方案、阶段漂移提示
后端上一轮做完了但小程序上看不到,这次接上。

## 建档第四步:挑方案
方案按阶段取,所以必须排在「资料」之后(阶段是第三步定的)。卡片上按重复方式
分开报数(每天 3 项 / 每周 2 项 / 一次性 2 项),不报一个总数——4 条「每天」
套一整月就是 120 多条,只给总数用户会以为出 bug。

「先空着,我自己排」是个平等的选项,和方案卡长一样、就在下面一眼能看到,
不是藏起来的退路:已经养熟的宠物主人有自己的节奏,硬塞一套只会被删掉。

默认选第一套——多数用户是新手,给一个合理默认比让他面对空白好。

编辑档案时不出现第四步:改档案不该顺手改计划。

套方案失败不算建档失败:档案已经建好了,方案之后在计划页还能套,
不该把用户退回表单。

## 计划页:空月份和平时都能套
空月份的提示条带「套一套方案」按钮 —— 空着让用户从零排,多数人会直接退出去。
但「+ 加一件」始终在,不能只给套用。

浮层里写明「套用是往里加,不会覆盖你已经排好的」,因为服务端就是按
「日期+标题」去重的,这句话是真的。套完 toast 报排了几件;一件都没排
(全都已存在)时说「这个月已经有这些安排了」,不说「成功」。

## 阶段漂移提示
首页门面卡下面一张卡:「钞票已经 17个月,从幼年期进入成年期了」,
带上新阶段的划分依据、养护重点和体型说明,点进去到建档页改。

用主色不用红色 —— 这是「可以做一下」不是「出问题了」。
改完回首页 onShow 重拉一次,卡片自然消失。

不在首页直接改阶段:改完要不要套新方案是同一件事,放在建档页一起做才连贯。

## 顺手把合并的尾巴清了
manageTasks 那一支删了:它管的是 DailyTask,而今日任务已经并进计划节点,
增删改都在计划页。留着会出现「这里加的任务」和「计划页加的节点」进不同的表。

连带清掉 7 个死方法 + taskSheets + 一个死 require(简易表单那套早随记录表单
搬到 pages/addrecord 了)。首页现在一个弹层都不开,bottom-sheet 组件也去掉了。

弹层累计:js 1110 → 689 行,wxml 485 → 278 行。

## 验证(预生产库实跑)
  建档四步    6 个月柯基 → 幼年期 → 拉到「幼犬标准照护」→ 套用本月 7 条
              → 首页今日任务 4 条
  先空着      今日任务 0 条,疫苗驱虫提醒仍然建
  计划页套用  0 → 7 条;重复套用 0 条(去重生效)
  漂移检测    17 个月柯基存着幼年期 → drifted=true,给出依据/重点/体型
              改成成年期 → drifted=false
              没生日 → 不提示(算不出就不猜)
              「刚到家」→ 不提示(叠加层不是年龄段)

第一版验证脚本把阶段改成了和算出来一样的值,drifted=False 是对的,
但我的 print 无条件打了提示文案,看着像 bug。重测才验到真的。

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-30 12:59:27 +08:00

283 lines
8.4 KiB
JavaScript

const store = require('../../utils/store.js');
const api = require('../../utils/api.js');
const { toastErr } = require('../../utils/ui.js');
function pad2(n) {
return n < 10 ? '0' + n : '' + n;
}
function ymd(d) {
return d.getFullYear() + '-' + pad2(d.getMonth() + 1) + '-' + pad2(d.getDate());
}
// "2026-07-04" → "7月4日"
function fmtMD(date) {
const p = (date || '').split('-');
return p.length < 3 ? '' : +p[1] + '月' + +p[2] + '日';
}
function parseYMD(s) {
const p = (s || '').split('-');
return p.length < 3 ? null : new Date(+p[0], +p[1] - 1, +p[2]);
}
// 自然月网格。周一为一周起点,首尾补空格对齐星期。
// 按月切而不是「从计划开始日起 30 天」:那样窗口跨两个月,
// 用户看着一个 7月26日—8月25日 的日历,既不像本月也不像下月。
function buildCells(year, month, byDate, todayStr) {
const first = new Date(year, month - 1, 1);
const lead = (first.getDay() + 6) % 7;
const total = new Date(year, month, 0).getDate();
const cells = [];
for (let i = 0; i < lead; i++) cells.push({ blank: true });
for (let d = 1; d <= total; d++) {
const s = year + '-' + pad2(month) + '-' + pad2(d);
cells.push({ day: d, date: s, count: (byDate[s] || []).length, today: s === todayStr });
}
while (cells.length % 7) cells.push({ blank: true });
return cells;
}
// off=0 本月,off=1 下月
function monthOf(off) {
const n = new Date();
const d = new Date(n.getFullYear(), n.getMonth() + off, 1);
return { year: d.getFullYear(), month: d.getMonth() + 1 };
}
function monthEnd(off) {
const m = monthOf(off);
return m.year + '-' + pad2(m.month) + '-' + pad2(new Date(m.year, m.month, 0).getDate());
}
Page({
data: {
pet: {},
plan: null,
cells: [],
monthOff: 0,
monthLabel: '',
monthEndLabel: '',
nextCount: 0,
monthEmpty: false,
tplOptions: [],
tplShow: false,
pickStart: '',
pickEnd: '',
selDate: '',
selLabel: '',
dayTasks: [],
editShow: false,
editId: '',
form: { title: '', date: '', description: '', daily: false },
saving: false,
sheetShow: false,
sheetType: '',
},
onLoad() {
this._unsub = store.subscribe((pet) => {
this.setData({ pet });
if (this._inited) this.load();
});
},
onUnload() {
if (this._unsub) this._unsub();
},
onShow() {
store
.ready()
.then(() => {
this._inited = true;
this.setData({ pet: store.getPet() });
this.load();
})
.catch((e) => toastErr(e));
},
load() {
const id = store.currentPetId();
if (!id) return;
this.loadTemplates();
api
.getPlan(id)
.then((plan) => this.apply(plan))
.catch(() => this.setData({ plan: null, cells: [], dayTasks: [] }));
},
apply(plan) {
const tasks = plan.tasks || [];
const byDate = {};
tasks.forEach((t) => {
if (!t.date) return;
(byDate[t.date] = byDate[t.date] || []).push(t);
});
this._byDate = byDate;
this.setData({
plan: { ...plan, tasks },
// 能排的区间:不早于计划开始日,不晚于下月月底。和后端 dayOffset 的校验一致
pickStart: (plan.start_date || '').slice(0, 10),
pickEnd: monthEnd(1),
});
this.renderMonth(this.data.monthOff);
if (this._pendingSel) {
// 刚加/改完,跳到那一天,不然用户看不到东西去哪了
this.selectDay(this._pendingSel);
this._pendingSel = '';
}
},
renderMonth(off) {
const m = monthOf(off);
const todayStr = ymd(new Date());
const byDate = this._byDate || {};
const prefix = m.year + '-' + pad2(m.month);
this.setData({
monthOff: off,
monthLabel: m.year + ' 年 ' + m.month + ' 月',
monthEndLabel: fmtMD(monthEnd(off)),
cells: buildCells(m.year, m.month, byDate, todayStr),
nextCount: Object.keys(byDate).filter((d) => d.indexOf(monthOf(1).year + '-' + pad2(monthOf(1).month)) === 0)
.reduce((n, d) => n + byDate[d].length, 0),
// 当前看的这个月一条都没有 —— 空月份要给套方案的入口
monthEmpty: !Object.keys(byDate).some((d) => d.indexOf(prefix) === 0),
});
// 切到本月默认选今天;切到下月默认选 1 号(今天不在那个月里)
const inMonth = todayStr.indexOf(prefix) === 0;
this.selectDay(inMonth ? todayStr : prefix + '-01', todayStr);
},
// 方案按宠物当前阶段取。宠物换了(pet-switch)阶段也可能不同,所以跟着 load 走
loadTemplates() {
const pet = this.data.pet || {};
if (!pet.stage) return;
api
.careTemplates(pet.type === '狗狗' ? 'dog' : 'cat', pet.stage)
.then((opts) => this.setData({ tplOptions: opts || [] }))
.catch(() => this.setData({ tplOptions: [] }));
},
openTpl() {
this.setData({ tplShow: true });
},
closeTpl() {
this.setData({ tplShow: false });
},
applyTpl(e) {
const id = e.currentTarget.dataset.id;
const petID = store.currentPetId();
if (!petID || !id) return;
wx.showLoading({ title: '排进去…' });
api
.applyTemplate(petID, id, this.data.monthOff)
.then((res) => {
wx.hideLoading();
this.setData({ tplShow: false });
const r = res || {};
wx.showToast({
title: r.plans > 0 ? '排了 ' + r.plans + ' 件事' : '这个月已经有这些安排了',
icon: 'none',
});
this.load();
})
.catch((err) => {
wx.hideLoading();
toastErr(err, '套用失败');
});
},
switchMonth(e) {
this.renderMonth(Number(e.currentTarget.dataset.off));
},
selectDay(date, todayStr) {
const t = todayStr || ymd(new Date());
this.setData({
selDate: date,
selLabel: date === t ? '今天的安排' : fmtMD(date) + '的安排',
dayTasks: (this._byDate || {})[date] || [],
});
},
onTapDay(e) {
const d = e.currentTarget.dataset.date;
if (d) this.selectDay(d);
},
toggleTask(e) {
api
.togglePlanTask(e.currentTarget.dataset.id)
.then(() => this.load())
.catch((err) => toastErr(err, '操作失败'));
},
// ── 加 / 改 ──
openAdd() {
this.setData({
editShow: true, editId: '',
form: { title: '', date: this.data.selDate || ymd(new Date()), description: '', daily: false },
});
},
openEdit(e) {
const t = (this.data.dayTasks || []).find((x) => x.id === e.currentTarget.dataset.id);
if (!t) return;
this.setData({
editShow: true, editId: t.id,
form: { title: t.title, date: t.date || this.data.selDate, description: t.description || '', daily: false },
});
},
closeEdit() {
this.setData({ editShow: false });
},
noop() {},
onFormTitle(e) {
this.setData({ 'form.title': e.detail.value });
},
onFormDate(e) {
this.setData({ 'form.date': e.detail.value });
},
onFormDesc(e) {
this.setData({ 'form.description': e.detail.value });
},
onFormDaily(e) {
this.setData({ 'form.daily': e.detail.value });
},
onSubmit() {
if (this.data.saving) return;
const f = this.data.form;
if (!(f.title || '').trim()) return wx.showToast({ title: '写一下做什么', icon: 'none' });
const id = store.currentPetId();
if (!id) return;
this.setData({ saving: true });
const req = this.data.editId
? api.updatePlanTask(this.data.editId, f)
: api.addPlanTask(id, f);
req
.then(() => {
this.setData({ saving: false, editShow: false });
// 加完/改完要跳到那一天,不然用户改了日期却看不到东西去哪了
this._pendingSel = f.date;
this.load();
})
.catch((e) => {
this.setData({ saving: false });
toastErr(e, '保存失败');
});
},
onDelete(e) {
const { id, title } = e.currentTarget.dataset;
wx.showModal({
title: '删掉这件事?',
content: title || '',
confirmColor: '#EE7D73',
success: (r) => {
if (!r.confirm) return;
api
.deletePlanTask(id)
.then(() => this.load())
.catch((err) => toastErr(err, '删除失败'));
},
});
},
closeSheet() {
this.setData({ sheetShow: false });
},
onAddPet() {
wx.navigateTo({ url: '/pages/petform/petform' });
},
onShareAppMessage() {
return { title: '我给毛孩子做了份养护计划', path: '/pages/plan/plan' };
},
onShareTimeline() {
return { title: '我给毛孩子做了份养护计划' };
},
});