-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_2.go
190 lines (158 loc) · 4.2 KB
/
main_2.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package main
import (
"bytes"
"encoding/xml"
"fmt"
"github.com/gin-gonic/gin"
"github.com/wechatDemo/util"
"io/ioutil"
"log"
"time"
)
// 与填写的服务器配置中的Token一致
const Token = "nXdJfEgxFbqUyZb7"
func Testmain() {
router := gin.Default()
router.GET("/wx", WXCheckSignature)
router.POST("/wx", WXReceive)
log.Fatalln(router.Run(":11080"))
}
// WXCheckSignature 微信接入校验
func WXCheckSignature(c *gin.Context) {
signature := c.Query("signature")
timestamp := c.Query("timestamp")
nonce := c.Query("nonce")
echostr := c.Query("echostr")
ok := util.CheckSignature(signature, timestamp, nonce, Token)
if !ok {
log.Println("微信公众号接入校验失败!")
return
}
log.Println("微信公众号接入校验成功!")
_, _ = c.Writer.WriteString(echostr)
}
// WXTextMsg 微信文本消息结构体
type WXTextMsg struct {
ToUserName string
FromUserName string
CreateTime int64
MsgType string
Content string
MsgId int64
}
// WXMsgReceive 微信消息接收
func WXReceive(c *gin.Context) {
type Content struct {
MsgType string
}
reqBody, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
log.Printf("[消息接收] - c.Request.Body读取失败: %v\n", err)
return
}
// restore Body
c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(reqBody))
var content Content
err = xml.Unmarshal(reqBody, &content)
if err != nil {
log.Printf("[消息接收] - XML数据包解析失败: %v\n", err)
return
}
if content.MsgType == "text" {
WXMsgReceive(c)
}else if content.MsgType == "image"{
WXImgReceive(c)
}else {
log.Printf("MsgType:%v not support", content.MsgType)
return
}
}
// WXMsgReceive 微信消息接收
func WXMsgReceive(c *gin.Context) {
var textMsg WXTextMsg
err := c.ShouldBindXML(&textMsg)
if err != nil {
log.Printf("[消息接收] - XML数据包解析失败: %v\n", err)
return
}
log.Printf("[消息接收] - 收到消息, 消息类型为: %s, 消息内容为: %s\n", textMsg.MsgType, textMsg.Content)
// 对接收的消息进行被动回复
WXMsgReply(c, textMsg.ToUserName, textMsg.FromUserName)
}
// WXRepTextMsg 微信回复文本消息结构体
type WXRepTextMsg struct {
ToUserName string
FromUserName string
CreateTime int64
MsgType string
Content string
// 若不标记XMLName, 则解析后的xml名为该结构体的名称
XMLName xml.Name `xml:"xml"`
}
// WXMsgReply 微信消息回复
func WXMsgReply(c *gin.Context, fromUser, toUser string) {
repTextMsg := WXRepTextMsg{
ToUserName: toUser,
FromUserName: fromUser,
CreateTime: time.Now().Unix(),
MsgType: "text",
Content: fmt.Sprintf("[消息回复] - %s", time.Now().Format("2006-01-02 15:04:05")),
}
msg, err := xml.Marshal(&repTextMsg)
fmt.Print(string(msg))
if err != nil {
log.Printf("[消息回复] - 将对象进行XML编码出错: %v\n", err)
return
}
_, _ = c.Writer.Write(msg)
}
func WXImgReceive(c *gin.Context) {
var msg WXImgMsg
err := c.ShouldBindXML(&msg)
if err != nil {
log.Printf("[消息接收] - XML数据包解析失败: %v\n", err)
return
}
log.Printf("[消息接收] - 收到消息, 消息类型为: %s, MediaId: %s\n", msg.MsgType, msg.MediaId)
// 对接收的消息进行被动回复
WXImgReply(c, msg.ToUserName, msg.FromUserName, msg.MediaId)
}
// WXMsgReply 微信消息回复
func WXImgReply(c *gin.Context, fromUser, toUser, MediaId string) {
repMsg := WXRepImgMsg{
ToUserName: toUser,
FromUserName: fromUser,
CreateTime: time.Now().Unix(),
MsgType: "image",
Image:Image{MediaId:MediaId},
}
msg, err := xml.Marshal(&repMsg)
fmt.Print(string(msg))
if err != nil {
log.Printf("[消息回复] - 将对象进行XML编码出错: %v\n", err)
return
}
_, _ = c.Writer.Write(msg)
}
// WXTextMsg 微信文本消息结构体
type WXImgMsg struct {
ToUserName string
FromUserName string
CreateTime int64
MsgType string
PicUrl string
MediaId string
MsgId int64
}
type Image struct {
MediaId string
}
type WXRepImgMsg struct {
ToUserName string
FromUserName string
CreateTime int64
MsgType string
Image Image
// 若不标记XMLName, 则解析后的xml名为该结构体的名称
XMLName xml.Name `xml:"xml"`
}