feat: 添加记录独立成页,表单字段由后端配置驱动

## 弹层里塞 24 个表单是走不通的
弹层高度受 .sheet-scroll 的 78vh 限制,字段一多就变成内滚;24 项的选择器
挤在弹层里更难看(用户原话「很丑」)。整体搬成页面。

## pages/addrecord —— 一个页面吃掉全部 24 种
形态:记录宠物 / 记录时间 / 类型专属字段 / 描述 + 照片 / 底部固定保存键。

关键在「类型专属字段」整段是后端驱动的:前端不认识「体重」「金额」这些业务词,
只认识 number / options / text 三种渲染方式,以及每个字段的值该落到哪儿。

record_types 加了 fields 列,后台用紧凑写法配(一行一个):

  number:体重:kg                    数值 → num_value
  options:状态:正常|软便|拉稀          单选 → category
  options:症状:呕吐|拉稀|精神差:-      末尾 - 表示不落 category,只进标题
  text:吃了什么                      文本 → 只进标题
  (空)                            只要 时间+描述+照片

解析放服务端不放前端:配错了要在后台保存时就报出来(第几行、错在哪),
不能等用户点开表单才发现渲染不出东西。读取时解析失败只让这一种事项没有额外
字段并打 warn,不让一条烂配置把整个记录页打空。

## 那个 - 开关不是过度设计
异常观察有两个单选(症状 + 严重程度),而 category 只有一个坑,
周报的高风险数按 category='高' 统计(report.go:46)——必须能指定谁占这个坑。
同理账单按 cost 的 category 分组聚合(report.go:134),
食便相关性排除 poop 的 category='正常'。这三处口径都得严丝合缝对上。

## 记录页瘦成纯列表
只留分组宫格。健康洞察 / 体重趋势 / 健康时间轴整页搬到 pages/history,
从报告页「记录与趋势」进——报告页才是看数据的地方,而一个「我要记一笔」的
页面上摆三块只读图表,用户每次都得先滚过去才能找到要点的东西。

**没有直接删掉那三块**:时间轴是唯一能看和删历史记录的地方,报告页只有
周报/账单这些聚合。删了用户就再也看不到自己记过什么。搬页面用的是 git 里
改动前的完整版复制,比往 report.js 里合并代码安全。

## 弹层瘦了一圈
9 个记录分支删掉,连带 17 个方法、SIMPLE_HINT、buildRecord 的 7 个 case
和一批只有它们在用的 data 字段。
  js   1089 → 857 行
  wxml  480 → 335 行
弹层现在只管「不是记一笔」的事:提醒、海报、档案、发帖、评论、导出、反馈。
buildRecord 只剩 vetSummary 一个分支(它写一条 note 留痕,不是用户填的表单)。

## 验证(预生产库实跑)
24 种的字段配置逐个核对解析结果,然后照 addrecord 的 buildBody 组装落库:
  体重  title='体重:4.6kg'        num=4.6            → 宠物档案回写成 4.6kg ✓
  记账  title='记账:128元 医疗'     num=128 cat=医疗    → 账单 total=128 分类[(医疗,128)] ✓
  异常  title='异常:呕吐 高'        cat=高             → 周报 high_risk_count=1 ✓
  排便  cat=软便                                    食便相关性口径保住
  饮食  title='饮食:幼猫粮 45g 偏少' cat=偏少
  喝水  num=180(新类型带数值,老代码没有它的分支也不影响)
  洗澡  title='洗澡'(无额外字段)
  看病  num=320(两个字段:文本+数值)
体重趋势 1 个点、值 4.6,没被其他 7 条污染。

回填踩过一次坑:fields 列是服务启动时 AutoMigrate 才建的,我在启动前就跑了
回填,Update 报错被忽略、13 行静默没写进去。第二版加了 HasColumn 前置检查
和逐行错误统计才发现。

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Blizzard
2026-07-30 10:25:42 +08:00
parent 29d2cd3faf
commit 3ea50efb7d
23 changed files with 906 additions and 777 deletions
+44 -29
View File
@@ -38,45 +38,60 @@ func seedRecordTypes(db *gorm.DB) error {
// 代码分支(体重回写档案、食便相关性、消费统计、疫苗完成度…),并且被
// care_template_items.sheet_type 引用,后台不许删,只能停用。
// form=full 表示 bottom-sheet 里有它专用的 wx:elif 分支。
full := func(code, label, icon, group string, sort int) model.RecordType {
// fields 是表单额外字段的配置,一行一个(写法见 model.RecordType.Fields 的注释)。
// 空 = 只要「时间 + 描述 + 照片」。
//
// 原有 9 种的配置必须和现在的统计口径严丝合缝:
// weight number → num_valueCreateRecord 靠它回写宠物档案
// cost 金额→num_value + 类别→category,账单是按 category 分组聚合的
// poop 状态→category,食便相关性分析排除 category='正常'
// symptom 两个单选:症状不占 category,严重程度占——周报按 category='高' 数高风险
mk := func(code, label, icon, group string, sort int, locked bool, fields string) model.RecordType {
form := model.RecordFormSimple
if locked {
form = model.RecordFormFull
}
return model.RecordType{Code: code, Label: label, Icon: icon, GroupKey: group,
Sort: sort, Enabled: true, Form: model.RecordFormFull, Locked: true}
Sort: sort, Enabled: true, Form: form, Locked: locked, Fields: fields}
}
simple := func(code, label, icon, group string, sort int) model.RecordType {
return model.RecordType{Code: code, Label: label, Icon: icon, GroupKey: group,
Sort: sort, Enabled: true, Form: model.RecordFormSimple, Locked: false}
full := func(code, label, icon, group string, sort int, fields string) model.RecordType {
return mk(code, label, icon, group, sort, true, fields)
}
simple := func(code, label, icon, group string, sort int, fields string) model.RecordType {
return mk(code, label, icon, group, sort, false, fields)
}
d, h, c, cl := model.RecordGroupDaily, model.RecordGroupHealth, model.RecordGroupCare, model.RecordGroupClean
types := []model.RecordType{
// 日常
full("weight", "体重", "weight", d, 10),
full("poop", "排便", "poop", d, 20),
full("food", "饮食", "food", d, 30),
simple("water", "喝水", "water", d, 40),
full("cost", "记账", "cost", d, 50),
full("photo", "照片", "photo", d, 60),
full("weight", "体重", "weight", d, 10, "number:体重:kg"),
full("poop", "排便", "poop", d, 20, "options:状态:正常|软便|拉稀"),
full("food", "饮食", "food", d, 30, "text:吃了什么\noptions:食欲:正常|偏少|不吃"),
simple("water", "喝水", "water", d, 40, "number:饮水量:ml"),
full("cost", "记账", "cost", d, 50, "number:金额:元\noptions:类别:医疗|食品|用品|美容|其他"),
full("photo", "照片", "photo", d, 60, ""),
// 健康
full("symptom", "异常", "symptom", h, 10),
full("vaccine", "疫苗", "vaccine", h, 20),
full("deworm", "驱虫", "deworm", h, 30),
simple("checkup", "体检", "checkup", h, 40),
simple("vet", "看病", "vet", h, 50),
full("medicine", "给药", "medicine", h, 60),
simple("supplement", "保健品", "supplement", h, 70),
full("symptom", "异常", "symptom", h, 10,
"options:发生了什么:呕吐|拉稀|精神差|皮肤红|其他:-\noptions:严重程度:低|中|高"),
full("vaccine", "疫苗", "vaccine", h, 20, "text:疫苗名称"),
full("deworm", "驱虫", "deworm", h, 30, "options:类型:体内|体外|体内外同驱"),
simple("checkup", "体检", "checkup", h, 40, "text:在哪做的"),
simple("vet", "看病", "vet", h, 50, "text:诊断\nnumber:花费:元"),
full("medicine", "给药", "medicine", h, 60, "text:药品名称\ntext:剂量"),
simple("supplement", "保健品", "supplement", h, 70, "text:名称\ntext:剂量"),
// 洗护
simple("bath", "洗澡", "bath", c, 10),
simple("nail", "剪指甲", "nail", c, 20),
simple("ear", "洗耳朵", "ear", c, 30),
simple("tooth", "刷牙", "tooth", c, 40),
simple("brush", "梳毛", "brush", c, 50),
simple("groom", "美容", "groom", c, 60),
simple("bath", "洗澡", "bath", c, 10, ""),
simple("nail", "剪指甲", "nail", c, 20, ""),
simple("ear", "洗耳朵", "ear", c, 30, ""),
simple("tooth", "刷牙", "tooth", c, 40, ""),
simple("brush", "梳毛", "brush", c, 50, ""),
simple("groom", "美容", "groom", c, 60, "number:花费:元"),
// 清洁
simple("litter", "换猫砂", "litter", cl, 10),
simple("litterbox", "洗猫砂盆", "litterbox", cl, 20),
simple("bowl", "洗食盆", "bowl", cl, 30),
simple("waterbowl", "洗水盆", "waterbowl", cl, 40),
simple("clean", "消毒", "clean", cl, 50),
simple("litter", "换猫砂", "litter", cl, 10, ""),
simple("litterbox", "洗猫砂盆", "litterbox", cl, 20, ""),
simple("bowl", "洗食盆", "bowl", cl, 30, ""),
simple("waterbowl", "洗水盆", "waterbowl", cl, 40, ""),
simple("clean", "消毒", "clean", cl, 50, ""),
}
return db.Create(&types).Error
}
+2
View File
@@ -291,6 +291,7 @@ type recordTypeReq struct {
Sort int `json:"sort"`
Enabled bool `json:"enabled"`
Form string `json:"form"`
Fields string `json:"fields_raw"`
}
// AdminListRecordTypes GET /api/admin/record-types 连停用的一起返回
@@ -308,6 +309,7 @@ func (h *Handler) AdminSaveRecordType(c *gin.Context) {
t := &model.RecordType{
Code: req.Code, Label: req.Label, Icon: req.Icon,
GroupKey: req.Group, Sort: req.Sort, Enabled: req.Enabled, Form: req.Form,
Fields: req.Fields,
}
t.ID = req.ID
if err := h.svc.SaveRecordType(t); err != nil {
+35
View File
@@ -44,4 +44,39 @@ type RecordType struct {
// Locked 有代码分支或模板引用依赖它,后台不许删。
// 硬拦在 service 层,不是靠前端隐藏按钮。
Locked bool `json:"locked"`
// Fields 表单字段配置,一行一个。后台用紧凑写法编辑:
//
// number:体重:kg 数值,落 num_value
// options:状态:正常|软便|拉稀 单选,落 category
// options:症状:呕吐|拉稀|精神差:- 单选,末尾 - 表示不落 category(只进标题)
// text:吃了什么 文本,只进标题
//
// 空 = 这个事项只要「时间 + 描述 + 照片」(洗澡、换猫砂这类)。
//
// 为什么要有「不落 category」这个开关:异常观察有两个单选(症状 + 严重程度),
// 而 category 只有一个坑,周报的高风险数按 category='高' 统计,
// 所以必须能指定是哪一个占这个坑。
Fields string `gorm:"type:text" json:"fields_raw"`
// ParsedFields 由 Fields 解析出来,前端直接照它渲染表单,不用自己写解析器
ParsedFields []RecordField `gorm:"-" json:"fields"`
}
// 字段类型
const (
FieldNumber = "number"
FieldOptions = "options"
FieldText = "text"
)
// RecordField 一个表单字段
type RecordField struct {
Kind string `json:"kind"`
Label string `json:"label"`
Unit string `json:"unit,omitempty"`
Options []string `json:"options,omitempty"`
// ToCategory 这个字段的值是否落到 health_records.category。
// 同一个事项里最多一个字段能占它
ToCategory bool `json:"to_category"`
}
+77
View File
@@ -4,6 +4,7 @@ import (
"errors"
"log"
"strconv"
"strings"
"gorm.io/gorm"
@@ -28,10 +29,80 @@ type RecordTypeGroup struct {
Items []model.RecordType `json:"items"`
}
// parseFields 把后台那份紧凑写法解析成结构。
// 解析放服务端而不是前端:写错了要在后台保存时就报出来,
// 不能等用户点开表单才发现渲染不出东西
func parseFields(raw string) ([]model.RecordField, error) {
out := []model.RecordField{}
catTaken := false
for i, line := range strings.Split(raw, "\n") {
line = strings.TrimSpace(line)
if line == "" {
continue
}
seg := strings.Split(line, ":")
no := strconv.Itoa(i + 1)
if len(seg) < 2 {
return nil, errors.New("第 " + no + " 行格式不对,至少要 类型:名称")
}
f := model.RecordField{Kind: strings.TrimSpace(seg[0]), Label: strings.TrimSpace(seg[1])}
if f.Label == "" {
return nil, errors.New("第 " + no + " 行缺名称")
}
extra := ""
if len(seg) > 2 {
extra = strings.TrimSpace(seg[2])
}
switch f.Kind {
case model.FieldNumber:
f.Unit = extra
// 数值一律落 num_value,不占 category
case model.FieldOptions:
for _, o := range strings.Split(extra, "|") {
if o = strings.TrimSpace(o); o != "" {
f.Options = append(f.Options, o)
}
}
if len(f.Options) == 0 {
return nil, errors.New("第 " + no + " 行 options 没有选项,用 | 分隔")
}
// 末段是 - 表示放弃 category
f.ToCategory = !(len(seg) > 3 && strings.TrimSpace(seg[3]) == "-")
case model.FieldText:
// 文本只进标题
default:
return nil, errors.New("第 " + no + " 行类型 " + f.Kind + " 不认识,只能是 number / options / text")
}
if f.ToCategory {
if catTaken {
return nil, errors.New("第 " + no + " 行:一个事项里只能有一个字段落 category" +
"多出来的那个末尾加 :- ")
}
catTaken = true
}
out = append(out, f)
}
return out, nil
}
// fillFields 读取时把解析结果挂上。解析失败不让整个接口挂掉——
// 那会让一条配错的数据把整个记录页打空;只让这一种事项没有额外字段
func fillFields(list []model.RecordType) {
for i := range list {
f, err := parseFields(list[i].Fields)
if err != nil {
log.Printf("[warn] 记录类型 %s 的字段配置解析失败,按无额外字段处理:%v", list[i].Code, err)
f = []model.RecordField{}
}
list[i].ParsedFields = f
}
}
// ListRecordTypes 小程序用:只返回启用的,按分组归好
func (s *Service) ListRecordTypes() []RecordTypeGroup {
var all []model.RecordType
s.db.Where("enabled = ?", true).Order("sort asc, id asc").Find(&all)
fillFields(all)
if len(all) == 0 {
// 表被清空了。这里不硬编码兜底一份——那等于把「可配置」又变回代码里的常量,
// 下次改配置的人会发现改了没用。宁可返回空让记录页明显是空的,也别静默用旧值。
@@ -57,6 +128,7 @@ func (s *Service) ListRecordTypes() []RecordTypeGroup {
func (s *Service) AdminListRecordTypes() []model.RecordType {
var all []model.RecordType
s.db.Order("group_key asc, sort asc, id asc").Find(&all)
fillFields(all)
if all == nil {
return []model.RecordType{}
}
@@ -75,6 +147,10 @@ func (s *Service) SaveRecordType(in *model.RecordType) error {
if in.GroupKey == "" {
in.GroupKey = model.RecordGroupDaily
}
// 字段配置写错了必须在这里挡住,不能存进库等用户点开表单才暴露
if _, err := parseFields(in.Fields); err != nil {
return err
}
if in.ID == "" {
// 新增:Code 不能和已有的撞
@@ -105,6 +181,7 @@ func (s *Service) SaveRecordType(in *model.RecordType) error {
return s.db.Model(&model.RecordType{}).Where("id = ?", in.ID).Updates(map[string]any{
"code": in.Code, "label": in.Label, "icon": in.Icon,
"group_key": in.GroupKey, "sort": in.Sort, "enabled": in.Enabled,
"fields": in.Fields,
}).Error
}