Skip to content

Commit eea7d1c

Browse files
authored
Merge pull request #536 from ArtisanCloud/develop
Develop
2 parents a06d394 + f1bad4e commit eea7d1c

File tree

5 files changed

+166
-34
lines changed

5 files changed

+166
-34
lines changed

src/kernel/contract/eventInterface.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import "net/http"
55
type EventInterface interface {
66
GetToUserName() string
77
GetFromUserName() string
8-
GetCreateTime() string
8+
GetCreateTime() int
99
GetMsgType() string
1010
GetEvent() string
1111
GetChangeType() string

src/kernel/messages/message.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ const ALL = TEXT | IMAGE | VOICE | VIDEO |
2828
DEVICE_TEXT | FILE | TEXT_CARD | TRANSFER |
2929
EVENT | MINIPROGRAM_PAGE | MINIPROGRAM_NOTICE
3030

31+
// 消息推送的数据格式
32+
const (
33+
// DataTypeXML XML 格式数据
34+
DataTypeXML = "xml"
35+
// DataTypeJSON JSON 格式数据
36+
DataTypeJSON = "json"
37+
)
38+
3139
type Message struct {
3240
contract.MessageInterface
3341

src/kernel/models/callback.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ type CallbackMessageHeader struct {
2727
contract.EventInterface
2828
XMLName xml.Name `xml:"xml"`
2929
Text string `xml:",chardata"`
30-
ToUserName string `xml:"ToUserName"`
31-
FromUserName string `xml:"FromUserName"`
32-
CreateTime string `xml:"CreateTime"`
33-
MsgType string `xml:"MsgType"`
34-
Event string `xml:"Event"`
35-
ChangeType string `xml:"ChangeType"`
36-
EventKey string `xml:"EventKey,omitempty"`
30+
ToUserName string `xml:"ToUserName" json:"ToUserName"`
31+
FromUserName string `xml:"FromUserName" json:"FromUserName"`
32+
CreateTime int `xml:"CreateTime" json:"CreateTime"`
33+
MsgType string `xml:"MsgType" json:"MsgType"`
34+
Event string `xml:"Event" json:"Event"`
35+
ChangeType string `xml:"ChangeType" json:"ChangeType"`
36+
EventKey string `xml:"EventKey,omitempty" json:"EventKey,omitempty"`
3737
Content []byte
3838
}
3939

@@ -45,7 +45,7 @@ func (header CallbackMessageHeader) GetFromUserName() string {
4545
return header.FromUserName
4646
}
4747

48-
func (header CallbackMessageHeader) GetCreateTime() string {
48+
func (header CallbackMessageHeader) GetCreateTime() int {
4949
return header.CreateTime
5050
}
5151

src/kernel/serverGuard.go

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ type ServerGuard struct {
5050
IsSafeMode func(request *http.Request) bool
5151
Validate func(request *http.Request) (*ServerGuard, error)
5252
ShouldReturnRawResponse func(request *http.Request) bool
53+
RequestDataType func(request *http.Request) string
5354

5455
ToCallbackType func(callbackHeader contract.EventInterface, buf []byte) (decryptMessage interface{}, err error)
5556

@@ -75,6 +76,9 @@ func NewServerGuard(app *ApplicationInterface) *ServerGuard {
7576
serverGuard.ShouldReturnRawResponse = func(request *http.Request) bool {
7677
return serverGuard.shouldReturnRawResponse(request)
7778
}
79+
serverGuard.RequestDataType = func(request *http.Request) string {
80+
return serverGuard.requestDataType(request)
81+
}
7882

7983
serverGuard.OverrideGetToken()
8084
serverGuard.OverrideResolve()
@@ -150,15 +154,18 @@ func (serverGuard *ServerGuard) GetEvent(request *http.Request) (callback *model
150154
if request == nil {
151155
return nil, nil, errors.New("request is invalid")
152156
}
153-
var b []byte = []byte("<xml></xml>")
157+
var b []byte = []byte("")
154158
if request.Body != http.NoBody {
155159
b, err = io.ReadAll(request.Body)
156160
if err != nil || b == nil {
157161
return nil, nil, err
158162
}
159163
}
160164

161-
callback, err = serverGuard.ParseMessage(string(b))
165+
// 请求数据类型
166+
rDataType := serverGuard.RequestDataType(request)
167+
168+
callback, err = serverGuard.ParseMessage(string(b), rDataType)
162169
if err != nil {
163170
return nil, nil, err
164171
}
@@ -167,12 +174,15 @@ func (serverGuard *ServerGuard) GetEvent(request *http.Request) (callback *model
167174
callbackHeader, err = serverGuard.DecryptEvent(request, string(b))
168175
} else {
169176
callbackHeader = &models.CallbackMessageHeader{}
170-
err = xml.Unmarshal(b, callbackHeader)
177+
if rDataType == messages.DataTypeXML {
178+
err = xml.Unmarshal(b, callbackHeader)
179+
} else {
180+
err = json.Unmarshal(b, callbackHeader)
181+
}
171182
callbackHeader.Content = b
172183
}
173184

174185
return callback, callbackHeader, err
175-
176186
}
177187

178188
func (serverGuard *ServerGuard) GetMessage(request *http.Request) (callback *models.Callback, callbackHeader *models.CallbackMessageHeader, Decrypted interface{}, err error) {
@@ -184,7 +194,7 @@ func (serverGuard *ServerGuard) GetMessage(request *http.Request) (callback *mod
184194
}
185195
}
186196

187-
callback, err = serverGuard.ParseMessage(string(b))
197+
callback, err = serverGuard.ParseMessage(string(b), serverGuard.RequestDataType(request))
188198
if err != nil {
189199
return nil, nil, nil, err
190200
}
@@ -196,11 +206,9 @@ func (serverGuard *ServerGuard) GetMessage(request *http.Request) (callback *mod
196206
Text: callback.Text,
197207
ToUserName: callback.ToUserName,
198208
}
199-
200209
}
201210

202211
return callback, callbackHeader, Decrypted, err
203-
204212
}
205213

206214
func (serverGuard *ServerGuard) ResolveEvent(request *http.Request, closure func(event contract.EventInterface) interface{}) (rs *http.Response, err error) {
@@ -401,32 +409,39 @@ func (serverGuard *ServerGuard) signature(params []string) string {
401409
}
402410

403411
func (serverGuard *ServerGuard) isSafeMode(request *http.Request) bool {
404-
405412
query := request.URL.Query()
406413

407414
return query.Get("signature") != "" && "aes" == query.Get("encrypt_type")
408-
409415
}
410416

411-
func (serverGuard *ServerGuard) ParseMessage(content string) (callback *models.Callback, err error) {
417+
func (serverGuard *ServerGuard) requestDataType(request *http.Request) string {
418+
if strings.HasPrefix(request.Header.Get("Content-Type"), "text/xml") ||
419+
strings.HasPrefix(request.Header.Get("Content-Type"), "application/xml") {
420+
// xml 格式
421+
return messages.DataTypeXML
422+
} else {
423+
// json 格式
424+
return messages.DataTypeJSON
425+
}
426+
}
412427

428+
func (serverGuard *ServerGuard) ParseMessage(content string, dataType string) (callback *models.Callback, err error) {
413429
callback = &models.Callback{}
414430

415-
if len(content) > 0 {
416-
if content[0:1] == "<" {
417-
err = xml.Unmarshal([]byte(content), callback)
418-
if err != nil {
419-
return nil, err
420-
}
421-
} else {
422-
// Handle JSON format.
423-
err = object.JsonDecode([]byte(content), callback)
424-
if err != nil {
425-
return nil, err
426-
}
431+
if len(content) <= 0 {
432+
return nil, errors.New("request body is empty")
433+
}
434+
435+
if dataType == messages.DataTypeXML {
436+
err = xml.Unmarshal([]byte(content), callback)
437+
if err != nil {
438+
return nil, err
427439
}
428440
} else {
429-
441+
err = object.JsonDecode([]byte(content), callback)
442+
if err != nil {
443+
return nil, err
444+
}
430445
}
431446

432447
return callback, err
@@ -477,7 +492,11 @@ func (serverGuard *ServerGuard) DecryptEvent(request *http.Request, content stri
477492
}
478493

479494
callbackHeader = &models.CallbackMessageHeader{}
480-
err = xml.Unmarshal(buf, callbackHeader)
495+
if serverGuard.RequestDataType(request) == messages.DataTypeXML {
496+
err = xml.Unmarshal(buf, callbackHeader)
497+
} else {
498+
err = json.Unmarshal(buf, callbackHeader)
499+
}
481500
if err != nil {
482501
return nil, err
483502
}
@@ -503,7 +522,11 @@ func (serverGuard *ServerGuard) decryptMessage(request *http.Request, content st
503522
}
504523

505524
callbackHeader = &models.CallbackMessageHeader{}
506-
err = xml.Unmarshal(buf, callbackHeader)
525+
if serverGuard.RequestDataType(request) == messages.DataTypeXML {
526+
err = xml.Unmarshal(buf, callbackHeader)
527+
} else {
528+
err = json.Unmarshal(buf, callbackHeader)
529+
}
507530
if err != nil {
508531
return nil, nil, err
509532
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package models
2+
3+
import (
4+
"encoding/xml"
5+
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel/contract"
6+
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel/models"
7+
)
8+
9+
type MsgAgentID struct {
10+
MsgID uint64 `xml:"MsgId" json:"MsgId"`
11+
AgentID uint32 `xml:"AgentID" json:"AgentID"`
12+
}
13+
14+
type MessageText struct {
15+
contract.EventInterface
16+
models.CallbackMessageHeader
17+
Content string `xml:"Content" json:"Content"`
18+
MsgAgentID
19+
}
20+
21+
type MessageImage struct {
22+
contract.EventInterface
23+
models.CallbackMessageHeader
24+
PicUrl string `xml:"PicUrl"`
25+
MediaID string `xml:"MediaId"`
26+
MsgAgentID
27+
}
28+
29+
type MessageVoice struct {
30+
contract.EventInterface
31+
models.CallbackMessageHeader
32+
MediaID string `xml:"MediaId"`
33+
Format string `xml:"Format"`
34+
MsgAgentID
35+
}
36+
37+
type MessageLocation struct {
38+
contract.EventInterface
39+
models.CallbackMessageHeader
40+
LocationX string `xml:"Location_X"`
41+
LocationY string `xml:"Location_Y"`
42+
Scale string `xml:"Scale"`
43+
Label string `xml:"Label"`
44+
AppType string `xml:"AppType"`
45+
MsgAgentID
46+
}
47+
48+
type MessageVideo struct {
49+
contract.EventInterface
50+
models.CallbackMessageHeader
51+
MediaID string `xml:"MediaId"`
52+
ThumbMediaID string `xml:"ThumbMediaId"`
53+
MsgAgentID
54+
}
55+
56+
type MessageLink struct {
57+
contract.EventInterface
58+
models.CallbackMessageHeader
59+
Title string `xml:"Title"`
60+
Description string `xml:"Description"`
61+
URL string `xml:"Url"`
62+
PicUrl string `xml:"PicUrl"`
63+
MsgAgentID
64+
}
65+
66+
// CommonPushData 推送数据通用部分
67+
type CommonPushData struct {
68+
XMLName xml.Name `json:"-" xml:"xml"`
69+
MsgType string `json:"MsgType" xml:"MsgType"` // 消息类型,为固定值 "event"
70+
Event string `json:"Event" xml:"Event"` // 事件类型
71+
ToUserName string `json:"ToUserName" xml:"ToUserName"` // 小程序的原始 ID
72+
FromUserName string `json:"FromUserName" xml:"FromUserName"` // 发送方账号(一个 OpenID,此时发送方是系统账号)
73+
CreateTime int64 `json:"CreateTime" xml:"CreateTime"` // 消息创建时间(整型),时间戳
74+
}
75+
76+
// MediaCheckAsyncData 媒体内容安全异步审查结果通知
77+
type MediaCheckAsyncData struct {
78+
CommonPushData
79+
Appid string `json:"appid" xml:"appid"`
80+
TraceID string `json:"trace_id" xml:"trace_id"`
81+
Version int `json:"version" xml:"version"`
82+
Detail []*MediaCheckDetail `json:"detail" xml:"detail"`
83+
ErrCode int `json:"errCode" xml:"errCode"`
84+
ErrMsg string `json:"errMsg" xml:"errMsg"`
85+
Result MediaCheckAsyncResult `json:"result" xml:"result"`
86+
}
87+
88+
// MediaCheckDetail 检测结果详情
89+
type MediaCheckDetail struct {
90+
Strategy string `json:"strategy" xml:"strategy"`
91+
ErrCode int `json:"errCode" xml:"errCode"`
92+
Suggest string `json:"suggest" xml:"suggest"`
93+
Label int `json:"label" xml:"label"`
94+
Prob int `json:"prob" xml:"prob"`
95+
}
96+
97+
// MediaCheckAsyncResult 检测结果
98+
type MediaCheckAsyncResult struct {
99+
Suggest string `json:"suggest" xml:"suggest"`
100+
Label int `json:"label" xml:"label"`
101+
}

0 commit comments

Comments
 (0)