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) go RecordWechatLog(msgContent.FromUsername, msgContent.ToUsername, 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 } }