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

69 lines
1.7 KiB

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