package wechatcallback import ( "fmt" "key_performance_indicators/middleware/wechatapp/wechatstatice" "key_performance_indicators/models/wechatcallback" "key_performance_indicators/overall" "key_performance_indicators/overall/publicmethod" "strconv" "time" "github.com/gin-gonic/gin" ) // 企业微信回调入口 func (a *ApiRouter) Index(c *gin.Context) { outputCont := publicmethod.MapOut[string]() outputCont["index"] = "企业微信回调入口" publicmethod.Result(0, outputCont, c) } /* * @ 作者: 秦东 @ 时间: 2022-09-27 11:33:29 @ 功能: 回调入口 @ 参数 #MsgSignature 企业微信加密签名,msg_signature计算结合了企业填写的token、请求中的timestamp、nonce、加密的消息体。 #Timestamp 时间戳。与nonce结合使用,用于防止请求重放攻击。 #Nonce 随机数。与timestamp结合使用,用于防止请求重放攻击。 #Echostr 加密的字符串。需要解密得到消息内容明文,解密后有random、msg_len、msg、receiveid四个字段,其中msg即为消息内容明文 @ 返回值 # */ func (a *ApiRouter) CallbackMessageApi(c *gin.Context) { MsgSignature := c.Query("msg_signature") //企业微信加密签名,msg_signature计算结合了企业填写的token、请求中的timestamp、nonce、加密的消息体。 Timestamp := c.Query("timestamp") //时间戳。与nonce结合使用,用于防止请求重放攻击。 Nonce := c.Query("nonce") //随机数。与timestamp结合使用,用于防止请求重放攻击。 Echostr := c.Query("echostr") //加密的字符串。需要解密得到消息内容明文,解密后有random、msg_len、msg、receiveid四个字段,其中msg即为消息内容明文 EchostrType := c.Query("type") SystemApp := c.Query("systemapp") if EchostrType == "" { EchostrType = "json" } if SystemApp == "" { SystemApp = "kpi" } // fmt.Printf("(1)SystemApp---------->%v--->EchostrType---------->%v--->MsgSignature---------->%v--->Timestamp---------->%v--->Nonce---------->%v--->Echostr---------->%v\n", SystemApp, EchostrType, MsgSignature, Timestamp, Nonce, Echostr) var basicValueCallback CallBackData //企业微信回调基础参数 basicValueCallback.MsgSignature = MsgSignature basicValueCallback.Timestamp = Timestamp basicValueCallback.Nonce = Nonce basicValueCallback.DataType = EchostrType basicValueCallback.SystemApp = SystemApp var msgStr string if Echostr != "" { //Api地址验证 basicValueCallback.Echostr = Echostr msgStr = basicValueCallback.VerificationUrl() c.String(200, msgStr) } else { //回调事件 fmt.Printf("回调事件") } // fmt.Printf("(3)CallbackMessageApi---------->%v------------------->%v\n", msgStr, basicValueCallback) // publicmethod.Result(1, basicValueCallback, c, msgStr) } // 验证URL func (c *CallBackData) VerificationUrl() (msg string) { // wecahtCpt := WechatVerification() // timestampStr := strconv.FormatInt(c.Timestamp, 10) // echoStr, cryptErr := wecahtCpt.VerifyURL(c.MsgSignature, timestampStr, c.Nonce, c.Echostr) // if nil != cryptErr { // fmt.Println("verifyUrl fail", cryptErr) // } // msg = string(echoStr) // jsonk, _ := json.Marshal(c) // fmt.Printf("json--------->%v\n", string(jsonk)) switch c.DataType { case "json": wxcptJson := wechatstatice.WechatDecryptJson(c.SystemApp) echoStr, cryptErr := wxcptJson.VerifyURL(c.MsgSignature, c.Timestamp, c.Nonce, c.Echostr) fmt.Printf("(2)wxcptJson---------->%v------------echoStr------->%v----cryptErr---->%v-----MsgSignature--->%v----Timestamp---->%v-----Nonce--->%v-----Echostr--->%v\n", wxcptJson, string(echoStr), cryptErr, c.MsgSignature, c.Timestamp, c.Nonce, c.Echostr) msg = string(echoStr) var callbackLog wechatcallback.CallbackLog callbackLog.MsgSignature = c.MsgSignature TimestampInt, _ := strconv.ParseInt(c.Timestamp, 10, 64) callbackLog.TimeStamp = TimestampInt callbackLog.Nonce = c.Nonce callbackLog.Echostr = c.Echostr callbackLog.Xmlstr = string(echoStr) // callbackLog.Reqdata = string(reqData) callbackLog.AddTime = time.Now().Unix() overall.CONSTANT_DB_WECHAT_LOG.Create(&callbackLog) default: wxcptXml := wechatstatice.WechatDecryptXml(c.SystemApp) echoStr, cryptErr := wxcptXml.VerifyURL(c.MsgSignature, c.Timestamp, c.Nonce, c.Echostr) fmt.Printf("wxcptXml---------->%v------------echoStr------->%v----cryptErr---->%v-----MsgSignature--->%v----Timestamp---->%v-----Nonce--->%v-----Echostr--->%v\n", wxcptXml, string(echoStr), cryptErr, c.MsgSignature, c.Timestamp, c.Nonce, c.Echostr) msg = string(echoStr) var callbackLog wechatcallback.CallbackLog callbackLog.MsgSignature = c.MsgSignature TimestampInt, _ := strconv.ParseInt(c.Timestamp, 10, 64) callbackLog.TimeStamp = TimestampInt callbackLog.Nonce = c.Nonce callbackLog.Echostr = c.Echostr callbackLog.Xmlstr = string(echoStr) // callbackLog.Reqdata = string(reqData) callbackLog.AddTime = time.Now().Unix() overall.CONSTANT_DB_WECHAT_LOG.Create(&callbackLog) } return // fmt.Println(string(echoStr)) // fmt.Print(string(echoStr)) }