1
0
mirror of https://github.com/silenceper/wechat.git synced 2026-02-04 12:52:27 +08:00

添加GetPhoneNumberContext方法 (#587)

This commit is contained in:
stepbystep2
2022-07-28 00:08:08 +08:00
committed by GitHub
parent cc9be30ed1
commit 108a65ecf3
2 changed files with 27 additions and 7 deletions

View File

@@ -84,7 +84,9 @@ func (auth *Auth) CheckEncryptedDataContext(ctx context2.Context, encryptedMsgHa
if at, err = auth.GetAccessToken(); err != nil {
return
}
if response, err = util.HTTPPostContext(ctx, fmt.Sprintf(checkEncryptedDataURL, at), "encrypted_msg_hash="+encryptedMsgHash); err != nil {
// 由于GetPhoneNumberContext需要传入JSON所以HTTPPostContext入参改为[]byte
if response, err = util.HTTPPostContext(ctx, fmt.Sprintf(checkEncryptedDataURL, at), []byte("encrypted_msg_hash="+encryptedMsgHash), nil); err != nil {
return
}
if err = util.DecodeWithError(response, &result, "CheckEncryptedDataAuth"); err != nil {
@@ -111,8 +113,8 @@ type PhoneInfo struct {
} `json:"watermark"` // 数据水印
}
// GetPhoneNumber 小程序通过code获取用户手机号
func (auth *Auth) GetPhoneNumber(code string) (*GetPhoneNumberResponse, error) {
// GetPhoneNumberContext 小程序通过code获取用户手机号
func (auth *Auth) GetPhoneNumberContext(ctx context2.Context, code string) (*GetPhoneNumberResponse, error) {
var response []byte
var (
at string
@@ -124,12 +126,25 @@ func (auth *Auth) GetPhoneNumber(code string) (*GetPhoneNumberResponse, error) {
body := map[string]interface{}{
"code": code,
}
if response, err = util.PostJSON(fmt.Sprintf(getPhoneNumber, at), body); err != nil {
bodyBytes, err := json.Marshal(body)
if err != nil {
return nil, err
}
header := map[string]string{"Content-Type": "application/json;charset=utf-8"}
if response, err = util.HTTPPostContext(ctx, fmt.Sprintf(getPhoneNumber, at), bodyBytes, header); err != nil {
return nil, err
}
var result GetPhoneNumberResponse
if err = util.DecodeWithError(response, &result, "phonenumber.getPhoneNumber"); err != nil {
return nil, err
}
return &result, nil
}
// GetPhoneNumber 小程序通过code获取用户手机号
func (auth *Auth) GetPhoneNumber(code string) (*GetPhoneNumberResponse, error) {
return auth.GetPhoneNumberContext(context2.Background(), code)
}

View File

@@ -43,16 +43,21 @@ func HTTPGetContext(ctx context.Context, uri string) ([]byte, error) {
// HTTPPost post 请求
func HTTPPost(uri string, data string) ([]byte, error) {
return HTTPPostContext(context.Background(), uri, data)
return HTTPPostContext(context.Background(), uri, []byte(data), nil)
}
// HTTPPostContext post 请求
func HTTPPostContext(ctx context.Context, uri string, data string) ([]byte, error) {
body := bytes.NewBuffer([]byte(data))
func HTTPPostContext(ctx context.Context, uri string, data []byte, header map[string]string) ([]byte, error) {
body := bytes.NewBuffer(data)
request, err := http.NewRequestWithContext(ctx, http.MethodPost, uri, body)
if err != nil {
return nil, err
}
for key, value := range header {
request.Header.Set(key, value)
}
response, err := http.DefaultClient.Do(request)
if err != nil {
return nil, err