From 80ce4aefc4684c8c8d920d3528dc14f1c5f9fc2b Mon Sep 17 00:00:00 2001 From: wby Date: Tue, 11 Jan 2022 18:22:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E5=B0=8F=E7=A8=8B=E5=BA=8F?= =?UTF-8?q?=E6=96=B0=E7=89=88=E8=8E=B7=E5=8F=96=E6=89=8B=E6=9C=BA=E5=8F=B7?= =?UTF-8?q?=E6=8E=88=E6=9D=83=E6=8E=A5=E5=8F=A3=20(#528)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- miniprogram/auth/auth.go | 41 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/miniprogram/auth/auth.go b/miniprogram/auth/auth.go index 084bd925b..64204801b 100644 --- a/miniprogram/auth/auth.go +++ b/miniprogram/auth/auth.go @@ -13,6 +13,8 @@ const ( code2SessionURL = "https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code" checkEncryptedDataURL = "https://api.weixin.qq.com/wxa/business/checkencryptedmsg?access_token=%s" + + getPhoneNumber = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=%s" ) // Auth 登录/用户信息 @@ -90,3 +92,42 @@ func (auth *Auth) CheckEncryptedDataContext(ctx context2.Context, encryptedMsgHa } return } + +// GetPhoneNumberResponse 新版获取用户手机号响应结构体 +type GetPhoneNumberResponse struct { + util.CommonError + + PhoneInfo PhoneInfo `json:"phone_info"` +} + +// PhoneInfo 获取用户手机号内容 +type PhoneInfo struct { + PhoneNumber string `json:"phoneNumber"` // 用户绑定的手机号 + PurePhoneNumber string `json:"purePhoneNumber"` // 没有区号的手机号 + CountryCode string `json:"countryCode"` // 区号 + WaterMark struct { + Timestamp int64 `json:"timestamp"` + AppID string `json:"appid"` + } `json:"watermark"` // 数据水印 +} + +// GetPhoneNumber 小程序通过code获取用户手机号 +func (auth *Auth) GetPhoneNumber(code string) (result GetPhoneNumberResponse, err error) { + var response []byte + var ( + at string + ) + if at, err = auth.GetAccessToken(); err != nil { + return + } + body := map[string]interface{}{ + "code": code, + } + if response, err = util.PostJSON(fmt.Sprintf(getPhoneNumber, at), body); err != nil { + return + } + if err = util.DecodeWithError(response, &result, "phonenumber.getPhoneNumber"); err != nil { + return + } + return +}