feat: 建档改成三步向导页,品种改成后台可配的选择器

## pages/petform 三步向导
  ① 名字 + 传头像
  ② 猫还是狗 → 选品种
  ③ 性别 / 生日 / 到家日期 / 体重 / 毛色 / 阶段

**编辑时不分步**,一屏全放开:用户是来改某一项的,不该被按顺序走一遍。
同一个页面两种形态,靠 ?id= 区分。

每一步只校验这一步的东西。三步全填完才校验的话,用户在第三步才被告知
第一步没填名字,还得翻回去。

进度条没用 seg-tabs:那是「可以随意切」的语义,这里有顺序,
点第三步跳过前两步会拿到一份没名字的档案。

## 生日和到家日期分开
原来是一个「生日 / 到家时间」字段。领养的成年猫狗生日常常是不知道的,
而「一起生活了多少天」要按到家日算——合成一个字段两件事都说不准。
验过:到家 4 个月前 → 第 121 天;按生日算会是 801 天。

模型里 Birthday 和 ArrivedAt 本来就是两个字段,只是表单没露出来。

## 品种:自由文本 → 后台可配的选择器
原来品种是个输入框,用户手打「柯基」「柯基犬」「威尔士柯基」算三个品种,
以后想按品种做体重基准、常见病提示这类事就没法做。

新增 breeds 表,预置 34 种猫 + 45 种狗,按首字母分组、右侧 A-Z 索引跳转、
首字母吸顶。留了「不确定」——领养的串串确实说不出品种,逼着选一个只会
得到假数据。换物种会清掉已选品种(原来那个一定不对了)。

**首字母存字段不运行时算**:Go 没有标准拼音库,而多音字自动转常出错——
「藏獒」按 cang 还是 zang 分组、「柴犬」的柴,人来定比库来猜靠谱。
后台新增品种时手填这一位,服务端校验必须是单个 A-Z。

已有档案在用的品种不许删(删了那些档案的品种就变成查不到的字符串),
想隐藏用 enabled=false。同物种下不许重名——品种是统计口径,重名会把
同一种猫劈成两半。

预置数据自己填错了两条,实跑时看出来的:「苏格兰柯利犬」我按「柯利」
填了 K,应该按「苏格兰」算 S;「双血统边牧」根本不是品种,是血统描述。
seed 源和库里都改了。

## 弹层继续瘦
addPet/editPet 两支删掉,连带 10 个方法、ageFromBirthday/GENDERS/STAGES
和 5 个 add* 字段。累计(从记录表单搬走那次算起):
  js   1110 → 734 行
  wxml  485 → 307 行

多宠管理里「点某只去编辑」原来是 setType('editPet'),那支没了会开一个
空白弹层——改成关弹层再跳页。8 个页面的入口全换完,grep 无残留。

## 验证(预生产库实跑)
  品种接口   猫 34 种 16 个首字母、狗 45 种 17 个首字母;非法 species 返回 []
  建档      生日 / 到家日期 分别落库,品种毛色阶段都对
  编辑      只改名字,到家日期没被冲掉
  修正      柯利犬 K→S、双血统边牧已删

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Blizzard
2026-07-30 11:26:51 +08:00
parent 2aaae2b917
commit aefb1bc103
23 changed files with 748 additions and 166 deletions
+106 -1
View File
@@ -20,7 +20,112 @@ func Seed(db *gorm.DB, cfg *config.Config) error {
if err := seedAdmin(db, cfg); err != nil {
return err
}
return seedRecordTypes(db)
if err := seedRecordTypes(db); err != nil {
return err
}
return seedBreeds(db)
}
// seedBreeds 品种预置。同 record_types:只在表完全为空时铺一次。
//
// 这也是「结构」不是「内容」——品种列表空着,建档第二步就选不了品种。
// 首字母存字段不运行时算:Go 没有标准拼音库,而多音字(藏獒、柴犬)
// 自动转常出错,人来定比库来猜靠谱。
func seedBreeds(db *gorm.DB) error {
var count int64
if err := db.Model(&model.Breed{}).Count(&count).Error; err != nil {
return err
}
if count > 0 {
return nil
}
breeds := []model.Breed{
// 猫 34 种
{Species: model.SpeciesCat, Initial: "A", Name: "阿比西尼亚猫", Sort: 10},
{Species: model.SpeciesCat, Initial: "A", Name: "奥西猫", Sort: 20},
{Species: model.SpeciesCat, Initial: "B", Name: "巴厘猫", Sort: 30},
{Species: model.SpeciesCat, Initial: "B", Name: "波斯猫", Sort: 40},
{Species: model.SpeciesCat, Initial: "B", Name: "布偶猫", Sort: 50},
{Species: model.SpeciesCat, Initial: "B", Name: "伯曼猫", Sort: 60},
{Species: model.SpeciesCat, Initial: "C", Name: "长毛折耳猫", Sort: 70},
{Species: model.SpeciesCat, Initial: "D", Name: "德文卷毛猫", Sort: 80},
{Species: model.SpeciesCat, Initial: "D", Name: "东方短毛猫", Sort: 90},
{Species: model.SpeciesCat, Initial: "D", Name: "东奇尼猫", Sort: 100},
{Species: model.SpeciesCat, Initial: "E", Name: "俄罗斯蓝猫", Sort: 110},
{Species: model.SpeciesCat, Initial: "H", Name: "哈瓦那棕猫", Sort: 120},
{Species: model.SpeciesCat, Initial: "K", Name: "科拉特猫", Sort: 130},
{Species: model.SpeciesCat, Initial: "L", Name: "拉波猫", Sort: 140},
{Species: model.SpeciesCat, Initial: "M", Name: "缅因猫", Sort: 150},
{Species: model.SpeciesCat, Initial: "M", Name: "美国短毛猫", Sort: 160},
{Species: model.SpeciesCat, Initial: "M", Name: "美国卷耳猫", Sort: 170},
{Species: model.SpeciesCat, Initial: "M", Name: "曼赤肯猫", Sort: 180},
{Species: model.SpeciesCat, Initial: "M", Name: "孟买猫", Sort: 190},
{Species: model.SpeciesCat, Initial: "M", Name: "孟加拉豹猫", Sort: 200},
{Species: model.SpeciesCat, Initial: "N", Name: "挪威森林猫", Sort: 210},
{Species: model.SpeciesCat, Initial: "R", Name: "日本短尾猫", Sort: 220},
{Species: model.SpeciesCat, Initial: "S", Name: "苏格兰折耳猫", Sort: 230},
{Species: model.SpeciesCat, Initial: "S", Name: "索马里猫", Sort: 240},
{Species: model.SpeciesCat, Initial: "S", Name: "塞尔凯克卷毛猫", Sort: 250},
{Species: model.SpeciesCat, Initial: "S", Name: "斯芬克斯无毛猫", Sort: 260},
{Species: model.SpeciesCat, Initial: "S", Name: "沙特尔猫", Sort: 270},
{Species: model.SpeciesCat, Initial: "T", Name: "土耳其安哥拉猫", Sort: 280},
{Species: model.SpeciesCat, Initial: "T", Name: "土耳其梵猫", Sort: 290},
{Species: model.SpeciesCat, Initial: "X", Name: "暹罗猫", Sort: 300},
{Species: model.SpeciesCat, Initial: "X", Name: "新加坡猫", Sort: 310},
{Species: model.SpeciesCat, Initial: "Y", Name: "异国短毛猫", Sort: 320},
{Species: model.SpeciesCat, Initial: "Y", Name: "英国短毛猫", Sort: 330},
{Species: model.SpeciesCat, Initial: "Z", Name: "中华田园猫", Sort: 340},
// 狗 46 种
{Species: model.SpeciesDog, Initial: "A", Name: "阿拉斯加雪橇犬", Sort: 10},
{Species: model.SpeciesDog, Initial: "A", Name: "阿富汗猎犬", Sort: 20},
{Species: model.SpeciesDog, Initial: "A", Name: "澳洲牧羊犬", Sort: 30},
{Species: model.SpeciesDog, Initial: "B", Name: "比熊犬", Sort: 40},
{Species: model.SpeciesDog, Initial: "B", Name: "比格犬", Sort: 50},
{Species: model.SpeciesDog, Initial: "B", Name: "边境牧羊犬", Sort: 60},
{Species: model.SpeciesDog, Initial: "B", Name: "巴哥犬", Sort: 70},
{Species: model.SpeciesDog, Initial: "B", Name: "博美犬", Sort: 80},
{Species: model.SpeciesDog, Initial: "B", Name: "巴吉度猎犬", Sort: 90},
{Species: model.SpeciesDog, Initial: "B", Name: "北京犬", Sort: 100},
{Species: model.SpeciesDog, Initial: "C", Name: "柴犬", Sort: 110},
{Species: model.SpeciesDog, Initial: "C", Name: "川东猎犬", Sort: 120},
{Species: model.SpeciesDog, Initial: "D", Name: "德国牧羊犬", Sort: 130},
{Species: model.SpeciesDog, Initial: "D", Name: "杜宾犬", Sort: 140},
{Species: model.SpeciesDog, Initial: "D", Name: "大丹犬", Sort: 150},
{Species: model.SpeciesDog, Initial: "D", Name: "斗牛犬", Sort: 160},
{Species: model.SpeciesDog, Initial: "F", Name: "法国斗牛犬", Sort: 170},
{Species: model.SpeciesDog, Initial: "G", Name: "贵宾犬(泰迪)", Sort: 180},
{Species: model.SpeciesDog, Initial: "G", Name: "古代牧羊犬", Sort: 190},
{Species: model.SpeciesDog, Initial: "H", Name: "哈士奇", Sort: 200},
{Species: model.SpeciesDog, Initial: "H", Name: "蝴蝶犬", Sort: 210},
{Species: model.SpeciesDog, Initial: "J", Name: "金毛寻回犬", Sort: 220},
{Species: model.SpeciesDog, Initial: "J", Name: "吉娃娃", Sort: 230},
{Species: model.SpeciesDog, Initial: "K", Name: "柯基犬", Sort: 240},
{Species: model.SpeciesDog, Initial: "K", Name: "可卡犬", Sort: 250},
{Species: model.SpeciesDog, Initial: "K", Name: "苏格兰柯利犬", Sort: 260},
{Species: model.SpeciesDog, Initial: "L", Name: "拉布拉多寻回犬", Sort: 270},
{Species: model.SpeciesDog, Initial: "L", Name: "罗威纳犬", Sort: 280},
{Species: model.SpeciesDog, Initial: "L", Name: "灵缇犬", Sort: 290},
{Species: model.SpeciesDog, Initial: "L", Name: "腊肠犬", Sort: 300},
{Species: model.SpeciesDog, Initial: "M", Name: "马尔济斯犬", Sort: 310},
{Species: model.SpeciesDog, Initial: "M", Name: "迷你雪纳瑞", Sort: 320},
{Species: model.SpeciesDog, Initial: "N", Name: "牛头梗", Sort: 330},
{Species: model.SpeciesDog, Initial: "Q", Name: "秋田犬", Sort: 340},
{Species: model.SpeciesDog, Initial: "S", Name: "萨摩耶", Sort: 350},
{Species: model.SpeciesDog, Initial: "S", Name: "松狮犬", Sort: 360},
{Species: model.SpeciesDog, Initial: "S", Name: "圣伯纳犬", Sort: 370},
{Species: model.SpeciesDog, Initial: "X", Name: "雪纳瑞", Sort: 390},
{Species: model.SpeciesDog, Initial: "X", Name: "西高地白梗", Sort: 400},
{Species: model.SpeciesDog, Initial: "X", Name: "喜乐蒂牧羊犬", Sort: 410},
{Species: model.SpeciesDog, Initial: "Y", Name: "约克夏梗", Sort: 420},
{Species: model.SpeciesDog, Initial: "Y", Name: "银狐犬", Sort: 430},
{Species: model.SpeciesDog, Initial: "Z", Name: "中华田园犬", Sort: 440},
{Species: model.SpeciesDog, Initial: "Z", Name: "中亚牧羊犬", Sort: 450},
{Species: model.SpeciesDog, Initial: "Z", Name: "藏獒", Sort: 460},
}
for i := range breeds {
breeds[i].Enabled = true
}
return db.Create(&breeds).Error
}
// seedRecordTypes 只在表为空时跑。已经有数据(哪怕只有一条)就完全不碰,
+42
View File
@@ -280,6 +280,48 @@ func (h *Handler) AdminListPro(c *gin.Context) {
// AdminCareTemplateMeta GET /api/admin/care-templates/meta
// 给后台下拉用:物种、阶段、可选关联记录/弹层类型、提醒类型
// ── 品种 ──
type breedReq struct {
ID string `json:"id"`
Species string `json:"species"`
Name string `json:"name"`
Initial string `json:"initial"`
Sort int `json:"sort"`
Enabled bool `json:"enabled"`
}
// AdminListBreeds GET /api/admin/breeds?species=cat 连停用的一起返回
func (h *Handler) AdminListBreeds(c *gin.Context) {
response.OK(c, h.svc.AdminListBreeds(c.Query("species")))
}
// AdminSaveBreed POST /api/admin/breeds
func (h *Handler) AdminSaveBreed(c *gin.Context) {
var req breedReq
if err := c.ShouldBindJSON(&req); err != nil {
response.FailParams(c, err.Error())
return
}
b := &model.Breed{Species: req.Species, Name: req.Name,
Initial: req.Initial, Sort: req.Sort, Enabled: req.Enabled}
b.ID = req.ID
if err := h.svc.SaveBreed(b); err != nil {
respondErr(c, err)
return
}
response.OK(c, b)
}
// AdminDeleteBreed DELETE /api/admin/breeds/:id
func (h *Handler) AdminDeleteBreed(c *gin.Context) {
if err := h.svc.DeleteBreed(idParam(c, "id")); err != nil {
respondErr(c, err)
return
}
response.OK(c, nil)
}
// ── 记录类型 ──
type recordTypeReq struct {
+6
View File
@@ -158,3 +158,9 @@ func (h *Handler) Onboarding(c *gin.Context) {
}
response.OK(c, pet)
}
// Breeds GET /api/breeds?species=cat 品种,按首字母分组。
// 建档第二步的选择器直接照这个渲染,前端不做分组也不做排序
func (h *Handler) Breeds(c *gin.Context) {
response.OK(c, h.svc.ListBreeds(c.Query("species")))
}
+1
View File
@@ -30,6 +30,7 @@ func AllModels() []any {
&Pet{},
&HealthRecord{},
&RecordType{},
&Breed{},
&DailyTask{},
&Plan{},
&PlanTask{},
+19
View File
@@ -0,0 +1,19 @@
package model
// Breed 品种。原来建档时品种是个自由文本输入框,用户手打「柯基」「柯基犬」
// 「威尔士柯基」都算不同品种,后面想按品种做体重基准、常见病提示这类事就没法做。
type Breed struct {
Base
Species string `gorm:"size:16;index" json:"species"` // cat | dog
Name string `gorm:"size:64" json:"name"`
// Initial 首字母,A-Z,用来做侧边索引。
//
// 存字段而不是运行时算:Go 标准库没有拼音,引一个拼音库只为了取首字母不值得,
// 而且多音字(「柴犬」的柴、「藏獒」的藏)自动转常出错——「藏獒」按 cang
// 还是 zang 分组,人来定比库来猜靠谱。后台新增品种时手填这一位。
Initial string `gorm:"size:1;index" json:"initial"`
Sort int `json:"sort"`
Enabled bool `gorm:"default:true" json:"enabled"`
}
+5
View File
@@ -58,6 +58,7 @@ func registerUserAPI(api *gin.RouterGroup, h *handler.Handler, jm *appjwt.Manage
g.DELETE("/pets/:id", h.DeletePet)
g.GET("/record-types", h.RecordTypes)
g.GET("/breeds", h.Breeds)
g.GET("/pets/:id/records", h.ListRecords)
g.POST("/pets/:id/records", h.CreateRecord)
g.GET("/pets/:id/records/weight-trend", h.WeightTrend)
@@ -153,6 +154,10 @@ func registerAdminAPI(api *gin.RouterGroup, h *handler.Handler, jm *appjwt.Manag
g.POST("/articles", h.AdminSaveArticle)
g.DELETE("/articles/:id", h.AdminDeleteArticle)
g.GET("/breeds", h.AdminListBreeds)
g.POST("/breeds", h.AdminSaveBreed)
g.DELETE("/breeds/:id", h.AdminDeleteBreed)
g.GET("/record-types", h.AdminListRecordTypes)
g.POST("/record-types", h.AdminSaveRecordType)
g.DELETE("/record-types/:id", h.AdminDeleteRecordType)
+114
View File
@@ -0,0 +1,114 @@
package service
import (
"errors"
"log"
"sort"
"strconv"
"strings"
"github.com/sundynix/pets-be/internal/model"
)
// BreedGroup 同一首字母下的品种。小程序端的 A-Z 索引直接照这个渲染
type BreedGroup struct {
Initial string `json:"initial"`
Items []model.Breed `json:"items"`
}
// ListBreeds 某个物种的品种,按首字母分组。只返回启用的
func (s *Service) ListBreeds(species string) []BreedGroup {
if species != model.SpeciesCat && species != model.SpeciesDog {
return []BreedGroup{}
}
var all []model.Breed
s.db.Where("species = ? AND enabled = ?", species, true).
Order("initial asc, sort asc, id asc").Find(&all)
if len(all) == 0 {
// 不硬编码兜底一份:那等于把「后台可配」又变回代码常量,
// 改了配置的人会发现改了没用。宁可让选择器明显是空的
log.Printf("[warn] %s 没有任何启用的品种,建档第二步会选不了品种", species)
return []BreedGroup{}
}
byInitial := map[string][]model.Breed{}
for _, b := range all {
k := strings.ToUpper(b.Initial)
if k == "" {
k = "#" // 没填首字母的兜到最后,别让它凭空消失
}
byInitial[k] = append(byInitial[k], b)
}
keys := make([]string, 0, len(byInitial))
for k := range byInitial {
keys = append(keys, k)
}
sort.Strings(keys)
out := make([]BreedGroup, 0, len(keys))
for _, k := range keys {
out = append(out, BreedGroup{Initial: k, Items: byInitial[k]})
}
return out
}
// AdminListBreeds 后台用:连停用的一起返回
func (s *Service) AdminListBreeds(species string) []model.Breed {
q := s.db.Order("species asc, initial asc, sort asc, id asc")
if species == model.SpeciesCat || species == model.SpeciesDog {
q = q.Where("species = ?", species)
}
var all []model.Breed
q.Find(&all)
if all == nil {
return []model.Breed{}
}
return all
}
// SaveBreed 新增或更新
func (s *Service) SaveBreed(in *model.Breed) error {
in.Name = strings.TrimSpace(in.Name)
in.Initial = strings.ToUpper(strings.TrimSpace(in.Initial))
if in.Name == "" {
return ErrInvalidParam
}
if in.Species != model.SpeciesCat && in.Species != model.SpeciesDog {
return errors.New("物种只能是 cat 或 dog")
}
if len(in.Initial) != 1 || in.Initial[0] < 'A' || in.Initial[0] > 'Z' {
return errors.New("首字母要填一个 A-Z 的字母(用来做侧边索引)")
}
// 同物种下不允许同名:品种是拿来做统计口径的,重名会把同一种猫劈成两半
var n int64
q := s.db.Model(&model.Breed{}).Where("species = ? AND name = ?", in.Species, in.Name)
if in.ID != "" {
q = q.Where("id <> ?", in.ID)
}
q.Count(&n)
if n > 0 {
return errors.New("这个物种下已经有「" + in.Name + "」了")
}
if in.ID == "" {
return s.db.Create(in).Error
}
return s.db.Model(&model.Breed{}).Where("id = ?", in.ID).Updates(map[string]any{
"species": in.Species, "name": in.Name, "initial": in.Initial,
"sort": in.Sort, "enabled": in.Enabled,
}).Error
}
// DeleteBreed 已经有宠物在用的品种不许删——删了那些档案的品种就变成一个
// 查不到的字符串。想让它不再被选到应该用 enabled=false
func (s *Service) DeleteBreed(id string) error {
var b model.Breed
if err := s.db.First(&b, "id = ?", id).Error; err != nil {
return ErrNotFound
}
var used int64
s.db.Model(&model.Pet{}).Where("breed = ?", b.Name).Count(&used)
if used > 0 {
return errors.New("已经有 " + strconv.FormatInt(used, 10) + " 个档案填了「" +
b.Name + "」,删了它们的品种就查不到了。想隐藏请改成停用")
}
return s.db.Delete(&model.Breed{}, "id = ?", id).Error
}