From 7b2b7a16d78f313e977da13b8210b4f364269bfe Mon Sep 17 00:00:00 2001 From: tax Date: Sun, 14 Sep 2025 16:56:51 +0800 Subject: [PATCH 1/4] fix: handle JSON parse error when API returns binary file instead of error JSON --- util/error.go | 15 +++++++++++++++ work/material/media.go | 10 ++-------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/util/error.go b/util/error.go index 72b772f94..4354d32b3 100644 --- a/util/error.go +++ b/util/error.go @@ -68,3 +68,18 @@ func DecodeWithError(response []byte, obj interface{}, apiName string) error { } return nil } + +// HandleFileResponse 通用处理微信等接口返回:有时 JSON 错误,有时文件内容 +func HandleFileResponse(response []byte, apiName string) ([]byte, error) { + var commErr CommonError + if err := json.Unmarshal(response, &commErr); err == nil { + // 能解析成 JSON,判断是否为错误 + if commErr.ErrCode != 0 { + commErr.apiName = apiName + return nil, &commErr + } + // 能解析成 JSON 且没错误码,极少情况(比如微信返回的业务数据是 JSON 但无 errcode 字段),可根据需要调整 + } + // 不能解析成 JSON,或没错误码,直接返回原始内容 + return response, nil +} diff --git a/work/material/media.go b/work/material/media.go index 551eeee23..e11a52d80 100644 --- a/work/material/media.go +++ b/work/material/media.go @@ -191,12 +191,6 @@ func (r *Client) GetTempFile(mediaID string) ([]byte, error) { return nil, err } - // 检查响应是否为错误信息 - err = util.DecodeWithCommonError(response, "GetTempFile") - if err != nil { - return nil, err - } - - // 如果不是错误响应,则返回原始数据 - return response, nil + // 检查响应是否为错误信息,如果不是错误响应,则返回原始数据 + return util.HandleFileResponse(response, "GetTempFile") } From 5e90836b0f1debdc91de0faecaa35b389917e9f5 Mon Sep 17 00:00:00 2001 From: tax Date: Thu, 18 Sep 2025 17:41:58 +0800 Subject: [PATCH 2/4] fix: add JSON tags to SignatureOptions struct fields for proper serialization --- work/kf/callback.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/work/kf/callback.go b/work/kf/callback.go index a62cb1a9e..a7e7b92c9 100644 --- a/work/kf/callback.go +++ b/work/kf/callback.go @@ -8,10 +8,10 @@ import ( // SignatureOptions 微信服务器验证参数 type SignatureOptions struct { - Signature string `form:"msg_signature"` - TimeStamp string `form:"timestamp"` - Nonce string `form:"nonce"` - EchoStr string `form:"echostr"` + Signature string `form:"msg_signature" json:"msg_signature"` + TimeStamp string `form:"timestamp" json:"timestamp"` + Nonce string `form:"nonce" json:"nonce"` + EchoStr string `form:"echostr" json:"echostr"` } // VerifyURL 验证请求参数是否合法并返回解密后的消息内容 From 5b345055e88d2a6c368bf044c40180da80bfa482 Mon Sep 17 00:00:00 2001 From: tax Date: Thu, 18 Sep 2025 18:50:11 +0800 Subject: [PATCH 3/4] fix: mod module --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 0180599f0..fb5979c69 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/silenceper/wechat/v2 +module github.com/Xkta/wechat/v2 go 1.16 From c40277cac8bdbeb4fcafab758cf148fd253861ab Mon Sep 17 00:00:00 2001 From: tax Date: Thu, 18 Sep 2025 19:01:45 +0800 Subject: [PATCH 4/4] fix: rollback --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index fb5979c69..0180599f0 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/Xkta/wechat/v2 +module github.com/silenceper/wechat/v2 go 1.16