@@ -35,6 +35,11 @@ const (
35
35
CorpAPIJsapi = CorpAPI + "get_jsapi_ticket?access_token="
36
36
)
37
37
38
+ const (
39
+ DataFormatXML = "XML" // default format
40
+ DataFormatJSON = "JSON"
41
+ )
42
+
38
43
var (
39
44
// Debug is a flag to Println()
40
45
Debug bool = false
@@ -54,7 +59,7 @@ type WxConfig struct {
54
59
AppName string
55
60
AppType int // 0-公众号,小程序; 1-企业微信
56
61
ExternalTokenHandler func (string , ... string ) * AccessToken // 外部token获取函数
57
- DateFormat string // 数据格式:JSON、XML
62
+ DataFormat string // 数据格式:JSON、XML
58
63
}
59
64
60
65
// Server 微信服务容器
@@ -72,7 +77,7 @@ type Server struct {
72
77
AesKey []byte // 解密的AesKey
73
78
SafeMode bool
74
79
EntMode bool
75
- DateFormat string // 通讯数据格式:JSON、XML
80
+ DataFormat string // 通讯数据格式:JSON、XML
76
81
77
82
RootUrl string
78
83
MsgUrl string
@@ -102,14 +107,19 @@ func Set(wc *WxConfig) *Server {
102
107
Token : wc .Token ,
103
108
EncodingAESKey : wc .EncodingAESKey ,
104
109
ExternalTokenHandler : wc .ExternalTokenHandler ,
105
- DateFormat : wc .DateFormat ,
110
+ DataFormat : wc .DataFormat ,
106
111
}
107
112
}
108
113
109
114
// New 微信服务容器
110
115
func New (wc * WxConfig ) * Server {
111
116
s := Set (wc )
112
117
118
+ // Set XML as default when data format is no setting.
119
+ if s .DataFormat == "" {
120
+ s .DataFormat = DataFormatXML
121
+ }
122
+
113
123
switch wc .AppType {
114
124
case 1 :
115
125
s .RootUrl = CorpAPI
@@ -162,23 +172,23 @@ func New(wc *WxConfig) *Server {
162
172
163
173
// 依据交互数据类型,从请求体中解析消息体
164
174
func (s * Server ) DecodeMsgFromRequest (r * http.Request , msg interface {}) error {
165
- if s .DateFormat == "XML" {
175
+ if s .DataFormat == DataFormatXML {
166
176
return xml .NewDecoder (r .Body ).Decode (msg )
167
- } else if s .DateFormat == "JSON" {
177
+ } else if s .DataFormat == DataFormatJSON {
168
178
return json .NewDecoder (r .Body ).Decode (msg )
169
179
} else {
170
- panic (fmt .Errorf ("invalid DataFormat:%s" , s .DateFormat ))
180
+ panic (fmt .Errorf ("invalid DataFormat:%s" , s .DataFormat ))
171
181
}
172
182
}
173
183
174
184
// 依据交互数据类型,从字符串中解析消息体
175
185
func (s * Server ) DecodeMsgFromString (str string , msg interface {}) error {
176
- if s .DateFormat == "XML" {
186
+ if s .DataFormat == DataFormatXML {
177
187
return xml .Unmarshal ([]byte (str ), msg )
178
- } else if s .DateFormat == "JSON" {
188
+ } else if s .DataFormat == DataFormatJSON {
179
189
return json .Unmarshal ([]byte (str ), msg )
180
190
} else {
181
- panic (fmt .Errorf ("invalid DataFormat:%s" , s .DateFormat ))
191
+ panic (fmt .Errorf ("invalid DataFormat:%s" , s .DataFormat ))
182
192
}
183
193
}
184
194
0 commit comments