/
weixin.go
182 lines (165 loc) · 5.19 KB
/
weixin.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
package controllers
import (
"context"
"encoding/xml"
"fmt"
"github.com/beego/beego/v2/core/logs"
"github.com/beego/beego/v2/server/web"
"github.com/lifei6671/douyinbot/admin/service"
"github.com/lifei6671/douyinbot/wechat"
"github.com/lifei6671/fink-download/fink"
"strings"
"time"
)
var (
token = web.AppConfig.DefaultString("wechattoken", "")
key = web.AppConfig.DefaultString("wechatencodingaeskey", "")
appId = web.AppConfig.DefaultString("wechatappid", "")
autoReplyContent = "回复关键字:\n1:注册账号\n2:绑定百度网盘"
)
type WeiXinController struct {
web.Controller
wx *wechat.WeiXin
body *wechat.TextRequestBody
domain string
}
func (c *WeiXinController) Prepare() {
c.wx = wechat.NewWeiXin(appId, token, key)
c.domain = c.Ctx.Input.Scheme() + "://" + c.Ctx.Input.Host()
}
// Index 验证是否是微信请求
func (c *WeiXinController) Index() {
timestamp := c.Ctx.Input.Query("timestamp")
signature := c.Ctx.Input.Query("signature")
nonce := c.Ctx.Input.Query("nonce")
echoStr := c.Ctx.Input.Query("echoStr")
signatureGen := c.wx.MakeSignature(timestamp, nonce)
if signatureGen == signature {
_ = c.Ctx.Output.Body([]byte(echoStr))
} else {
_ = c.Ctx.Output.Body([]byte("false"))
}
c.StopRun()
}
func (c *WeiXinController) Dispatch() {
encryptType := c.Ctx.Input.Query("encrypt_type")
msgSignature := c.Ctx.Input.Query("msg_signature")
nonce := c.Ctx.Input.Query("nonce")
timestamp := c.Ctx.Input.Query("timestamp")
logs.Info("微信请求 ->", string(c.Ctx.Input.RequestBody))
if encryptType == wechat.EncryptTypeAES {
requestBody := &wechat.EncryptRequestBody{}
if err := xml.Unmarshal(c.Ctx.Input.RequestBody, requestBody); err != nil {
logs.Error("解析微信消息失败 -> %+v", err)
_ = c.Ctx.Output.Body([]byte("success"))
c.StopRun()
return
}
if !c.wx.ValidateMsg(timestamp, nonce, requestBody.Encrypt, msgSignature) {
logs.Error("解析微信消息失败 -> %+v", msgSignature)
_ = c.Ctx.Output.Body([]byte("success"))
c.StopRun()
return
}
var err error
c.body, err = c.wx.ParseEncryptRequestBody(timestamp, nonce, msgSignature, c.Ctx.Input.RequestBody)
if err != nil {
logs.Error("解析微信消息失败 -> %+v", err)
_ = c.Ctx.Output.Body([]byte("success"))
c.StopRun()
return
}
} else {
var textRequestBody wechat.TextRequestBody
err := xml.Unmarshal(c.Ctx.Input.RequestBody, &textRequestBody)
if err != nil {
logs.Error("解析微信消息失败 -> %+v", msgSignature)
_ = c.Ctx.Output.Body([]byte("success"))
c.StopRun()
return
}
c.body = &textRequestBody
}
if c.body.MsgType == string(wechat.WeiXinTextMsgType) {
if c.body.Content == "" {
_ = c.response("解析消息失败")
return
}
if handler := service.GetHandler(c.body.Content); handler != nil {
if resp, err := handler(c.body); err != nil {
_ = c.response("处理失败")
} else {
c.responseBody(resp)
}
return
}
if err := service.Register(c.body.Content, c.body.FromUserName); err != service.ErrNoUserRegister {
if err != nil {
_ = c.response(err.Error())
} else {
_ = c.response("注册成功")
}
return
}
if i := strings.Index(c.body.Content,"www.finkapp.cn"); i >= 0 {
fink.Push(c.body.Content)
} else {
service.Push(context.Background(), service.MediaContent{
Content: c.body.Content,
UserId: c.body.FromUserName,
})
}
_ = c.response("处理成功")
return
} else if c.body.MsgType == string(wechat.WeiXinEventMsgType) {
//如果是推送的订阅事件
if c.body.Event == wechat.WeiXinSubscribeEvent {
_ = c.response(autoReplyContent)
}
}
_ = c.response("不支持的消息类型")
}
func (c *WeiXinController) responseBody(resp wechat.PassiveUserReplyMessage) {
nonce := c.Ctx.Input.Query("nonce")
timestamp := c.Ctx.Input.Query("timestamp")
encryptType := c.Ctx.Input.Query("encrypt_type")
if encryptType == wechat.EncryptTypeAES {
c.Data["xml"] = resp
_ = c.ServeXML()
} else {
body, err := c.wx.MakeEncryptResponseBody(resp.FromUserName.Text, resp.ToUserName.Text, resp.Content.Text, nonce, timestamp)
if err != nil {
logs.Error("解析微信消息失败 -> %+v", resp)
_ = c.Ctx.Output.Body([]byte("success"))
c.StopRun()
}
err = c.Ctx.Output.Body(body)
}
c.StopRun()
}
func (c *WeiXinController) response(content string) error {
nonce := c.Ctx.Input.Query("nonce")
timestamp := c.Ctx.Input.Query("timestamp")
encryptType := c.Ctx.Input.Query("encrypt_type")
if encryptType == wechat.EncryptTypeAES {
c.Data["xml"] = wechat.PassiveUserReplyMessage{
ToUserName: wechat.Value(c.body.FromUserName),
FromUserName: wechat.Value(c.body.ToUserName),
CreateTime: wechat.Value(fmt.Sprintf("%d", time.Now().Unix())),
MsgType: wechat.Value(string(wechat.WeiXinTextMsgType)),
Content: wechat.Value(content),
}
return c.ServeXML()
} else {
body, err := c.wx.MakeEncryptResponseBody(c.body.ToUserName, c.body.FromUserName, content, nonce, timestamp)
if err != nil {
logs.Error("解析微信消息失败 -> %+v", c.body)
_ = c.Ctx.Output.Body([]byte("success"))
c.StopRun()
return err
}
err = c.Ctx.Output.Body(body)
c.StopRun()
return err
}
}