refactor(fe): 报告页重构成数据看板,历史页降级成「全部记录」

## 之前报告页的问题
它是个顶级 tab,却只有四张薄卡,而真正的分析全被埋起来:
  - 养宠账单里「记一笔」开 cost 弹层 —— 那分支上一轮随记录表单搬走了,
    点了开空弹层,是个断按钮
  - 养宠账单是我新建花销页的一个残缺子集,重复
  - 后端有 6 条洞察 + 体重曲线,全在「历史页」,报告页只用一行链过去
  - 健康摘要四行手拼字符串,很薄

## 报告 = 数据看板
把埋在历史页的分析提上来,报告页成为真正「看数据」的地方:
  本周概览   周报小结 + 完成/体重变化/高风险三个数 + 生成分享卡片
  健康洞察   6 条洞察直接铺在报告页(按 alert>warn>info 上色),
             点一条跳去记那一类。这是看板的主角
  体重趋势   把那张 SVG 曲线图提上来(点圆点看每次记录)
  健康摘要   疫苗/驱虫/体重/异常四行 + 导出就医摘要
  养宠花销   只给「本月 ¥X + top 类别 + 分类条」概要,点整卡进花销页,
             不再内嵌半套花销 UI,断掉的「记一笔」删了
  全部记录   一行入口进历史页

## 历史页降级成「全部记录」
洞察和体重曲线搬走了,历史页只留「时间轴流水 + 类型筛选」——
分工清晰:报告=看分析,全部记录=翻流水。nav 标题改成「全部记录」,
去掉不再用的 bottom-sheet / fab / seg-tabs 之外的东西。

体重曲线的「记一笔」原来开 weight 弹层(已删),改成跳
addrecord?type=weight;洞察点击也从开弹层改成跳 addrecord。

## 顺手
报告页去掉了 Pro 会员占位卡(支付未接入,就是个营销占位),
接入支付时再加回来。

## 验证(预生产库实跑)
  周报         需要关注 / 完成0 / 高风险1,小结文案对
  洞察         1 条(食便相关),带 level=warn + action=food + 依据
  体重趋势     字段验证只有 1 个点 → 曲线走空态「先记录几次」
  健康摘要     疫苗 0/3 即将到期、驱虫下次日期、体重稳定增长、异常 1 次
  账单         本月 ¥188、top 医疗、2 个类别
  5 个接口全部返回真数据

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Blizzard
2026-07-30 15:03:47 +08:00
parent 6a132d13bd
commit 7abcc0dd2d
8 changed files with 283 additions and 292 deletions
+109 -13
View File
@@ -2,20 +2,64 @@ const store = require('../../utils/store.js');
const api = require('../../utils/api.js');
const { syncTabBar } = require('../../utils/tabbar.js');
function fmtDay(iso) {
if (!iso) return '';
const d = new Date(iso);
return d.getMonth() + 1 + '月' + d.getDate() + '日';
}
// 体重点 → 趋势图。返回 { svg(折线 data-uri), dots(可点圆点,坐标为百分比) }。
// 从历史页搬过来的:报告页才是「看数据」的地方,图不该埋在二级页
function buildChart(points) {
const vals = points.map((p) => Number(p.value) || 0);
const W = 300, H = 100, pad = 12, n = vals.length;
let min = Math.min.apply(null, vals);
let max = Math.max.apply(null, vals);
if (max === min) max = min + 1;
const xs = (i) => (n === 1 ? W / 2 : pad + ((W - 2 * pad) * i) / (n - 1));
const ys = (v) => H - pad - ((H - 2 * pad) * (v - min)) / (max - min);
const pts = vals.map((v, i) => xs(i).toFixed(1) + ',' + ys(v).toFixed(1)).join(' ');
const svg =
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ' + W + ' ' + H + '" preserveAspectRatio="none">' +
'<polyline points="' + pts + '" fill="none" stroke="#73BE9D" stroke-width="4" stroke-linecap="round" stroke-linejoin="round"/>' +
'</svg>';
const dots = points.map((p, i) => ({
xp: Number(((xs(i) / W) * 100).toFixed(2)),
yp: Number(((ys(vals[i]) / H) * 100).toFixed(2)),
value: vals[i],
date: fmtDay(p.occurred_at),
note: p.note || '',
delta: i > 0 ? Number((vals[i] - vals[i - 1]).toFixed(2)) : null,
}));
return { svg: 'data:image/svg+xml,' + encodeURIComponent(svg), dots };
}
// 洞察等级 → 图标。alert/warn 用警告号,info 用趋势
function mapInsight(i) {
return { ...i, icon: i.level === 'info' ? 'trend' : 'warn' };
}
Page({
data: {
pet: {},
report: null,
gainText: '+0.0',
bill: null,
insights: [],
trendSvg: '',
trendStats: null,
trendDots: [],
selDot: null,
selIdx: -1,
summary: null,
bill: null,
billTop: null,
sheetShow: false,
sheetType: '',
},
onLoad() {
this._unsub = store.subscribe((pet) => {
this.setData({ pet });
if (!this._inited) return; // 首次由 onShow 加载
if (!this._inited) return;
this.loadData();
});
},
@@ -36,18 +80,74 @@ Page({
loadData() {
const id = store.currentPetId();
if (!id) return;
Promise.all([api.weeklyReport(id), api.getBill(id, 'month'), api.healthSummary(id)])
.then(([report, bill, summary]) => {
const g = report.weight_gain || 0;
api
.weeklyReport(id)
.then((report) => {
const g = (report && report.weight_gain) || 0;
this.setData({ report, gainText: (g >= 0 ? '+' : '') + g.toFixed(1) });
})
.catch(() => {});
api
.petInsights(id)
.then((list) => this.setData({ insights: (list || []).map(mapInsight) }))
.catch(() => this.setData({ insights: [] }));
api
.healthSummary(id)
.then((summary) => this.setData({ summary }))
.catch(() => {});
// 账单只在报告页取一个概要,详细录入和流水在花销页
api
.getBill(id, 'month')
.then((bill) => {
const cats = (bill && bill.categories) || [];
this.setData({ bill, billTop: cats.length ? cats[0] : null });
})
.catch(() => {});
this.loadTrend(id);
},
loadTrend(id) {
api
.weightTrend(id)
.then((points) => {
points = points || [];
if (points.length < 2) {
this.setData({ trendSvg: '', trendStats: null, trendDots: [], selDot: null, selIdx: -1 });
return;
}
const vals = points.map((p) => Number(p.value) || 0);
const latest = vals[vals.length - 1];
const delta = Number((latest - vals[0]).toFixed(2));
const chart = buildChart(points);
const last = chart.dots.length - 1;
this.setData({
report,
gainText: (g >= 0 ? '+' : '') + g.toFixed(1),
bill,
summary,
trendSvg: chart.svg,
trendDots: chart.dots,
selDot: chart.dots[last], // 默认选中最新一次
selIdx: last,
trendStats: { latest, delta, count: points.length },
});
})
.catch(() => {});
},
onTapPoint(e) {
const i = e.currentTarget.dataset.index;
this.setData({ selDot: this.data.trendDots[i], selIdx: i });
},
// 点一条洞察,跳到对应类型的记录入口(洞察带 action = 记录类型 code
onTapInsight(e) {
const it = this.data.insights[e.currentTarget.dataset.index];
if (it && it.action) wx.navigateTo({ url: '/pages/addrecord/addrecord?type=' + it.action });
},
// 体重趋势的「记一笔」直接去体重记录页(原来开的 weight 弹层已经搬成独立页)
goWeight() {
wx.navigateTo({ url: '/pages/addrecord/addrecord?type=weight' });
},
goExpense() {
wx.navigateTo({ url: '/pages/expense/expense' });
},
goHistory() {
wx.navigateTo({ url: '/pages/history/history' });
},
openSheet(e) {
this.setData({ sheetType: e.currentTarget.dataset.type, sheetShow: true });
},
@@ -57,11 +157,7 @@ Page({
onAddPet() {
wx.navigateTo({ url: '/pages/petform/petform' });
},
goHistory() {
wx.navigateTo({ url: '/pages/history/history' });
},
onShareAppMessage() {
// 带上宠物名,转发出去别人一眼知道是谁家的
const n = (this.data.pet && this.data.pet.name) || '我家毛孩子';
return { title: `${n} 的成长报告`, path: '/pages/report/report' };
},