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