message.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package message
  2. import "encoding/xml"
  3. //MsgType 基本消息类型
  4. type MsgType string
  5. //EventType 事件类型
  6. type EventType string
  7. const (
  8. //MsgTypeText 表示文本消息
  9. MsgTypeText MsgType = "text"
  10. //MsgTypeImage 表示图片消息
  11. MsgTypeImage = "image"
  12. //MsgTypeVoice 表示语音消息
  13. MsgTypeVoice = "voice"
  14. //MsgTypeVideo 表示视频消息
  15. MsgTypeVideo = "video"
  16. //MsgTypeShortVideo 表示短视频消息[限接收]
  17. MsgTypeShortVideo = "shortvideo"
  18. //MsgTypeLocation 表示坐标消息[限接收]
  19. MsgTypeLocation = "location"
  20. //MsgTypeLink 表示链接消息[限接收]
  21. MsgTypeLink = "link"
  22. //MsgTypeMusic 表示音乐消息[限回复]
  23. MsgTypeMusic = "music"
  24. //MsgTypeNews 表示图文消息[限回复]
  25. MsgTypeNews = "news"
  26. //MsgTypeTransfer 表示消息消息转发到客服
  27. MsgTypeTransfer = "transfer_customer_service"
  28. )
  29. const (
  30. //EventSubscribe 订阅
  31. EventSubscribe EventType = "subscribe"
  32. //EventUnsubscribe 取消订阅
  33. EventUnsubscribe EventType = "unsubscribe"
  34. //EventScan 用户已经关注公众号,则微信会将带场景值扫描事件推送给开发者
  35. EventScan EventType = "SCAN"
  36. //EventLocation 上报地理位置事件
  37. EventLocation EventType = "LOCATION"
  38. //EventClick 点击菜单拉取消息时的事件推送
  39. EventClick EventType = "CLICK"
  40. //EventView 点击菜单跳转链接时的事件推送
  41. EventView EventType = "VIEW"
  42. )
  43. //MixMessage 存放所有微信发送过来的消息和事件
  44. type MixMessage struct {
  45. CommonToken
  46. //基本消息
  47. MsgID int64 `xml:"MsgId"`
  48. Content string `xml:"Content"`
  49. PicURL string `xml:"PicUrl"`
  50. MediaID string `xml:"MediaId"`
  51. Format string `xml:"Format"`
  52. ThumbMediaID string `xml:"ThumbMediaId"`
  53. LocationX float64 `xml:"Location_X"`
  54. LocationY float64 `xml:"Location_Y"`
  55. Scale float64 `xml:"Scale"`
  56. Label string `xml:"Label"`
  57. Title string `xml:"Title"`
  58. Description string `xml:"Description"`
  59. URL string `xml:"Url"`
  60. //事件相关
  61. Event string `xml:"Event"`
  62. EventKey string `xml:"EventKey"`
  63. Ticket string `xml:"Ticket"`
  64. Latitude string `xml:"Latitude"`
  65. Longitude string `xml:"Longitude"`
  66. Precision string `xml:"Precision"`
  67. MenuID string `xml:"MenuId"`
  68. ScanCodeInfo struct {
  69. ScanType string `xml:"ScanType"`
  70. ScanResult string `xml:"ScanResult"`
  71. } `xml:"ScanCodeInfo"`
  72. SendPicsInfo struct {
  73. Count int32 `xml:"Count"`
  74. PicList []EventPic `xml:"PicList>item"`
  75. } `xml:"SendPicsInfo"`
  76. SendLocationInfo struct {
  77. LocationX float64 `xml:"Location_X"`
  78. LocationY float64 `xml:"Location_Y"`
  79. Scale float64 `xml:"Scale"`
  80. Label string `xml:"Label"`
  81. Poiname string `xml:"Poiname"`
  82. }
  83. }
  84. //EventPic 发图事件推送
  85. type EventPic struct {
  86. PicMd5Sum string `xml:PicMd5Sum`
  87. }
  88. //EncryptedXMLMsg 安全模式下的消息体
  89. type EncryptedXMLMsg struct {
  90. XMLName struct{} `xml:"xml" json:"-"`
  91. ToUserName string `xml:"ToUserName" json:"ToUserName"`
  92. EncryptedMsg string `xml:"Encrypt" json:"Encrypt"`
  93. }
  94. //ResponseEncryptedXMLMsg 需要返回的消息体
  95. type ResponseEncryptedXMLMsg struct {
  96. XMLName struct{} `xml:"xml" json:"-"`
  97. EncryptedMsg string `xml:"Encrypt" json:"Encrypt"`
  98. MsgSignature string `xml:"MsgSignature" json:"MsgSignature"`
  99. Timestamp int64 `xml:"TimeStamp" json:"TimeStamp"`
  100. Nonce string `xml:"Nonce" json:"Nonce"`
  101. }
  102. // CommonToken 消息中通用的结构
  103. type CommonToken struct {
  104. XMLName xml.Name `xml:"xml"`
  105. ToUserName string `xml:"ToUserName"`
  106. FromUserName string `xml:"FromUserName"`
  107. CreateTime int64 `xml:"CreateTime"`
  108. MsgType MsgType `xml:"MsgType"`
  109. }
  110. //SetToUserName set ToUserName
  111. func (msg *CommonToken) SetToUserName(toUserName string) {
  112. msg.ToUserName = toUserName
  113. }
  114. //SetFromUserName set FromUserName
  115. func (msg *CommonToken) SetFromUserName(fromUserName string) {
  116. msg.FromUserName = fromUserName
  117. }
  118. //SetCreateTime set createTime
  119. func (msg *CommonToken) SetCreateTime(createTime int64) {
  120. msg.CreateTime = createTime
  121. }
  122. //SetMsgType set MsgType
  123. func (msg *CommonToken) SetMsgType(msgType MsgType) {
  124. msg.MsgType = msgType
  125. }