From 1e2f909f3493d263444400262169906fc7838211 Mon Sep 17 00:00:00 2001 From: okhowang <3352585+okhowang@users.noreply.github.com> Date: Fri, 17 Sep 2021 10:11:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=8F=E7=A8=8B=E5=BA=8Fauth=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0Context=E6=8E=A5=E5=8F=A3=20(#483)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- miniprogram/auth/auth.go | 15 +++++++++++++-- util/http.go | 23 +++++++++++++++++++++-- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/miniprogram/auth/auth.go b/miniprogram/auth/auth.go index 4f239f7..084bd92 100644 --- a/miniprogram/auth/auth.go +++ b/miniprogram/auth/auth.go @@ -1,6 +1,7 @@ package auth import ( + context2 "context" "encoding/json" "fmt" @@ -43,8 +44,13 @@ type RspCheckEncryptedData struct { // Code2Session 登录凭证校验。 func (auth *Auth) Code2Session(jsCode string) (result ResCode2Session, err error) { + return auth.Code2SessionContext(context2.Background(), jsCode) +} + +// Code2SessionContext 登录凭证校验。 +func (auth *Auth) Code2SessionContext(ctx context2.Context, jsCode string) (result ResCode2Session, err error) { var response []byte - if response, err = util.HTTPGet(fmt.Sprintf(code2SessionURL, auth.AppID, auth.AppSecret, jsCode)); err != nil { + if response, err = util.HTTPGetContext(ctx, fmt.Sprintf(code2SessionURL, auth.AppID, auth.AppSecret, jsCode)); err != nil { return } if err = json.Unmarshal(response, &result); err != nil { @@ -64,6 +70,11 @@ func (auth *Auth) GetPaidUnionID() { // CheckEncryptedData .检查加密信息是否由微信生成(当前只支持手机号加密数据),只能检测最近3天生成的加密数据 func (auth *Auth) CheckEncryptedData(encryptedMsgHash string) (result RspCheckEncryptedData, err error) { + return auth.CheckEncryptedDataContext(context2.Background(), encryptedMsgHash) +} + +// CheckEncryptedDataContext .检查加密信息是否由微信生成(当前只支持手机号加密数据),只能检测最近3天生成的加密数据 +func (auth *Auth) CheckEncryptedDataContext(ctx context2.Context, encryptedMsgHash string) (result RspCheckEncryptedData, err error) { var response []byte var ( at string @@ -71,7 +82,7 @@ func (auth *Auth) CheckEncryptedData(encryptedMsgHash string) (result RspCheckEn if at, err = auth.GetAccessToken(); err != nil { return } - if response, err = util.HTTPPost(fmt.Sprintf(checkEncryptedDataURL, at), "encrypted_msg_hash="+encryptedMsgHash); err != nil { + if response, err = util.HTTPPostContext(ctx, fmt.Sprintf(checkEncryptedDataURL, at), "encrypted_msg_hash="+encryptedMsgHash); err != nil { return } if err = util.DecodeWithError(response, &result, "CheckEncryptedDataAuth"); err != nil { diff --git a/util/http.go b/util/http.go index 2445341..40aefbc 100644 --- a/util/http.go +++ b/util/http.go @@ -2,6 +2,7 @@ package util import ( "bytes" + "context" "crypto/tls" "encoding/json" "encoding/pem" @@ -19,7 +20,16 @@ import ( // HTTPGet get 请求 func HTTPGet(uri string) ([]byte, error) { - response, err := http.Get(uri) + return HTTPGetContext(context.Background(), uri) +} + +// HTTPGetContext get 请求 +func HTTPGetContext(ctx context.Context, uri string) ([]byte, error) { + request, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil) + if err != nil { + return nil, err + } + response, err := http.DefaultClient.Do(request) if err != nil { return nil, err } @@ -33,8 +43,17 @@ func HTTPGet(uri string) ([]byte, error) { // HTTPPost post 请求 func HTTPPost(uri string, data string) ([]byte, error) { + return HTTPPostContext(context.Background(), uri, data) +} + +// HTTPPostContext post 请求 +func HTTPPostContext(ctx context.Context, uri string, data string) ([]byte, error) { body := bytes.NewBuffer([]byte(data)) - response, err := http.Post(uri, "", body) + request, err := http.NewRequestWithContext(ctx, http.MethodPost, uri, body) + if err != nil { + return nil, err + } + response, err := http.DefaultClient.Do(request) if err != nil { return nil, err }