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:
@@ -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