Skip to content

Commit bf4f142

Browse files
authored
Merge pull request #42 from deancn/master
fix: resolve dataformat typo and set xml as default dataformat
2 parents 09c148b + be892fb commit bf4f142

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

server.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ const (
3535
CorpAPIJsapi = CorpAPI + "get_jsapi_ticket?access_token="
3636
)
3737

38+
const (
39+
DataFormatXML = "XML" // default format
40+
DataFormatJSON = "JSON"
41+
)
42+
3843
var (
3944
// Debug is a flag to Println()
4045
Debug bool = false
@@ -54,7 +59,7 @@ type WxConfig struct {
5459
AppName string
5560
AppType int // 0-公众号,小程序; 1-企业微信
5661
ExternalTokenHandler func(string, ...string) *AccessToken // 外部token获取函数
57-
DateFormat string // 数据格式:JSON、XML
62+
DataFormat string // 数据格式:JSON、XML
5863
}
5964

6065
// Server 微信服务容器
@@ -72,7 +77,7 @@ type Server struct {
7277
AesKey []byte // 解密的AesKey
7378
SafeMode bool
7479
EntMode bool
75-
DateFormat string // 通讯数据格式:JSON、XML
80+
DataFormat string // 通讯数据格式:JSON、XML
7681

7782
RootUrl string
7883
MsgUrl string
@@ -102,14 +107,19 @@ func Set(wc *WxConfig) *Server {
102107
Token: wc.Token,
103108
EncodingAESKey: wc.EncodingAESKey,
104109
ExternalTokenHandler: wc.ExternalTokenHandler,
105-
DateFormat: wc.DateFormat,
110+
DataFormat: wc.DataFormat,
106111
}
107112
}
108113

109114
// New 微信服务容器
110115
func New(wc *WxConfig) *Server {
111116
s := Set(wc)
112117

118+
// Set XML as default when data format is no setting.
119+
if s.DataFormat == "" {
120+
s.DataFormat = DataFormatXML
121+
}
122+
113123
switch wc.AppType {
114124
case 1:
115125
s.RootUrl = CorpAPI
@@ -162,23 +172,23 @@ func New(wc *WxConfig) *Server {
162172

163173
// 依据交互数据类型,从请求体中解析消息体
164174
func (s *Server) DecodeMsgFromRequest(r *http.Request, msg interface{}) error {
165-
if s.DateFormat == "XML" {
175+
if s.DataFormat == DataFormatXML {
166176
return xml.NewDecoder(r.Body).Decode(msg)
167-
} else if s.DateFormat == "JSON" {
177+
} else if s.DataFormat == DataFormatJSON {
168178
return json.NewDecoder(r.Body).Decode(msg)
169179
} else {
170-
panic(fmt.Errorf("invalid DataFormat:%s", s.DateFormat))
180+
panic(fmt.Errorf("invalid DataFormat:%s", s.DataFormat))
171181
}
172182
}
173183

174184
// 依据交互数据类型,从字符串中解析消息体
175185
func (s *Server) DecodeMsgFromString(str string, msg interface{}) error {
176-
if s.DateFormat == "XML" {
186+
if s.DataFormat == DataFormatXML {
177187
return xml.Unmarshal([]byte(str), msg)
178-
} else if s.DateFormat == "JSON" {
188+
} else if s.DataFormat == DataFormatJSON {
179189
return json.Unmarshal([]byte(str), msg)
180190
} else {
181-
panic(fmt.Errorf("invalid DataFormat:%s", s.DateFormat))
191+
panic(fmt.Errorf("invalid DataFormat:%s", s.DataFormat))
182192
}
183193
}
184194

0 commit comments

Comments
 (0)