feat: 宠物加性格/过敏/备注字段 + 身份卡照护信息 + 微信取手机号
- Pet 增 personality/allergy/notes;建档表单加「性格标签(多选)/过敏/备注」 - IDCard 增 color/personality/owner/phone(脱敏)/allergy/notes/疫苗驱虫状态/发证机构 - 微信取手机号:POST /api/user/phone 用 getPhoneNumber 的 code 换手机号存库 (BindPhoneByCode 走 getuserphonenumber,复用 access_token) Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -210,3 +210,20 @@ func (h *Handler) UpdateProfile(c *gin.Context) {
|
||||
}
|
||||
response.OK(c, user)
|
||||
}
|
||||
|
||||
// BindPhone POST /api/user/phone 用小程序 getPhoneNumber 的 code 绑定手机号
|
||||
func (h *Handler) BindPhone(c *gin.Context) {
|
||||
var req struct {
|
||||
Code string `json:"code"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil || req.Code == "" {
|
||||
response.FailParams(c, "缺少 code")
|
||||
return
|
||||
}
|
||||
phone, err := h.svc.BindPhoneByCode(middleware.UserID(c), req.Code)
|
||||
if err != nil {
|
||||
response.FailErr(c, err)
|
||||
return
|
||||
}
|
||||
response.OK(c, gin.H{"phone": phone})
|
||||
}
|
||||
|
||||
@@ -26,6 +26,9 @@ type petReq struct {
|
||||
Breed string `json:"breed"`
|
||||
AvatarFileID string `json:"avatar_file_id"`
|
||||
Goals []string `json:"goals"`
|
||||
Personality string `json:"personality"`
|
||||
Allergy string `json:"allergy"`
|
||||
Notes string `json:"notes"`
|
||||
}
|
||||
|
||||
func (r petReq) toInput() service.PetInput {
|
||||
@@ -33,6 +36,7 @@ func (r petReq) toInput() service.PetInput {
|
||||
Name: r.Name, Emoji: r.Emoji, Type: r.Type, Gender: r.Gender,
|
||||
Weight: r.Weight, Stage: r.Stage, Age: r.Age, Color: r.Color, Breed: r.Breed,
|
||||
AvatarFileID: r.AvatarFileID,
|
||||
Personality: r.Personality, Allergy: r.Allergy, Notes: r.Notes,
|
||||
}
|
||||
if r.Birthday != "" {
|
||||
if t, err := time.ParseInLocation("2006-01-02", r.Birthday, time.Local); err == nil {
|
||||
@@ -123,6 +127,17 @@ func (h *Handler) UpdatePet(c *gin.Context) {
|
||||
if req.Color != "" {
|
||||
fields["color"] = req.Color
|
||||
}
|
||||
// 性格/过敏/备注:空串也允许写(用户可能想清空),用指针语义太重,
|
||||
// 这里约定前端总是回传当前值,非空才更新;清空场景暂用「无」占位
|
||||
if req.Personality != "" {
|
||||
fields["personality"] = req.Personality
|
||||
}
|
||||
if req.Allergy != "" {
|
||||
fields["allergy"] = req.Allergy
|
||||
}
|
||||
if req.Notes != "" {
|
||||
fields["notes"] = req.Notes
|
||||
}
|
||||
if req.Breed != "" {
|
||||
fields["breed"] = req.Breed
|
||||
}
|
||||
|
||||
@@ -27,4 +27,9 @@ type Pet struct {
|
||||
Breed string `gorm:"size:64" json:"breed"` // 品种
|
||||
HealthStatus string `gorm:"size:16;default:正常" json:"health_status"`
|
||||
Goals datatypes.JSON `json:"goals"` // onboarding 目标多选
|
||||
|
||||
// —— 身份卡用的补充信息 ——
|
||||
Personality string `gorm:"size:64" json:"personality"` // 性格标签,逗号分隔,如 "活泼,亲人,贪吃"
|
||||
Allergy string `gorm:"size:128" json:"allergy"` // 过敏情况,没有就填「无」
|
||||
Notes string `gorm:"size:255" json:"notes"` // 备注信息(照护提醒)
|
||||
}
|
||||
|
||||
@@ -49,6 +49,7 @@ func registerUserAPI(api *gin.RouterGroup, h *handler.Handler, jm *appjwt.Manage
|
||||
|
||||
g.GET("/user/profile", h.Profile)
|
||||
g.PUT("/user/profile", h.UpdateProfile)
|
||||
g.POST("/user/phone", h.BindPhone)
|
||||
g.GET("/user/summary", h.UserSummary)
|
||||
g.PUT("/user/decoration", h.UpdateDecoration)
|
||||
|
||||
|
||||
@@ -17,11 +17,23 @@ type IDCard struct {
|
||||
Zodiac string `json:"zodiac"` // 星座,按生日算
|
||||
Weight string `json:"weight"` // 体重,如 "13.5kg"
|
||||
Intro string `json:"intro"`
|
||||
Color string `json:"color"` // 毛色
|
||||
Personality string `json:"personality"` // 性格标签,逗号分隔
|
||||
AvatarURL string `json:"avatar_url"`
|
||||
IDNo string `json:"id_no"` // 玩具身份号,不入库
|
||||
Validity string `json:"validity"` // "2023.12.15 - 永远"
|
||||
QRURL string `json:"qr_url"` // 小程序码,未发布时为空
|
||||
QRReady bool `json:"qr_ready"` // 码有没有生成出来
|
||||
|
||||
// —— 背面:照护信息 ——
|
||||
Owner string `json:"owner"` // 主人昵称
|
||||
Phone string `json:"phone"` // 联系电话(脱敏)
|
||||
Allergy string `json:"allergy"` // 过敏情况
|
||||
Notes string `json:"notes"` // 备注
|
||||
VaccineState string `json:"vaccine_state"` // 疫苗情况
|
||||
DewormState string `json:"deworm_state"` // 驱虫情况
|
||||
Org string `json:"org"` // 发证机构
|
||||
|
||||
QRURL string `json:"qr_url"` // 小程序码,未发布时为空
|
||||
QRReady bool `json:"qr_ready"` // 码有没有生成出来
|
||||
}
|
||||
|
||||
// petIDNo 造一个稳定的「身份号」。纯装饰,不入库、不做校验。
|
||||
@@ -73,6 +85,14 @@ func zodiacOf(month, day int) string {
|
||||
return name
|
||||
}
|
||||
|
||||
// maskPhone 脱敏手机号:138****8888。空或非 11 位原样返回
|
||||
func maskPhone(p string) string {
|
||||
if len(p) != 11 {
|
||||
return p
|
||||
}
|
||||
return p[:3] + "****" + p[7:]
|
||||
}
|
||||
|
||||
// GetIDCard 组装身份卡。小程序码是 best-effort:拿不到(未发布/配额)也照出卡
|
||||
func (s *Service) GetIDCard(userID, petID string) (*IDCard, error) {
|
||||
pet, err := s.GetPet(userID, petID)
|
||||
@@ -99,10 +119,39 @@ func (s *Service) GetIDCard(userID, petID string) (*IDCard, error) {
|
||||
zodiac = zodiacOf(int(pet.Birthday.Month()), pet.Birthday.Day())
|
||||
}
|
||||
|
||||
// 主人昵称 + 脱敏电话
|
||||
var user model.User
|
||||
s.db.First(&user, userID)
|
||||
owner := user.Nickname
|
||||
if owner == "" {
|
||||
owner = "铲屎官"
|
||||
}
|
||||
|
||||
// 疫苗:有记录算已接种;驱虫:取下一次到期提醒的日期
|
||||
var vaccineCnt int64
|
||||
s.db.Model(&model.HealthRecord{}).Where("pet_id = ? AND type = ?", petID, model.RecordVaccine).Count(&vaccineCnt)
|
||||
vaccineState := "待接种"
|
||||
if vaccineCnt > 0 {
|
||||
vaccineState = "已接种"
|
||||
}
|
||||
dewormState := "—"
|
||||
var dw model.Reminder
|
||||
if err := s.db.Where("pet_id = ? AND type = ?", petID, model.ReminderDeworm).Order("next_due_date asc").First(&dw).Error; err == nil && dw.NextDueDate != nil {
|
||||
dewormState = dw.NextDueDate.Format("2006.01.02")
|
||||
}
|
||||
|
||||
allergy := pet.Allergy
|
||||
if allergy == "" {
|
||||
allergy = "无"
|
||||
}
|
||||
|
||||
card := &IDCard{
|
||||
Name: pet.Name, Species: pet.Type, Gender: pet.Gender, Breed: pet.Breed,
|
||||
Birthday: bd, Zodiac: zodiac, Weight: pet.Weight, Intro: intro,
|
||||
Color: pet.Color, Personality: pet.Personality,
|
||||
AvatarURL: pet.AvatarURL, IDNo: petIDNo(pet), Validity: validity,
|
||||
Owner: owner, Phone: maskPhone(user.Phone), Allergy: allergy, Notes: pet.Notes,
|
||||
VaccineState: vaccineState, DewormState: dewormState, Org: "肉垫计划",
|
||||
}
|
||||
|
||||
// 小程序码取不到不算错——未发布/配额用尽时卡照样出,只是没码
|
||||
|
||||
@@ -27,6 +27,9 @@ type PetInput struct {
|
||||
Breed string
|
||||
AvatarFileID string
|
||||
Goals datatypes.JSON
|
||||
Personality string
|
||||
Allergy string
|
||||
Notes string
|
||||
}
|
||||
|
||||
func normalizeWeight(w string) string {
|
||||
@@ -86,6 +89,9 @@ func (s *Service) CreatePet(userID string, in PetInput) (*model.Pet, error) {
|
||||
Color: in.Color,
|
||||
Breed: in.Breed,
|
||||
AvatarFileID: in.AvatarFileID,
|
||||
Personality: in.Personality,
|
||||
Allergy: in.Allergy,
|
||||
Notes: in.Notes,
|
||||
HealthStatus: "正常",
|
||||
Goals: in.Goals,
|
||||
}
|
||||
|
||||
@@ -11,6 +11,8 @@ import (
|
||||
"net/url"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/sundynix/pets-be/internal/model"
|
||||
)
|
||||
|
||||
// 小程序码(wxacode)。身份卡右下角那个码,扫了能进小程序。
|
||||
@@ -123,3 +125,38 @@ func (s *Service) PetQRCode(petID string) (string, error) {
|
||||
// 41030 = page 不在已发布版本里。开发期常见,往上层透传让它降级
|
||||
return "", fmt.Errorf("微信生成小程序码失败(%d): %s", e.ErrCode, e.ErrMsg)
|
||||
}
|
||||
|
||||
// BindPhoneByCode 用小程序 getPhoneNumber 拿到的 code 换手机号,存到用户上。
|
||||
// 返回脱敏后的手机号。开发期/未认证时微信会报错,往上层透传。
|
||||
func (s *Service) BindPhoneByCode(userID, code string) (string, error) {
|
||||
token, err := s.wechatAccessToken()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
payload, _ := json.Marshal(map[string]string{"code": code})
|
||||
var out struct {
|
||||
ErrCode int `json:"errcode"`
|
||||
ErrMsg string `json:"errmsg"`
|
||||
PhoneInfo struct {
|
||||
PhoneNumber string `json:"phoneNumber"`
|
||||
PurePhoneNumber string `json:"purePhoneNumber"`
|
||||
} `json:"phone_info"`
|
||||
}
|
||||
if err := postJSON("https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token="+token, payload, &out); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if out.ErrCode != 0 {
|
||||
return "", fmt.Errorf("获取手机号失败(%d): %s", out.ErrCode, out.ErrMsg)
|
||||
}
|
||||
phone := out.PhoneInfo.PurePhoneNumber
|
||||
if phone == "" {
|
||||
phone = out.PhoneInfo.PhoneNumber
|
||||
}
|
||||
if phone == "" {
|
||||
return "", fmt.Errorf("微信没返回手机号")
|
||||
}
|
||||
if err := s.db.Model(&model.User{}).Where("id = ?", userID).Update("phone", phone).Error; err != nil {
|
||||
return "", err
|
||||
}
|
||||
return maskPhone(phone), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user