From 664978dd55f0cfef8e853cbf4dc14dfbd8899023 Mon Sep 17 00:00:00 2001 From: Rebellioncry <709476884@qq.com> Date: Tue, 22 Nov 2022 17:00:41 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E5=B0=8F=E7=A8=8B=E5=BA=8F=E7=A0=81=E6=8A=A5=E9=94=99=E9=97=AE?= =?UTF-8?q?=E9=A2=982?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- account.go | 30 ++++++++++++++++++++++++------ api_test.go | 16 ++++++++++++++++ core/method.go | 27 +++++++++------------------ 3 files changed, 49 insertions(+), 24 deletions(-) create mode 100644 api_test.go diff --git a/account.go b/account.go index e40bb4c..ba66fa7 100644 --- a/account.go +++ b/account.go @@ -372,8 +372,14 @@ func (s *Server) CreateWxaQrcode(authorizerAccessToken string, req *CreateWxaQrc u = CGIUrl + "/wxaapp/createwxaqrcode?" ) resp = &CreateWxaQrcodeResp{} - resp.Err(core.PostJsonReturnBuffer(s.AuthToken2url(u, authorizerAccessToken), req, resp)) - return + buffer, err := core.PostJsonReturnBuffer(s.AuthToken2url(u, authorizerAccessToken), req, resp) + if err != nil { + resp.Err(err) + return + } else { + resp.Buffer = buffer + return + } } type RGB struct { @@ -403,8 +409,14 @@ func (s *Server) GetWxaCode(authorizerAccessToken string, req *GetWxaCodeReq) (r u = WECHAT_API_URL + "/wxa/getwxacode?" ) resp = &GetWxaCodeResp{} - resp.Err(core.PostJsonReturnBuffer(s.AuthToken2url(u, authorizerAccessToken), req, resp)) - return + buffer, err := core.PostJsonReturnBuffer(s.AuthToken2url(u, authorizerAccessToken), req, resp) + if err != nil { + resp.Err(err) + return + } else { + resp.Buffer = buffer + return + } } type GetWxaCodeUnLimitReq struct { @@ -430,8 +442,14 @@ func (s *Server) GetWxaCodeUnLimit(authorizerAccessToken string, req *GetWxaCode u = WECHAT_API_URL + "/wxa/getwxacodeunlimit?" ) resp = &GetWxaCodeUnLimitResp{} - resp.Err(core.PostJsonReturnBuffer(s.AuthToken2url(u, authorizerAccessToken), req, resp)) - return + buffer, err := core.PostJsonReturnBuffer(s.AuthToken2url(u, authorizerAccessToken), req, resp) + if err != nil { + resp.Err(err) + return + } else { + resp.Buffer = buffer + return + } } type GetUserPhoneNumberReq struct { diff --git a/api_test.go b/api_test.go new file mode 100644 index 0000000..91b8fae --- /dev/null +++ b/api_test.go @@ -0,0 +1,16 @@ +package wechat3rd + +import ( + "log" + "testing" +) + +func TestName(t *testing.T) { + token := "63_vKrfyQ39H0f0fFJWSLcsIaTCOZD5KvFBtGrPkl9QxtzkDW5g7Y_R6CvZ4zxkZCqDpC6wfjl6RX4vCYQqiK9WfGprVcJQPS17K-2h-NBB0j_Kr1eHjs9ETpqOtnRurWOWrLpzdVnrgDgN1LksSLJaAIDJCA" + req := &GetWxaCodeUnLimitReq{ + Scene: "aaa", + } + s := Server{} + limit := s.GetWxaCodeUnLimit(token, req) + log.Print(limit) +} diff --git a/core/method.go b/core/method.go index 5062d00..7444818 100644 --- a/core/method.go +++ b/core/method.go @@ -32,39 +32,30 @@ func PostJson(incompleteURL string, request interface{}, response interface{}) e return json.NewDecoder(httpResp.Body).Decode(response) } -func PostJsonReturnBuffer(incompleteURL string, request interface{}, response interface{}) error { +func PostJsonReturnBuffer(incompleteURL string, request interface{}, response interface{}) (buffer []byte, err error) { var buf bytes.Buffer encoder := json.NewEncoder(&buf) encoder.SetEscapeHTML(false) - if err := encoder.Encode(request); err != nil { - return err + if err = encoder.Encode(request); err != nil { + return nil, err } httpResp, err := http.Post(incompleteURL, "application/json; charset=utf-8", &buf) if err != nil { - return err + return nil, err } defer httpResp.Body.Close() if httpResp.StatusCode != http.StatusOK { - return errors.New("http.Status:" + httpResp.Status) + return nil, errors.New("http.Status:" + httpResp.Status) } if err = json.NewDecoder(httpResp.Body).Decode(response); err != nil { - buffer, err := io.ReadAll(httpResp.Body) - if err != nil { - return err - } - m := make(map[string]interface{}) - m["errcode"] = 0 - m["errmsg"] = "" - m["buffer"] = buffer - jsonBytes, err := json.Marshal(m) + buffer, err = io.ReadAll(httpResp.Body) if err != nil { - return err + return nil, err } - json.Unmarshal(jsonBytes, &response) - return nil + return buffer, nil } - return nil + return nil, nil } func GetRequest(u string, request url.Values, response interface{}) error { From 2f46a3ae12ff0a74f38a9c9549eb76c0eb6af562 Mon Sep 17 00:00:00 2001 From: Rebellioncry <709476884@qq.com> Date: Tue, 22 Nov 2022 21:20:02 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E7=9C=9F=E6=AD=A3=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E5=B0=8F=E7=A8=8B=E5=BA=8F=E7=A0=81=E6=8A=A5?= =?UTF-8?q?=E9=94=99=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- account.go | 30 ++++++------------------------ api_test.go | 14 +++++++++++--- core/method.go | 33 +++++++++++++++++++++++---------- 3 files changed, 40 insertions(+), 37 deletions(-) diff --git a/account.go b/account.go index ba66fa7..e40bb4c 100644 --- a/account.go +++ b/account.go @@ -372,14 +372,8 @@ func (s *Server) CreateWxaQrcode(authorizerAccessToken string, req *CreateWxaQrc u = CGIUrl + "/wxaapp/createwxaqrcode?" ) resp = &CreateWxaQrcodeResp{} - buffer, err := core.PostJsonReturnBuffer(s.AuthToken2url(u, authorizerAccessToken), req, resp) - if err != nil { - resp.Err(err) - return - } else { - resp.Buffer = buffer - return - } + resp.Err(core.PostJsonReturnBuffer(s.AuthToken2url(u, authorizerAccessToken), req, resp)) + return } type RGB struct { @@ -409,14 +403,8 @@ func (s *Server) GetWxaCode(authorizerAccessToken string, req *GetWxaCodeReq) (r u = WECHAT_API_URL + "/wxa/getwxacode?" ) resp = &GetWxaCodeResp{} - buffer, err := core.PostJsonReturnBuffer(s.AuthToken2url(u, authorizerAccessToken), req, resp) - if err != nil { - resp.Err(err) - return - } else { - resp.Buffer = buffer - return - } + resp.Err(core.PostJsonReturnBuffer(s.AuthToken2url(u, authorizerAccessToken), req, resp)) + return } type GetWxaCodeUnLimitReq struct { @@ -442,14 +430,8 @@ func (s *Server) GetWxaCodeUnLimit(authorizerAccessToken string, req *GetWxaCode u = WECHAT_API_URL + "/wxa/getwxacodeunlimit?" ) resp = &GetWxaCodeUnLimitResp{} - buffer, err := core.PostJsonReturnBuffer(s.AuthToken2url(u, authorizerAccessToken), req, resp) - if err != nil { - resp.Err(err) - return - } else { - resp.Buffer = buffer - return - } + resp.Err(core.PostJsonReturnBuffer(s.AuthToken2url(u, authorizerAccessToken), req, resp)) + return } type GetUserPhoneNumberReq struct { diff --git a/api_test.go b/api_test.go index 91b8fae..82c1e20 100644 --- a/api_test.go +++ b/api_test.go @@ -1,16 +1,24 @@ package wechat3rd import ( + "fmt" + "io/ioutil" "log" "testing" ) func TestName(t *testing.T) { - token := "63_vKrfyQ39H0f0fFJWSLcsIaTCOZD5KvFBtGrPkl9QxtzkDW5g7Y_R6CvZ4zxkZCqDpC6wfjl6RX4vCYQqiK9WfGprVcJQPS17K-2h-NBB0j_Kr1eHjs9ETpqOtnRurWOWrLpzdVnrgDgN1LksSLJaAIDJCA" + token := "63_vV-1ucItNdryGAAVjtt9uAi-q3bSXXqwQvudlG6D-Iq2H5pCazfUOHy0xqUJJ9Y8NiuwskHkdivpfWiY86wmsK1eAui_Y4tM" req := &GetWxaCodeUnLimitReq{ Scene: "aaa", + Page: "pages/xxxx", } s := Server{} - limit := s.GetWxaCodeUnLimit(token, req) - log.Print(limit) + resp := s.GetWxaCodeUnLimit(token, req) + fileName := "./test.jpeg" + err := ioutil.WriteFile(fileName, resp.Buffer, 0666) + if err != nil { + fmt.Println("write fail") + } + log.Print(resp) } diff --git a/core/method.go b/core/method.go index 7444818..de68bd9 100644 --- a/core/method.go +++ b/core/method.go @@ -32,30 +32,43 @@ func PostJson(incompleteURL string, request interface{}, response interface{}) e return json.NewDecoder(httpResp.Body).Decode(response) } -func PostJsonReturnBuffer(incompleteURL string, request interface{}, response interface{}) (buffer []byte, err error) { +func PostJsonReturnBuffer(incompleteURL string, request interface{}, response interface{}) error { var buf bytes.Buffer encoder := json.NewEncoder(&buf) encoder.SetEscapeHTML(false) - if err = encoder.Encode(request); err != nil { - return nil, err + if err := encoder.Encode(request); err != nil { + return err } + httpResp, err := http.Post(incompleteURL, "application/json; charset=utf-8", &buf) if err != nil { - return nil, err + return err } defer httpResp.Body.Close() if httpResp.StatusCode != http.StatusOK { - return nil, errors.New("http.Status:" + httpResp.Status) + return errors.New("http.Status:" + httpResp.Status) } - if err = json.NewDecoder(httpResp.Body).Decode(response); err != nil { - buffer, err = io.ReadAll(httpResp.Body) + contentType := httpResp.Header.Get("content-type") + if contentType == "image/jpeg" { + buffer, err := io.ReadAll(httpResp.Body) + if err != nil { + return err + } + if len(buffer) < 200 { + return errors.New("二维码数据错误") + } + resMap := make(map[string]interface{}) + resMap["errcode"] = 0 + resMap["buffer"] = buffer + mapToJson, err := json.Marshal(resMap) if err != nil { - return nil, err + return err } - return buffer, nil + return json.Unmarshal(mapToJson, response) + } else { + return json.NewDecoder(httpResp.Body).Decode(response) } - return nil, nil } func GetRequest(u string, request url.Values, response interface{}) error {