一物一码
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.
 
 
 
 
 
 

67 lines
1.5 KiB

  1. package svc
  2. import (
  3. "encoding/xml"
  4. "github.com/gin-gonic/gin"
  5. "log"
  6. "time"
  7. )
  8. // WXRepTextMsg 微信回复文本消息结构体
  9. type WXRepTextMsg struct {
  10. ToUserName string
  11. FromUserName string
  12. CreateTime int64
  13. MsgType string
  14. Content string
  15. // 若不标记XMLName, 则解析后的xml名为该结构体的名称
  16. XMLName xml.Name `xml:"xml"`
  17. }
  18. // WXRepImageMsg 微信回复图片消息结构体
  19. type WXRepImageMsg struct {
  20. ToUserName string
  21. FromUserName string
  22. CreateTime int64
  23. MsgType string
  24. Image struct {
  25. MediaId string
  26. }
  27. // 若不标记XMLName, 则解析后的xml名为该结构体的名称
  28. XMLName xml.Name `xml:"xml"`
  29. }
  30. // WXMsgTextReply 微信消息回复
  31. func WXMsgTextReply(c *gin.Context, fromUser, toUser, content string) {
  32. repTextMsg := WXRepTextMsg{
  33. ToUserName: toUser,
  34. FromUserName: fromUser,
  35. CreateTime: time.Now().Unix(),
  36. MsgType: "text",
  37. Content: content,
  38. }
  39. msg, err := xml.Marshal(&repTextMsg)
  40. if err != nil {
  41. log.Printf("[消息回复] - 将对象进行XML编码出错: %v\n", err)
  42. return
  43. }
  44. _, _ = c.Writer.Write(msg)
  45. }
  46. func WXMsgImageReply(c *gin.Context, fromUser, toUser, content string) {
  47. repTextMsg := WXRepImageMsg{
  48. ToUserName: toUser,
  49. FromUserName: fromUser,
  50. CreateTime: time.Now().Unix(),
  51. MsgType: "image",
  52. Image: struct{ MediaId string }{MediaId: content},
  53. }
  54. msg, err := xml.Marshal(&repTextMsg)
  55. if err != nil {
  56. log.Printf("[消息回复] - 将对象进行XML编码出错: %v\n", err)
  57. return
  58. }
  59. _, _ = c.Writer.Write(msg)
  60. }