3fc3cbc7d2
## 通用简易表单
洗护/清洁那 15 种只需要「什么时候 + 备注 + 可选照片」。弹层现在是 28 个
wx:elif 手写分支(402 行),各写一个会推到 700 行,而且每加一种类型都要发版。
加一个 wx:elif="{{isSimple}}",isSimple 由 JS 按后端返回的 form 字段算,
和上面那 9 种永不重叠。插在 photo 之后、提醒中心之前,没动任何现有分支的顺序。
备注给了逐类型的具体占位提示(「用了什么沐浴露、有没有吹干」),
比「请输入备注」有用得多——用户看到提示才知道这栏该写什么。
## 一个不写就会静默出错的地方
dayToISO():后端用 time.Parse(time.RFC3339) 解 occurred_at,只给日期
("2026-07-30")它解不出来会**静默回退成 time.Now()**——用户选「昨天洗澡」
会存成今天,而且不报错。
时区必须带真实偏移:转成 UTC 的 Z 形式,落库再转回本地时会把边界日期挪一天
(首页任务重复生成那个 bug 就是这么来的)。取正午同样是为了远离日界。
## 四处写死的类型列表全删
RECORD_ITEMS record.js 记录页 9 宫格
QUICK_ITEMS home.js 首页 8 宫格
TYPE_TONE record.js 时间轴色调(9 种写死,新类型会全掉到橙色一片分不清)
TASK_SHEETS 弹层 任务关联记录类型的下拉(只有 6 种,加了类型选不到)
全部改成读 utils/recordTypes.js 这份共用缓存。缓存 load() 只真正请求一次,
并发调用共用同一个 promise;isSimple/label/groups 是同步的,弹层打开时
不该再等一次网络。
首页 8 宫格改成取后端前 8 个(按分组顺序再按 sort),后台调 sort 就能换首屏
露出哪几个;全部 24 种在记录页。
分组色系(daily橙/health绿/care紫/clean蓝)留在前端,后端只存 group key——
改配色不用动数据。
## 联调(预生产库实跑)
15 种新类型 全部落库,且选「昨天」没被存成今天(15/15)
原有 9 种 逐一回归,写库 emoji 原样;weight 回写宠物档案 → 4.5kg ✓
时间轴 24 条混排,色调分布 6/7/5/6,没有一条查不到类型
体重趋势 仍只有 1 个点(没被 24 条记录污染)
周报/首页汇总 正常
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
278 lines
9.1 KiB
JavaScript
278 lines
9.1 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');
|
||
|
||
function pad(n) {
|
||
return n < 10 ? '0' + n : '' + n;
|
||
}
|
||
function fmtTime(iso) {
|
||
if (!iso) return '';
|
||
const d = new Date(iso);
|
||
const now = new Date();
|
||
const hm = pad(d.getHours()) + ':' + pad(d.getMinutes());
|
||
if (d.toDateString() === now.toDateString()) return '今天 ' + hm;
|
||
const y = new Date(now.getTime() - 86400000);
|
||
if (d.toDateString() === y.toDateString()) return '昨天 ' + hm;
|
||
return d.getMonth() + 1 + '月' + d.getDate() + '日';
|
||
}
|
||
// 记录类型 → 配色。和首页快速记录入口用同一套分组:
|
||
// 体重/疫苗=橙、便便/饮食=绿、异常/用药=蓝、消费/照片=紫
|
||
// 时间轴每条记录的色调跟着它所属的分组走,和「记一笔」的宫格是同一套色。
|
||
// 原来这里是 9 种写死的映射,新类型会全掉到 tone-1,一片橙色分不出类别
|
||
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: r.type || r.icon || '',
|
||
tone: toneOf(r.type),
|
||
title: r.title,
|
||
desc: fmtTime(r.occurred_at) + (r.description ? '|' + r.description : ''),
|
||
image: r.image_url || '',
|
||
};
|
||
}
|
||
|
||
// 时间轴类型筛选
|
||
const REC_FILTERS = [
|
||
{ key: '', label: '全部' },
|
||
{ key: 'weight', label: '体重' },
|
||
{ key: 'poop', label: '便便' },
|
||
{ key: 'food', label: '饮食' },
|
||
{ key: 'symptom', label: '异常' },
|
||
{ key: 'medicine', label: '用药' },
|
||
{ key: 'vaccine', 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;
|
||
|
||
Page({
|
||
data: {
|
||
pet: {},
|
||
timeline: [],
|
||
recPage: 1,
|
||
recTotal: 0,
|
||
recHasMore: false,
|
||
recFilters: REC_FILTERS,
|
||
recFilterIdx: 0,
|
||
typeGroups: [],
|
||
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 加载
|
||
this.loadRecords();
|
||
});
|
||
},
|
||
onUnload() {
|
||
if (this._unsub) this._unsub();
|
||
},
|
||
onShow() {
|
||
syncTabBar(this);
|
||
this.loadTypes();
|
||
store
|
||
.ready()
|
||
.then(() => {
|
||
this._inited = true;
|
||
this.setData({ pet: store.getPet() });
|
||
this.loadRecords();
|
||
})
|
||
.catch((e) => wx.showToast({ title: e.message || '加载失败', icon: 'none' }));
|
||
},
|
||
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());
|
||
},
|
||
// 长按删除一条记录
|
||
onDeleteRecord(e) {
|
||
const r = this.data.timeline[e.currentTarget.dataset.index];
|
||
if (!r || !r.id) return;
|
||
wx.showModal({
|
||
title: '删除记录',
|
||
content: '确定删除「' + r.title + '」?删除后不可恢复。',
|
||
confirmColor: '#D9534F',
|
||
success: (res) => {
|
||
if (!res.confirm) return;
|
||
api
|
||
.deleteRecord(r.id)
|
||
.then(() => {
|
||
wx.showToast({ title: '已删除', icon: 'success' });
|
||
this.loadRecords();
|
||
})
|
||
.catch((err) => wx.showToast({ title: err.message || '删除失败', icon: 'none' }));
|
||
},
|
||
});
|
||
},
|
||
// 健康洞察:把散落的记录变成结论。点一条直接跳到对应的记录入口
|
||
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);
|
||
},
|
||
// 类型只在缓存没热时真正请求;弹层也读这份缓存判断走哪种表单,
|
||
// 所以这一步必须在用户能点开弹层之前完成
|
||
loadTypes() {
|
||
if (this.data.typeGroups.length) return;
|
||
recordTypes
|
||
.load()
|
||
.then((groups) => this.setData({ typeGroups: groups }))
|
||
.catch(() => {});
|
||
},
|
||
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() {
|
||
this.openSheetType('addPet');
|
||
},
|
||
onFab() {
|
||
wx.navigateTo({ url: '/pages/ai/ai' });
|
||
},
|
||
onShareAppMessage() {
|
||
return { title: '用肉垫计划记录毛孩子的成长', path: '/pages/record/record' };
|
||
},
|
||
onShareTimeline() {
|
||
return { title: '用肉垫计划记录毛孩子的成长' };
|
||
},
|
||
});
|