|
9 | 9 | "encoding/base64"
|
10 | 10 | "encoding/binary"
|
11 | 11 | "encoding/hex"
|
| 12 | + "encoding/json" |
12 | 13 | "encoding/xml"
|
13 | 14 | "fmt"
|
14 | 15 | "github.com/ArtisanCloud/PowerLibs/v3/object"
|
@@ -41,17 +42,17 @@ type CDATA struct {
|
41 | 42 | }
|
42 | 43 |
|
43 | 44 | type WeComReplyMsg struct {
|
44 |
| - XMLName xml.Name `xml:"xml"` |
45 |
| - Encrypt CDATA `xml:"Encrypt"` |
46 |
| - Signature CDATA `xml:"MsgSignature"` |
47 |
| - Timestamp string `xml:"TimeStamp"` |
48 |
| - Nonce CDATA `xml:"Nonce"` |
| 45 | + XMLName xml.Name `xml:"xml" json:"-"` |
| 46 | + Encrypt CDATA `xml:"Encrypt" json:"Encrypt"` |
| 47 | + Signature CDATA `xml:"MsgSignature" json:"Signature"` |
| 48 | + Timestamp string `xml:"TimeStamp" json:"Timestamp"` |
| 49 | + Nonce CDATA `xml:"Nonce" json:"Nonce"` |
49 | 50 | }
|
50 | 51 |
|
51 | 52 | type WeComRecvMsg struct {
|
52 |
| - ToUserName string `xml:"ToUserName"` |
53 |
| - Encrypt string `xml:"Encrypt"` |
54 |
| - AgentID string `xml:"AgentID"` |
| 53 | + ToUserName string `xml:"ToUserName" json:"ToUserName"` |
| 54 | + Encrypt string `xml:"Encrypt" json:"Encrypt"` |
| 55 | + AgentID string `xml:"AgentID" json:"AgentID"` |
55 | 56 | }
|
56 | 57 |
|
57 | 58 | type Encryptor struct {
|
@@ -147,7 +148,13 @@ func (encryptor *Encryptor) Encrypt(msg, nonce, timestamp string) ([]byte, *supp
|
147 | 148 | // Decrypt decrypt xml msg and return xml
|
148 | 149 | func (encryptor *Encryptor) Decrypt(content []byte, msgSignature, nonce, timestamp string) ([]byte, *support.CryptError) {
|
149 | 150 | var msg4Recv WeComRecvMsg
|
150 |
| - err := xml.Unmarshal(content, &msg4Recv) |
| 151 | + var err error |
| 152 | + // 根据第一个字符判断是json还是xml |
| 153 | + if content[0] == '<' { |
| 154 | + err = xml.Unmarshal(content, &msg4Recv) |
| 155 | + } else if content[0] == '{' { |
| 156 | + err = json.Unmarshal(content, &msg4Recv) |
| 157 | + } |
151 | 158 |
|
152 | 159 | if err != nil {
|
153 | 160 | return nil, support.NewCryptError(ErrorDecryptAes, err.Error())
|
|
0 commit comments