|
@@ -3,8 +3,10 @@ package controllers
|
3
|
3
|
import (
|
4
|
4
|
"encoding/base64"
|
5
|
5
|
"encoding/json"
|
|
6
|
+ "encoding/xml"
|
6
|
7
|
"io/ioutil"
|
7
|
8
|
"net/http"
|
|
9
|
+ "strconv"
|
8
|
10
|
"wechat-conf/models"
|
9
|
11
|
"wechat-conf/models/model"
|
10
|
12
|
"wechat-conf/service/autoreply"
|
|
@@ -150,6 +152,8 @@ func (c *WechatController) ComponentPush() {
|
150
|
152
|
// 删除wechatConf信息,同时将org解绑
|
151
|
153
|
appid := msg["AuthorizerAppid"]
|
152
|
154
|
c.wechatServ.UnAuthorized(appid)
|
|
155
|
+ // 删除第三方中的微信信息
|
|
156
|
+ utils.Component.DelWxClient(appid)
|
153
|
157
|
break
|
154
|
158
|
}
|
155
|
159
|
c.ResponseRaw([]byte("success"))
|
|
@@ -168,17 +172,50 @@ func (c *WechatController) WechatInfo() {
|
168
|
172
|
|
169
|
173
|
// WxReceive 微信消息接收
|
170
|
174
|
func (c *WechatController) WxReceive() {
|
|
175
|
+ beego.Error("微信消息接入")
|
171
|
176
|
appid := c.GetString(":appid")
|
172
|
177
|
wechat, err := utils.Component.GetWxClient(appid)
|
173
|
178
|
if err != nil {
|
174
|
179
|
utils.LogError("获取微信失败: " + err.Error())
|
175
|
180
|
}
|
176
|
|
- val, err := wechat.TransformMessage(string(c.Ctx.Input.RequestBody))
|
|
181
|
+ msg, err := wechat.TransformMessage(string(c.Ctx.Input.RequestBody))
|
177
|
182
|
if err != nil {
|
178
|
183
|
utils.LogError("读取微信服务发送内容失败: " + err.Error())
|
179
|
184
|
c.ResponseRaw([]byte(""))
|
180
|
185
|
}
|
|
186
|
+ xp := &core.XMLParse{}
|
|
187
|
+ beego.Error("消息:", msg)
|
|
188
|
+ encrypt := msg["Encrypt"]
|
|
189
|
+ conf, err := c.wechatServ.GetComponentInfo()
|
|
190
|
+ if err != nil || conf == nil || conf.Appid == "" {
|
|
191
|
+ utils.LogError("读取微信配置文件失败")
|
|
192
|
+ c.ResponseRaw([]byte("success"))
|
|
193
|
+ }
|
|
194
|
+ EncodingAESKey := conf.Aeskey
|
181
|
195
|
|
|
196
|
+ AESKey, err := base64.StdEncoding.DecodeString(EncodingAESKey + "=")
|
|
197
|
+ if err != nil {
|
|
198
|
+ utils.LogError("DecodeString失败:", err)
|
|
199
|
+ c.ResponseRaw([]byte("success"))
|
|
200
|
+ }
|
|
201
|
+ EncryptVal, err := base64.StdEncoding.DecodeString(encrypt)
|
|
202
|
+ if err != nil {
|
|
203
|
+ utils.LogError("密文base64解析", err)
|
|
204
|
+ c.ResponseRaw([]byte("success"))
|
|
205
|
+ }
|
|
206
|
+ msgbyte, err := utils.AesDecrypt(EncryptVal, AESKey)
|
|
207
|
+ if err != nil {
|
|
208
|
+ utils.LogError("解密失败:", err)
|
|
209
|
+ c.ResponseRaw([]byte("success"))
|
|
210
|
+ }
|
|
211
|
+ utils.LogError("解密成功")
|
|
212
|
+ // 解析xml
|
|
213
|
+ val, err := xp.Parse(string(msgbyte))
|
|
214
|
+ if err != nil {
|
|
215
|
+ utils.LogError("xml解析失败:", err)
|
|
216
|
+ c.ResponseRaw([]byte("success"))
|
|
217
|
+ }
|
|
218
|
+ utils.LogError("xml解析成功:", val)
|
182
|
219
|
var replay = new(model.TaAutoReply)
|
183
|
220
|
switch val["MsgType"] {
|
184
|
221
|
case "text":
|
|
@@ -196,24 +233,52 @@ func (c *WechatController) WxReceive() {
|
196
|
233
|
utils.LogError("获取微信自动回复信息失败: " + err.Error())
|
197
|
234
|
c.ResponseRaw([]byte(""))
|
198
|
235
|
}
|
199
|
|
-
|
200
|
236
|
if replay == nil {
|
201
|
237
|
c.ResponseRaw([]byte(""))
|
202
|
238
|
}
|
203
|
239
|
openID := val["FromUserName"]
|
204
|
|
- message, err := c.getReplayMessage(replay, openID, wechat)
|
|
240
|
+ signature := c.GetString("msg_signature")
|
|
241
|
+ nonce := c.GetString("nonce")
|
|
242
|
+ timestamp, _ := strconv.Atoi(c.GetString("timestamp"))
|
|
243
|
+ beego.Error("signature:", signature, ",nonce:", nonce, ",timestamp:", timestamp)
|
|
244
|
+ message, err := c.getReplayMessage(replay, openID, AESKey, wechat)
|
205
|
245
|
if err != nil {
|
206
|
246
|
utils.LogError("转换回复信息失败: " + err.Error())
|
207
|
247
|
c.ResponseRaw([]byte(""))
|
208
|
248
|
}
|
209
|
|
- c.ResponseRaw(message, false)
|
|
249
|
+ // message, err = utils.AesEncrypt(message, AESKey)
|
|
250
|
+ // if err != nil {
|
|
251
|
+ // utils.LogError("加密失败:", err)
|
|
252
|
+ // c.ResponseRaw([]byte(""))
|
|
253
|
+ // }
|
|
254
|
+ // beego.Error(string(message))
|
|
255
|
+ // data := PassiveMessage{
|
|
256
|
+ // Encrypt: component.CDATA{string(message)},
|
|
257
|
+ // MsgSignature: component.CDATA{signature},
|
|
258
|
+ // TimeStamp: int64(timestamp),
|
|
259
|
+ // Nonce: component.CDATA{nonce},
|
|
260
|
+ // }
|
|
261
|
+ // beego.Error(data)
|
|
262
|
+ // res, err := xml.Marshal(data)
|
|
263
|
+ // beego.Error(err)
|
|
264
|
+ // beego.Error(string(res))
|
|
265
|
+ c.ResponseRaw(message)
|
210
|
266
|
c.DestroyContext()
|
211
|
267
|
c.StopRun()
|
212
|
268
|
}
|
213
|
269
|
|
214
|
|
-func (c *WechatController) getReplayMessage(replay *model.TaAutoReply, openid string, wx *component.WxClient) ([]byte, error) {
|
|
270
|
+// PassiveMessage 加密后的返回信息
|
|
271
|
+type PassiveMessage struct {
|
|
272
|
+ XMLName xml.Name `xml:"xml"`
|
|
273
|
+ Encrypt component.CDATA `xml:"Encrypt"`
|
|
274
|
+ MsgSignature component.CDATA `xml:"MsgSignature"`
|
|
275
|
+ TimeStamp int64 `xml:"TimeStamp"`
|
|
276
|
+ Nonce component.CDATA `xml:"Nonce"`
|
|
277
|
+}
|
|
278
|
+
|
|
279
|
+func (c *WechatController) getReplayMessage(replay *model.TaAutoReply, openid string, Aeskey []byte, wx *component.WxClient) ([]byte, error) {
|
215
|
280
|
switch replay.MessageType {
|
216
|
|
- case models.MESSAGE_TYPE_CONTENT:
|
|
281
|
+ case models.MESSAGE_TYPE_PARAGRAPH:
|
217
|
282
|
msg, err := wx.ResponseMessageText(openid, replay.MessageParagraph)
|
218
|
283
|
return msg, err
|
219
|
284
|
case models.MESSAGE_TYPE_IMG:
|