智盟项目
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.

149 lines
4.4 KiB

  1. package hdl
  2. import (
  3. "applet/app/utils"
  4. "encoding/xml"
  5. "fmt"
  6. "log"
  7. "time"
  8. "github.com/gin-gonic/gin"
  9. )
  10. const Token = "temptoken"
  11. // WXCheckSignature 微信接入校验
  12. func WXCheckSignature(c *gin.Context) {
  13. signature := c.Query("signature")
  14. timestamp := c.Query("timestamp")
  15. nonce := c.Query("nonce")
  16. echostr := c.Query("echostr")
  17. ok := utils.CheckSignature(signature, timestamp, nonce, Token)
  18. if !ok {
  19. log.Println("[微信接入] - 微信公众号接入校验失败!")
  20. return
  21. }
  22. log.Println("[微信接入] - 微信公众号接入校验成功!")
  23. _, _ = c.Writer.WriteString(echostr)
  24. }
  25. // WXMsg 微信消息结构体
  26. type WXMsg struct {
  27. ToUserName string
  28. FromUserName string
  29. CreateTime int64
  30. MsgType string
  31. }
  32. // WXTextMsg 微信文本消息结构体
  33. type WXTextMsg struct {
  34. ToUserName string
  35. FromUserName string
  36. CreateTime int64
  37. MsgType string
  38. Content string
  39. MsgId int64
  40. }
  41. // WXEventForSubscribeMsg 扫描带参数二维码事件消息结构体(用户未关注时,进行关注后的事件推送)
  42. type WXEventForSubscribeMsg struct {
  43. ToUserName string //开发者微信号
  44. FromUserName string //发送方帐号(一个OpenID)
  45. CreateTime int64 //消息创建时间 (整型)
  46. MsgType string //消息类型,event
  47. Event string //事件类型,subscribe
  48. EventKey string //事件KEY值,qrscene_为前缀,后面为二维码的参数值
  49. Ticket string //二维码的ticket,可用来换取二维码图片
  50. }
  51. // WXEventForScanMsg 扫描带参数二维码事件消息结构体(用户已关注时的事件推送)
  52. type WXEventForScanMsg struct {
  53. ToUserName string //开发者微信号
  54. FromUserName string //发送方帐号(一个OpenID)
  55. CreateTime int64 //消息创建时间 (整型)
  56. MsgType string //消息类型,event
  57. Event string //事件类型,subscribe
  58. EventKey string //事件KEY值,是一个32位无符号整数,即创建二维码时的二维码scene_id
  59. Ticket string //二维码的ticket,可用来换取二维码图片
  60. }
  61. // WXMsgReceive 微信消息接收
  62. func WXMsgReceive(c *gin.Context) {
  63. var msg WXMsg
  64. err := c.ShouldBindXML(&msg)
  65. if err != nil {
  66. log.Printf("[消息接收] - XML数据包解析失败: %v\n", err)
  67. return
  68. }
  69. log.Printf("[消息接收] - 收到消息, 消息类型为: %s", msg.MsgType)
  70. if msg.MsgType == "event" {
  71. //事件类型消息
  72. var eventMsg WXEventForSubscribeMsg
  73. err := c.ShouldBindXML(&eventMsg)
  74. if err != nil {
  75. log.Printf("[事件类型-消息接收] - XML数据包解析失败: %v\n", err)
  76. return
  77. }
  78. log.Printf("[事件类型]-收到消息, 事件类型为: %s, 事件KEY值为: %s\n, 二维码的ticket值为: %s\n", eventMsg.Event, eventMsg.EventKey, eventMsg.Ticket)
  79. if eventMsg.Event == "subscribe" {
  80. //用户未关注时,进行关注后的事件推送
  81. //userUseQrcodeRecordsDb := db.UserUseQrcodeRecordsDb{}
  82. //userUseQrcodeRecordsDb.Set()
  83. //userUseQrcodeRecordsDb.InsertUserUseQrcodeRecords(model.UserUseQrcodeRecords{
  84. // UserWxOpenId: eventMsg.FromUserName,
  85. // RecordsId: 0,
  86. // State: 0,
  87. // CreateAt: "",
  88. // UpdateAt: "",
  89. //})
  90. }
  91. if eventMsg.Event == "SCAN" {
  92. //用户已关注时的事件推送
  93. }
  94. }
  95. if msg.MsgType == "text" {
  96. //事件类型消息
  97. var textMsg WXTextMsg
  98. err := c.ShouldBindXML(&textMsg)
  99. if err != nil {
  100. log.Printf("[文本消息-消息接收] - XML数据包解析失败: %v\n", err)
  101. return
  102. }
  103. log.Printf("[文本消息]-收到消息, 消息内容为: %s", textMsg.Content)
  104. WXMsgReply(c, textMsg.ToUserName, textMsg.FromUserName)
  105. }
  106. }
  107. // WXRepTextMsg 微信回复文本消息结构体
  108. type WXRepTextMsg struct {
  109. ToUserName string
  110. FromUserName string
  111. CreateTime int64
  112. MsgType string
  113. Content string
  114. // 若不标记XMLName, 则解析后的xml名为该结构体的名称
  115. XMLName xml.Name `xml:"xml"`
  116. }
  117. // WXMsgReply 微信消息回复
  118. func WXMsgReply(c *gin.Context, fromUser, toUser string) {
  119. repTextMsg := WXRepTextMsg{
  120. ToUserName: toUser,
  121. FromUserName: fromUser,
  122. CreateTime: time.Now().Unix(),
  123. MsgType: "text",
  124. Content: fmt.Sprintf("[消息回复] - %s\n,hello, world!", time.Now().Format("2006-01-02 15:04:05")),
  125. }
  126. msg, err := xml.Marshal(&repTextMsg)
  127. if err != nil {
  128. log.Printf("[消息回复] - 将对象进行XML编码出错: %v\n", err)
  129. return
  130. }
  131. _, _ = c.Writer.Write(msg)
  132. }