Files
sundynix-pets/pets-fe/pages/plan/plan.js
T
Blizzard cecc7f4cbb feat(fe): Phase B —— 计划并进首页,日期条能展开成整月
## 日期条 → 可展开日历
首页那条七天日期带下面加了「展开整月」。展开后是整月网格(有安排的
日子带绿点),点任意一天,任务卡跟着切到那天。收起来还是七天,默认
不拉日历接口。

这块原来是计划页的「日历」tab,现在从计划页整个摘掉了——「哪天要做
什么」和「今天要做什么」本来就是一件事,分在两个 tab 里只是让人多点
两下。日历样式(.cal-* / .day-detail / .dd-*)跟着从 plan.wxss 搬到
home.wxss,plan.wxss 里没留死规则。

## 任务卡下面挂当天的计划节点和提醒
day-plan 接口本来就返回 plan_nodes / reminders / tasks 三份。这里只取
前两份,任务列表仍然走 /tasks——因为 /tasks 会补生成当天任务而
day-plan 不会,拿它的 tasks 覆盖列表会让今天的任务凭空少掉。

## 「接下来」卡
30 天计划里最近 3 个还没做的节点,日期显示成「今天/明天/3 天后」而不是
7月31日。带完成度进度条,右上角「完整计划」进计划页。

计划页降级成二级页之后,这张卡是它在首页的唯一露出;没有它,计划这个
功能对用户来说就等于消失了。

## AI 计划入口
AI 页快捷区加了「生成养护计划」,直达 /pages/plan/plan?tab=aiPlan。

没有把这个表单搬进 AI 聊天页:它要渲染 extracted 四宫格和可勾选的任务
节点,塞进聊天流意味着同一个页面维护两套完全不同的渲染,收益只是少跳
一次。原本要解决的是「计划页降级后 AI 计划找不到了」,一个入口就够了。

顺带:iconfont 补了 up / down 两个 chevron(展开收起用)。

联调验证(本地 9090,新建幼犬档案):
  day-plan 今天    → plan_nodes=['确认免疫与驱虫计划']
  plan            → completion_pct=0,4 个节点,date 字段齐全
  接下来筛出        → 07-29 确认免疫与驱虫计划 / 08-01 观察饮水与排便 / 08-05 体重趋势检查
  calendar        → 2026-7,today=29,tasked_days=[29]
  day-plan 未来日   → 08-01、08-05 各自返回对应节点

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-29 17:48:12 +08:00

125 lines
3.4 KiB
JavaScript

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',
planTabs: [
{ key: 'timeline', label: '路线图' },
{ key: 'aiPlan', label: 'AI 计划' },
],
plan: null,
aiInput: '',
aiPlan: null,
aiBusy: false,
sheetShow: false,
sheetType: '',
},
onLoad(options) {
if (options && options.tab === 'aiPlan') this.setData({ planView: 'aiPlan' });
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 }));
},
// 来自 seg-tabs 的 change 事件,key 即 planView
switchPlanView(e) {
this.setData({ planView: e.detail.key });
},
// 计划明细勾选
toggleTimelineTask(e) {
const id = e.currentTarget.dataset.id;
api.togglePlanTask(id).then(() => this.loadTimeline()).catch((e) => toastErr(e, '操作失败'));
},
// AI 计划
onAiInput(e) {
this.setData({ aiInput: e.detail.value });
},
genAIPlan() {
const id = store.currentPetId();
const input = (this.data.aiInput || '').trim();
if (!id || !input) {
wx.showToast({ title: '先描述一下情况', icon: 'none' });
return;
}
this.setData({ aiBusy: true });
wx.showLoading({ title: 'AI 生成中...' });
api
.createAIPlan(id, input)
.then((plan) => {
wx.hideLoading();
this.setData({ aiPlan: plan, aiBusy: false });
})
.catch((err) => {
wx.hideLoading();
this.setData({ aiBusy: false });
wx.showToast({ title: err.message || '生成失败', icon: 'none' });
});
},
applyAIPlan() {
const p = this.data.aiPlan;
if (!p) return;
api
.applyAIPlan(p.id)
.then(() => wx.showToast({ title: '已加入我的计划', icon: 'success' }))
.catch((err) => wx.showToast({ title: err.message || '加入失败', icon: 'none' }));
},
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 });
},
onFab() {
wx.navigateTo({ url: '/pages/ai/ai' });
},
onShareAppMessage() {
return { title: '我给毛孩子做了份养护计划', path: '/pages/plan/plan' };
},
onShareTimeline() {
return { title: '我给毛孩子做了份养护计划' };
},
});