Browse Source

update

master
DengBiao 1 year ago
parent
commit
372410f373
2 changed files with 68 additions and 65 deletions
  1. +68
    -64
      app/hdl/hdl_wx.go
  2. +0
    -1
      app/router/router.go

+ 68
- 64
app/hdl/hdl_wx.go View File

@@ -5,7 +5,6 @@ import (
"applet/app/db/model"
"applet/app/enum"
"applet/app/md"
"applet/app/svc"
"applet/app/utils"
"encoding/json"
"encoding/xml"
@@ -13,7 +12,6 @@ import (
"fmt"
"log"
"net/http"
"strings"
"time"

"github.com/gin-gonic/gin"
@@ -27,8 +25,14 @@ func WXCheckSignature(c *gin.Context) {
timestamp := c.Query("timestamp")
nonce := c.Query("nonce")
echostr := c.Query("echostr")

ok := utils.CheckSignature(signature, timestamp, nonce, Token)
utils.FilePutContents("WXCheckSignature", utils.SerializeStr(map[string]interface{}{
"signature": signature,
"timestamp": timestamp,
"nonce": nonce,
"echostr": echostr,
"ok": ok,
}))
if !ok {
log.Println("[微信接入] - 微信公众号接入校验失败!")
return
@@ -85,68 +89,68 @@ type WXEventForScanMsg struct {

// WXMsgReceive 微信消息接收
func WXMsgReceive(c *gin.Context) {
log.Printf("[消息接收] - 收到新消息")
var msg WXMsg
err := c.ShouldBindJSON(&msg)
if err != nil {
log.Printf("[消息接收] - XML数据包解析失败: %v\n", err)
return
}
utils.FilePutContents("WXMsgReceive", utils.SerializeStr(msg))
log.Printf("[消息接收] - 收到消息, 消息类型为: %s", msg.MsgType)
if msg.MsgType == "event" {
//事件类型消息
eventMsg := msg
log.Printf("[事件类型]-收到消息, 事件类型为: %s, 事件KEY值为: %s\n, 二维码的ticket值为: %s\n", eventMsg.Event, eventMsg.EventKey, eventMsg.Ticket)
if eventMsg.Event == "subscribe" {
//用户未关注时,进行关注后的事件推送
err := DealCallBack(eventMsg.FromUserName, eventMsg.EventKey)
if err != nil {
WXMsgReply(c, eventMsg.ToUserName, eventMsg.FromUserName, err.Error())
}
}
if eventMsg.Event == "SCAN" {
//用户已关注时的事件推送
err := DealCallBack(eventMsg.FromUserName, eventMsg.EventKey)
if err != nil {
WXMsgReply(c, eventMsg.ToUserName, eventMsg.FromUserName, err.Error())
}
}
if eventMsg.Event == "CLICK" { //公众号点击
if strings.Contains(strings.ToLower(eventMsg.EventKey), "official_account_custom_reply") {
id := strings.ReplaceAll(strings.ToLower(eventMsg.EventKey), "official_account_custom_reply_", "")
var mod model.SysModule
db.Db.Where("mod_id=?", id).Get(&mod)
fmt.Println(mod)
if mod.Data == "" {
fmt.Println(333)
return
}
var data []md.ReplyContent
json.Unmarshal([]byte(mod.Data), &data)
for _, v := range data {
if v.MsgType == "text" {
fmt.Println(1111)
svc.WXMsgTextReply(c, msg.ToUserName, msg.FromUserName, v.Text.Content)
}
if v.MsgType == "image" {
fmt.Println(22222)
svc.WXMsgImageReply(c, msg.ToUserName, msg.FromUserName, v.Image.MediaId)
}
}
}
}
}
if msg.MsgType == "text" {
//文本类型消息
textMsg := msg
log.Printf("[文本消息]-收到消息, 消息内容为: %s", textMsg.Content)
WXMsgReply(c, textMsg.ToUserName, textMsg.FromUserName, textMsg.Content)
}
//log.Printf("[消息接收] - 收到新消息")
//var msg WXMsg
//err := c.ShouldBindXML(&msg)
//if err != nil {
// log.Printf("[消息接收] - XML数据包解析失败: %v\n", err)
// return
//}
//utils.FilePutContents("WXMsgReceive", utils.SerializeStr(msg))
//log.Printf("[消息接收] - 收到消息, 消息类型为: %s", msg.MsgType)
//if msg.MsgType == "event" {
// //事件类型消息
// eventMsg := msg
// log.Printf("[事件类型]-收到消息, 事件类型为: %s, 事件KEY值为: %s\n, 二维码的ticket值为: %s\n", eventMsg.Event, eventMsg.EventKey, eventMsg.Ticket)
// if eventMsg.Event == "subscribe" {
// //用户未关注时,进行关注后的事件推送
// err := DealCallBack(eventMsg.FromUserName, eventMsg.EventKey)
// if err != nil {
// WXMsgReply(c, eventMsg.ToUserName, eventMsg.FromUserName, err.Error())
// }
// }
// if eventMsg.Event == "SCAN" {
// //用户已关注时的事件推送
// err := DealCallBack(eventMsg.FromUserName, eventMsg.EventKey)
// if err != nil {
// WXMsgReply(c, eventMsg.ToUserName, eventMsg.FromUserName, err.Error())
// }
// }
// if eventMsg.Event == "CLICK" { //公众号点击
// if strings.Contains(strings.ToLower(eventMsg.EventKey), "official_account_custom_reply") {
// id := strings.ReplaceAll(strings.ToLower(eventMsg.EventKey), "official_account_custom_reply_", "")
// var mod model.SysModule
// db.Db.Where("mod_id=?", id).Get(&mod)
// fmt.Println(mod)
// if mod.Data == "" {
// fmt.Println(333)
//
// return
// }
// var data []md.ReplyContent
// json.Unmarshal([]byte(mod.Data), &data)
// for _, v := range data {
// if v.MsgType == "text" {
// fmt.Println(1111)
//
// svc.WXMsgTextReply(c, msg.ToUserName, msg.FromUserName, v.Text.Content)
// }
// if v.MsgType == "image" {
// fmt.Println(22222)
// svc.WXMsgImageReply(c, msg.ToUserName, msg.FromUserName, v.Image.MediaId)
// }
// }
// }
// }
//}
//if msg.MsgType == "text" {
// //文本类型消息
// textMsg := msg
// log.Printf("[文本消息]-收到消息, 消息内容为: %s", textMsg.Content)
// WXMsgReply(c, textMsg.ToUserName, textMsg.FromUserName, textMsg.Content)
//}

c.String(http.StatusOK, "")
c.JSON(http.StatusOK, "")
return
}



+ 0
- 1
app/router/router.go View File

@@ -49,7 +49,6 @@ func route(r *gin.RouterGroup) {
r.POST("/img_upload", hdl.ImgUpload)
r.Group("/wx")
{
r.Use(mw.DB)
// 微信公众号消息通知
r.GET("/msgReceive", hdl.WXCheckSignature)
r.POST("/msgReceive", hdl.WXMsgReceive)


Loading…
Cancel
Save