package svc import ( "applet/app/utils" "encoding/xml" "fmt" "github.com/gin-gonic/gin" "log" "time" ) // WXRepTextMsg 微信回复文本消息结构体 type WXRepTextMsg struct { ToUserName string FromUserName string CreateTime int64 MsgType string Content string // 若不标记XMLName, 则解析后的xml名为该结构体的名称 XMLName xml.Name `xml:"xml"` } // WXRepImageMsg 微信回复图片消息结构体 type WXRepImageMsg struct { ToUserName string FromUserName string CreateTime int64 MsgType string Image struct { MediaId string } // 若不标记XMLName, 则解析后的xml名为该结构体的名称 XMLName xml.Name `xml:"xml"` } // WXMsgTextReply 微信消息回复 func WXMsgTextReply(c *gin.Context, fromUser, toUser, content string) { repTextMsg := WXRepTextMsg{ ToUserName: toUser, FromUserName: fromUser, CreateTime: time.Now().Unix(), MsgType: "text", Content: content, } fmt.Println(utils.SerializeStr(repTextMsg)) msg, err := xml.Marshal(&repTextMsg) if err != nil { log.Printf("[消息回复] - 将对象进行XML编码出错: %v\n", err) return } _, _ = c.Writer.Write(msg) } func WXMsgImageReply(c *gin.Context, fromUser, toUser, content string) { repTextMsg := WXRepImageMsg{ ToUserName: toUser, FromUserName: fromUser, CreateTime: time.Now().Unix(), MsgType: "image", Image: struct{ MediaId string }{MediaId: content}, } fmt.Println(utils.SerializeStr(repTextMsg)) msg, err := xml.Marshal(&repTextMsg) if err != nil { log.Printf("[消息回复] - 将对象进行XML编码出错: %v\n", err) return } _, _ = c.Writer.Write(msg) }