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:
@@ -1,6 +1,7 @@
|
||||
const store = require('../../utils/store.js');
|
||||
const api = require('../../utils/api.js');
|
||||
const { toastErr } = require('../../utils/ui.js');
|
||||
const recordTypes = require('../../utils/recordTypes.js');
|
||||
|
||||
function pad(n) {
|
||||
return n < 10 ? '0' + n : '' + n;
|
||||
@@ -15,23 +16,15 @@ function fmtTime(iso) {
|
||||
if (d.toDateString() === y.toDateString()) return '昨天 ' + hm;
|
||||
return d.getMonth() + 1 + '月' + d.getDate() + '日';
|
||||
}
|
||||
// 记录类型 → 配色。和首页快速记录入口用同一套分组:
|
||||
// 体重/疫苗=橙、便便/饮食=绿、异常/用药=蓝、消费/照片=紫
|
||||
// 时间轴每条记录的色调。这页只读历史,不需要整份类型表,
|
||||
// 但仍要按分组着色——所以引一份轻量的 code→组 映射
|
||||
const recordTypes = require('../../utils/recordTypes.js');
|
||||
// 每条记录的分组配色。这页只读历史,引一份轻量的 code→组 映射就够
|
||||
function toneOf(type) {
|
||||
const t = recordTypes.get(type);
|
||||
return (t && t.tone) || 'tone-1';
|
||||
}
|
||||
|
||||
// 「记一笔」的入口不再写死在这里:类型和分组由后端 record_types 表提供
|
||||
// (后台可配),前端只负责渲染。utils/recordTypes.js 是两处共用的缓存。
|
||||
|
||||
function mapRecord(r) {
|
||||
return {
|
||||
id: r.id,
|
||||
// 图标按 type 取(9 值枚举,可靠);老数据 icon 字段里的 emoji 只作兜底
|
||||
// 图标按 type 取(可靠);老数据 icon 字段里的 emoji 只作兜底
|
||||
type: r.type || r.icon || '',
|
||||
tone: toneOf(r.type),
|
||||
title: r.title,
|
||||
@@ -40,57 +33,19 @@ function mapRecord(r) {
|
||||
};
|
||||
}
|
||||
|
||||
// 时间轴类型筛选
|
||||
// 时间轴类型筛选。常用的一组,不是全部 24 种——seg-tabs 放满会挤成一条难点的横条
|
||||
const REC_FILTERS = [
|
||||
{ key: '', label: '全部' },
|
||||
{ key: 'weight', label: '体重' },
|
||||
{ key: 'poop', label: '便便' },
|
||||
{ key: 'poop', label: '排便' },
|
||||
{ key: 'food', label: '饮食' },
|
||||
{ key: 'symptom', label: '异常' },
|
||||
{ key: 'medicine', label: '用药' },
|
||||
{ key: 'vaccine', label: '疫苗' },
|
||||
{ key: 'cost', label: '消费' },
|
||||
{ key: 'cost', label: '记账' },
|
||||
{ key: 'photo', label: '照片' },
|
||||
];
|
||||
|
||||
function fmtDay(iso) {
|
||||
if (!iso) return '';
|
||||
const d = new Date(iso);
|
||||
return d.getMonth() + 1 + '月' + d.getDate() + '日';
|
||||
}
|
||||
|
||||
// 用真实体重点生成趋势图:返回 { svg(折线), 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 用警告号,其余按类别给
|
||||
const INS_ICON = { alert: 'warn', warn: 'warn', info: 'trend' };
|
||||
function mapInsight(i) {
|
||||
return { ...i, icon: i.level === 'info' ? (INS_ICON.info) : INS_ICON.alert };
|
||||
}
|
||||
|
||||
const REC_PAGE = 6;
|
||||
const REC_PAGE = 8;
|
||||
|
||||
Page({
|
||||
data: {
|
||||
@@ -101,19 +56,11 @@ Page({
|
||||
recHasMore: false,
|
||||
recFilters: REC_FILTERS,
|
||||
recFilterIdx: 0,
|
||||
insights: [],
|
||||
trendSvg: '',
|
||||
trendStats: null,
|
||||
trendDots: [],
|
||||
selDot: null,
|
||||
selIdx: -1,
|
||||
sheetShow: false,
|
||||
sheetType: '',
|
||||
},
|
||||
onLoad() {
|
||||
this._unsub = store.subscribe((pet) => {
|
||||
this.setData({ pet });
|
||||
if (!this._inited) return; // 首次由 onShow 加载
|
||||
if (!this._inited) return;
|
||||
this.loadRecords();
|
||||
});
|
||||
},
|
||||
@@ -121,9 +68,9 @@ Page({
|
||||
if (this._unsub) this._unsub();
|
||||
},
|
||||
onShow() {
|
||||
// 时间轴的分组配色要读类型缓存。这页可能是从报告页直接进来的,
|
||||
// 缓存没经过首页/记录页热过,所以自己拉一次(load 内部只会真请求一次)
|
||||
recordTypes.load().then(() => this.setData({ timeline: this.data.timeline })).catch(() => {});
|
||||
// 分组配色要读类型缓存。这页可能直接从报告页进来,缓存没经过首页热过,
|
||||
// 自己拉一次(load 内部只真请求一次),拉完刷新已有列表的配色
|
||||
recordTypes.load().then(() => this.setData({ timeline: this.data.timeline.map((t) => ({ ...t, tone: toneOf(t.type) })) })).catch(() => {});
|
||||
store
|
||||
.ready()
|
||||
.then(() => {
|
||||
@@ -136,13 +83,37 @@ Page({
|
||||
curFilter() {
|
||||
return this.data.recFilters[this.data.recFilterIdx].key;
|
||||
},
|
||||
// 来自 seg-tabs 的 change 事件
|
||||
onFilterTap(e) {
|
||||
const i = Number(e.detail.index);
|
||||
if (i === this.data.recFilterIdx) return;
|
||||
this.setData({ recFilterIdx: i }, () => this.loadRecords());
|
||||
},
|
||||
// 长按删除一条记录
|
||||
loadRecords() {
|
||||
const id = store.currentPetId();
|
||||
if (!id) return;
|
||||
api
|
||||
.getRecords(id, { page: 1, pageSize: REC_PAGE, type: this.curFilter() })
|
||||
.then((res) => {
|
||||
const list = (res.list || []).map(mapRecord);
|
||||
this.setData({ timeline: list, recPage: 1, recTotal: res.total || 0, recHasMore: list.length < (res.total || 0) });
|
||||
})
|
||||
.catch((e) => toastErr(e));
|
||||
},
|
||||
loadMoreRecords() {
|
||||
const id = store.currentPetId();
|
||||
if (!id) return;
|
||||
const next = this.data.recPage + 1;
|
||||
api
|
||||
.getRecords(id, { page: next, pageSize: REC_PAGE, type: this.curFilter() })
|
||||
.then((res) => {
|
||||
const merged = this.data.timeline.concat((res.list || []).map(mapRecord));
|
||||
this.setData({ timeline: merged, recPage: next, recTotal: res.total || 0, recHasMore: merged.length < (res.total || 0) });
|
||||
})
|
||||
.catch(() => {});
|
||||
},
|
||||
collapseRecords() {
|
||||
this.loadRecords();
|
||||
},
|
||||
onDeleteRecord(e) {
|
||||
const r = this.data.timeline[e.currentTarget.dataset.index];
|
||||
if (!r || !r.id) return;
|
||||
@@ -162,96 +133,10 @@ Page({
|
||||
},
|
||||
});
|
||||
},
|
||||
// 健康洞察:把散落的记录变成结论。点一条直接跳到对应的记录入口
|
||||
loadInsights() {
|
||||
const id = store.currentPetId();
|
||||
if (!id) return this.setData({ insights: [] });
|
||||
api
|
||||
.petInsights(id)
|
||||
.then((list) => this.setData({ insights: (list || []).map(mapInsight) }))
|
||||
.catch(() => this.setData({ insights: [] }));
|
||||
},
|
||||
onTapInsight(e) {
|
||||
const it = this.data.insights[e.currentTarget.dataset.index];
|
||||
if (it && it.action) this.openSheetType(it.action);
|
||||
},
|
||||
loadRecords() {
|
||||
const id = store.currentPetId();
|
||||
if (!id) return;
|
||||
api
|
||||
.getRecords(id, { page: 1, pageSize: REC_PAGE, type: this.curFilter() })
|
||||
.then((res) => {
|
||||
const list = (res.list || []).map(mapRecord);
|
||||
this.setData({ timeline: list, recPage: 1, recTotal: res.total || 0, recHasMore: list.length < (res.total || 0) });
|
||||
})
|
||||
.catch((e) => toastErr(e));
|
||||
this.loadTrend();
|
||||
this.loadInsights();
|
||||
},
|
||||
loadMoreRecords() {
|
||||
const id = store.currentPetId();
|
||||
if (!id) return;
|
||||
const next = this.data.recPage + 1;
|
||||
api
|
||||
.getRecords(id, { page: next, pageSize: REC_PAGE, type: this.curFilter() })
|
||||
.then((res) => {
|
||||
const merged = this.data.timeline.concat((res.list || []).map(mapRecord));
|
||||
this.setData({ timeline: merged, recPage: next, recTotal: res.total || 0, recHasMore: merged.length < (res.total || 0) });
|
||||
})
|
||||
.catch(() => {});
|
||||
},
|
||||
collapseRecords() {
|
||||
this.loadRecords();
|
||||
},
|
||||
loadTrend() {
|
||||
const id = store.currentPetId();
|
||||
if (!id) {
|
||||
this.setData({ trendSvg: '', trendStats: null, trendDots: [], selDot: null, selIdx: -1 });
|
||||
return;
|
||||
}
|
||||
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 lastIdx = chart.dots.length - 1;
|
||||
this.setData({
|
||||
trendSvg: chart.svg,
|
||||
trendDots: chart.dots,
|
||||
selDot: chart.dots[lastIdx], // 默认选中最新一次
|
||||
selIdx: lastIdx,
|
||||
trendStats: { latest, delta, count: points.length },
|
||||
});
|
||||
})
|
||||
.catch(() => {});
|
||||
},
|
||||
onTapPoint(e) {
|
||||
const i = e.currentTarget.dataset.index;
|
||||
this.setData({ selDot: this.data.trendDots[i], selIdx: i });
|
||||
},
|
||||
previewImage(e) {
|
||||
const src = e.currentTarget.dataset.src;
|
||||
if (src) wx.previewImage({ urls: [src], current: src });
|
||||
},
|
||||
openSheet(e) {
|
||||
this.openSheetType(e.currentTarget.dataset.type);
|
||||
},
|
||||
openSheetType(type) {
|
||||
this.setData({ sheetType: type, sheetShow: true });
|
||||
},
|
||||
closeSheet() {
|
||||
this.setData({ sheetShow: false });
|
||||
},
|
||||
onSheetSaved() {
|
||||
this.loadRecords();
|
||||
},
|
||||
onAddPet() {
|
||||
wx.navigateTo({ url: '/pages/petform/petform' });
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user