You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
68 lines
1.6 KiB
68 lines
1.6 KiB
package handle
|
|
|
|
import (
|
|
"WechatGateWay/global"
|
|
"encoding/xml"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
)
|
|
|
|
func handleMessage(w http.ResponseWriter, r *http.Request) {
|
|
msgSignature := r.URL.Query().Get("msg_signature") //消息签名
|
|
timestamp := r.URL.Query().Get("timestamp")
|
|
nonce := r.URL.Query().Get("nonce") //随机数,由企业自行生成
|
|
body, err := io.ReadAll(r.Body)
|
|
if err != nil {
|
|
log.Println("读取Body错误", err.Error())
|
|
return
|
|
} else {
|
|
msg, err := global.WxCrypt.DecryptMsg(msgSignature, timestamp, nonce, body)
|
|
if err != nil {
|
|
log.Println("解密消息Body错误", err.ErrMsg)
|
|
return
|
|
} else {
|
|
var msgContent MsgContent
|
|
err := xml.Unmarshal(msg, &msgContent)
|
|
if err != nil {
|
|
log.Println("反序列化错误")
|
|
return
|
|
} else {
|
|
log.Println("消息验证成功:", msgContent)
|
|
//event消息一直在 暂时不入库
|
|
if msgContent.MsgType != "event" {
|
|
go RecordWechatLog(msgContent.ToUsername, msgContent.FromUsername, msgContent.CreateTime, msgContent.MsgType, msgContent.Content)
|
|
}
|
|
if global.EnableReply {
|
|
Reply(msgContent, timestamp, nonce, w)
|
|
} else {
|
|
return
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func Reply(msgContent MsgContent, timestamp, nonce string, w http.ResponseWriter) {
|
|
switch msgContent.MsgType {
|
|
case "text":
|
|
log.Println("接收到文本消息")
|
|
replyText(msgContent, timestamp, nonce, w)
|
|
case "image":
|
|
log.Println("收到图片消息")
|
|
replyImage(msgContent, timestamp, nonce, w)
|
|
return
|
|
case "voice":
|
|
log.Println("收到语音消息")
|
|
return
|
|
case "event":
|
|
log.Println("收到事件消息")
|
|
return
|
|
case "location":
|
|
log.Println("收到地理位置消息")
|
|
return
|
|
}
|
|
|
|
}
|