mirror of
https://github.com/silenceper/wechat.git
synced 2026-02-23 13:42:25 +08:00
添加GetPhoneNumberContext方法 (#587)
This commit is contained in:
@@ -84,7 +84,9 @@ func (auth *Auth) CheckEncryptedDataContext(ctx context2.Context, encryptedMsgHa
|
|||||||
if at, err = auth.GetAccessToken(); err != nil {
|
if at, err = auth.GetAccessToken(); err != nil {
|
||||||
return
|
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
|
return
|
||||||
}
|
}
|
||||||
if err = util.DecodeWithError(response, &result, "CheckEncryptedDataAuth"); err != nil {
|
if err = util.DecodeWithError(response, &result, "CheckEncryptedDataAuth"); err != nil {
|
||||||
@@ -111,8 +113,8 @@ type PhoneInfo struct {
|
|||||||
} `json:"watermark"` // 数据水印
|
} `json:"watermark"` // 数据水印
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetPhoneNumber 小程序通过code获取用户手机号
|
// GetPhoneNumberContext 小程序通过code获取用户手机号
|
||||||
func (auth *Auth) GetPhoneNumber(code string) (*GetPhoneNumberResponse, error) {
|
func (auth *Auth) GetPhoneNumberContext(ctx context2.Context, code string) (*GetPhoneNumberResponse, error) {
|
||||||
var response []byte
|
var response []byte
|
||||||
var (
|
var (
|
||||||
at string
|
at string
|
||||||
@@ -124,12 +126,25 @@ func (auth *Auth) GetPhoneNumber(code string) (*GetPhoneNumberResponse, error) {
|
|||||||
body := map[string]interface{}{
|
body := map[string]interface{}{
|
||||||
"code": code,
|
"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
|
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
|
var result GetPhoneNumberResponse
|
||||||
if err = util.DecodeWithError(response, &result, "phonenumber.getPhoneNumber"); err != nil {
|
if err = util.DecodeWithError(response, &result, "phonenumber.getPhoneNumber"); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &result, nil
|
return &result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetPhoneNumber 小程序通过code获取用户手机号
|
||||||
|
func (auth *Auth) GetPhoneNumber(code string) (*GetPhoneNumberResponse, error) {
|
||||||
|
return auth.GetPhoneNumberContext(context2.Background(), code)
|
||||||
|
}
|
||||||
|
|||||||
11
util/http.go
11
util/http.go
@@ -43,16 +43,21 @@ func HTTPGetContext(ctx context.Context, uri string) ([]byte, error) {
|
|||||||
|
|
||||||
// HTTPPost post 请求
|
// HTTPPost post 请求
|
||||||
func HTTPPost(uri string, data string) ([]byte, error) {
|
func HTTPPost(uri string, data string) ([]byte, error) {
|
||||||
return HTTPPostContext(context.Background(), uri, data)
|
return HTTPPostContext(context.Background(), uri, []byte(data), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
// HTTPPostContext post 请求
|
// HTTPPostContext post 请求
|
||||||
func HTTPPostContext(ctx context.Context, uri string, data string) ([]byte, error) {
|
func HTTPPostContext(ctx context.Context, uri string, data []byte, header map[string]string) ([]byte, error) {
|
||||||
body := bytes.NewBuffer([]byte(data))
|
body := bytes.NewBuffer(data)
|
||||||
request, err := http.NewRequestWithContext(ctx, http.MethodPost, uri, body)
|
request, err := http.NewRequestWithContext(ctx, http.MethodPost, uri, body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for key, value := range header {
|
||||||
|
request.Header.Set(key, value)
|
||||||
|
}
|
||||||
|
|
||||||
response, err := http.DefaultClient.Do(request)
|
response, err := http.DefaultClient.Do(request)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
Reference in New Issue
Block a user