1
0
mirror of https://github.com/silenceper/wechat.git synced 2026-02-08 06:32:27 +08:00
Files
wechat/miniprogram/business/phone_number.go

50 lines
1.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package business
import (
"fmt"
"github.com/silenceper/wechat/v2/util"
)
const (
getPhoneNumberURL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=%s"
)
// GetPhoneNumberRequest 获取手机号请求
type GetPhoneNumberRequest struct {
Code string `json:"code"` // 手机号获取凭证
}
// PhoneInfo 手机号信息
type PhoneInfo struct {
PhoneNumber string `json:"phoneNumber"` // 用户绑定的手机号(国外手机号会有区号)
PurePhoneNumber string `json:"purePhoneNumber"` // 没有区号的手机号
CountryCode string `json:"countryCode"` // 区号
Watermark struct {
AppID string `json:"appid"` // 小程序 appid
Timestamp int64 `json:"timestamp"` // 用户获取手机号操作的时间戳
} `json:"watermark"`
}
// GetPhoneNumber code 换取用户手机号。每个 code 只能使用一次code 的有效期为 5min
func (business *Business) GetPhoneNumber(in *GetPhoneNumberRequest) (info PhoneInfo, err error) {
accessToken, err := business.GetAccessToken()
if err != nil {
return
}
uri := fmt.Sprintf(getPhoneNumberURL, accessToken)
response, err := util.PostJSON(uri, in)
if err != nil {
return
}
// 使用通用方法返回错误
var resp struct {
util.CommonError
PhoneInfo PhoneInfo `json:"phone_info"`
}
err = util.DecodeWithError(response, &resp, "business.GetPhoneNumber")
return resp.PhoneInfo, err
}