Skip to content

Commit

Permalink
Merge pull request #24 from Rebellioncry/master
Browse files Browse the repository at this point in the history
真正修复小程序码获取错误问题
  • Loading branch information
Rebellioncry authored Nov 22, 2022
2 parents c87fd22 + 2f46a3a commit 40fdc77
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
24 changes: 24 additions & 0 deletions api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package wechat3rd

import (
"fmt"
"io/ioutil"
"log"
"testing"
)

func TestName(t *testing.T) {
token := "63_vV-1ucItNdryGAAVjtt9uAi-q3bSXXqwQvudlG6D-Iq2H5pCazfUOHy0xqUJJ9Y8NiuwskHkdivpfWiY86wmsK1eAui_Y4tM"
req := &GetWxaCodeUnLimitReq{
Scene: "aaa",
Page: "pages/xxxx",
}
s := Server{}
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)
}
22 changes: 13 additions & 9 deletions core/method.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func PostJsonReturnBuffer(incompleteURL string, request interface{}, response in
if err := encoder.Encode(request); err != nil {
return err
}

httpResp, err := http.Post(incompleteURL, "application/json; charset=utf-8", &buf)
if err != nil {
return err
Expand All @@ -48,23 +49,26 @@ func PostJsonReturnBuffer(incompleteURL string, request interface{}, response in
if httpResp.StatusCode != http.StatusOK {
return errors.New("http.Status:" + httpResp.Status)
}
if err = json.NewDecoder(httpResp.Body).Decode(response); err != nil {
contentType := httpResp.Header.Get("content-type")
if contentType == "image/jpeg" {
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)
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 err
}
json.Unmarshal(jsonBytes, &response)
return nil
return json.Unmarshal(mapToJson, response)
} else {
return json.NewDecoder(httpResp.Body).Decode(response)
}
return nil
}

func GetRequest(u string, request url.Values, response interface{}) error {
Expand Down

0 comments on commit 40fdc77

Please sign in to comment.