message.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. package message
  2. import (
  3. "encoding/xml"
  4. "github.com/silenceper/wechat/v2/officialaccount/device"
  5. )
  6. // MsgType 基本消息类型
  7. type MsgType string
  8. // EventType 事件类型
  9. type EventType string
  10. // InfoType 第三方平台授权事件类型
  11. type InfoType string
  12. const (
  13. //MsgTypeText 表示文本消息
  14. MsgTypeText MsgType = "text"
  15. //MsgTypeImage 表示图片消息
  16. MsgTypeImage = "image"
  17. //MsgTypeVoice 表示语音消息
  18. MsgTypeVoice = "voice"
  19. //MsgTypeVideo 表示视频消息
  20. MsgTypeVideo = "video"
  21. //MsgTypeShortVideo 表示短视频消息[限接收]
  22. MsgTypeShortVideo = "shortvideo"
  23. //MsgTypeLocation 表示坐标消息[限接收]
  24. MsgTypeLocation = "location"
  25. //MsgTypeLink 表示链接消息[限接收]
  26. MsgTypeLink = "link"
  27. //MsgTypeMusic 表示音乐消息[限回复]
  28. MsgTypeMusic = "music"
  29. //MsgTypeNews 表示图文消息[限回复]
  30. MsgTypeNews = "news"
  31. //MsgTypeTransfer 表示消息消息转发到客服
  32. MsgTypeTransfer = "transfer_customer_service"
  33. //MsgTypeEvent 表示事件推送消息
  34. MsgTypeEvent = "event"
  35. )
  36. const (
  37. //EventSubscribe 订阅
  38. EventSubscribe EventType = "subscribe"
  39. //EventUnsubscribe 取消订阅
  40. EventUnsubscribe = "unsubscribe"
  41. //EventScan 用户已经关注公众号,则微信会将带场景值扫描事件推送给开发者
  42. EventScan = "SCAN"
  43. //EventLocation 上报地理位置事件
  44. EventLocation = "LOCATION"
  45. //EventClick 点击菜单拉取消息时的事件推送
  46. EventClick = "CLICK"
  47. //EventView 点击菜单跳转链接时的事件推送
  48. EventView = "VIEW"
  49. //EventScancodePush 扫码推事件的事件推送
  50. EventScancodePush = "scancode_push"
  51. //EventScancodeWaitmsg 扫码推事件且弹出“消息接收中”提示框的事件推送
  52. EventScancodeWaitmsg = "scancode_waitmsg"
  53. //EventPicSysphoto 弹出系统拍照发图的事件推送
  54. EventPicSysphoto = "pic_sysphoto"
  55. //EventPicPhotoOrAlbum 弹出拍照或者相册发图的事件推送
  56. EventPicPhotoOrAlbum = "pic_photo_or_album"
  57. //EventPicWeixin 弹出微信相册发图器的事件推送
  58. EventPicWeixin = "pic_weixin"
  59. //EventLocationSelect 弹出地理位置选择器的事件推送
  60. EventLocationSelect = "location_select"
  61. //EventTemplateSendJobFinish 发送模板消息推送通知
  62. EventTemplateSendJobFinish = "TEMPLATESENDJOBFINISH"
  63. //EventWxaMediaCheck 异步校验图片/音频是否含有违法违规内容推送事件
  64. EventWxaMediaCheck = "wxa_media_check"
  65. )
  66. const (
  67. // InfoTypeVerifyTicket 返回ticket
  68. InfoTypeVerifyTicket InfoType = "component_verify_ticket"
  69. // InfoTypeAuthorized 授权
  70. InfoTypeAuthorized = "authorized"
  71. // InfoTypeUnauthorized 取消授权
  72. InfoTypeUnauthorized = "unauthorized"
  73. // InfoTypeUpdateAuthorized 更新授权
  74. InfoTypeUpdateAuthorized = "updateauthorized"
  75. )
  76. //MixMessage 存放所有微信发送过来的消息和事件
  77. type MixMessage struct {
  78. CommonToken
  79. //基本消息
  80. MsgID int64 `xml:"MsgId"`
  81. Content string `xml:"Content"`
  82. Recognition string `xml:"Recognition"`
  83. PicURL string `xml:"PicUrl"`
  84. MediaID string `xml:"MediaId"`
  85. Format string `xml:"Format"`
  86. ThumbMediaID string `xml:"ThumbMediaId"`
  87. LocationX float64 `xml:"Location_X"`
  88. LocationY float64 `xml:"Location_Y"`
  89. Scale float64 `xml:"Scale"`
  90. Label string `xml:"Label"`
  91. Title string `xml:"Title"`
  92. Description string `xml:"Description"`
  93. URL string `xml:"Url"`
  94. //事件相关
  95. Event EventType `xml:"Event"`
  96. EventKey string `xml:"EventKey"`
  97. Ticket string `xml:"Ticket"`
  98. Latitude string `xml:"Latitude"`
  99. Longitude string `xml:"Longitude"`
  100. Precision string `xml:"Precision"`
  101. MenuID string `xml:"MenuId"`
  102. Status string `xml:"Status"`
  103. SessionFrom string `xml:"SessionFrom"`
  104. ScanCodeInfo struct {
  105. ScanType string `xml:"ScanType"`
  106. ScanResult string `xml:"ScanResult"`
  107. } `xml:"ScanCodeInfo"`
  108. SendPicsInfo struct {
  109. Count int32 `xml:"Count"`
  110. PicList []EventPic `xml:"PicList>item"`
  111. } `xml:"SendPicsInfo"`
  112. SendLocationInfo struct {
  113. LocationX float64 `xml:"Location_X"`
  114. LocationY float64 `xml:"Location_Y"`
  115. Scale float64 `xml:"Scale"`
  116. Label string `xml:"Label"`
  117. Poiname string `xml:"Poiname"`
  118. }
  119. // 第三方平台相关
  120. InfoType InfoType `xml:"InfoType"`
  121. AppID string `xml:"AppId"`
  122. ComponentVerifyTicket string `xml:"ComponentVerifyTicket"`
  123. AuthorizerAppid string `xml:"AuthorizerAppid"`
  124. AuthorizationCode string `xml:"AuthorizationCode"`
  125. AuthorizationCodeExpiredTime int64 `xml:"AuthorizationCodeExpiredTime"`
  126. PreAuthCode string `xml:"PreAuthCode"`
  127. // 卡券相关
  128. CardID string `xml:"CardId"`
  129. RefuseReason string `xml:"RefuseReason"`
  130. IsGiveByFriend int32 `xml:"IsGiveByFriend"`
  131. FriendUserName string `xml:"FriendUserName"`
  132. UserCardCode string `xml:"UserCardCode"`
  133. OldUserCardCode string `xml:"OldUserCardCode"`
  134. OuterStr string `xml:"OuterStr"`
  135. IsRestoreMemberCard int32 `xml:"IsRestoreMemberCard"`
  136. UnionID string `xml:"UnionId"`
  137. // 内容审核相关
  138. IsRisky bool `xml:"isrisky"`
  139. ExtraInfoJSON string `xml:"extra_info_json"`
  140. TraceID string `xml:"trace_id"`
  141. StatusCode int `xml:"status_code"`
  142. //设备相关
  143. device.MsgDevice
  144. }
  145. //EventPic 发图事件推送
  146. type EventPic struct {
  147. PicMd5Sum string `xml:"PicMd5Sum"`
  148. }
  149. //EncryptedXMLMsg 安全模式下的消息体
  150. type EncryptedXMLMsg struct {
  151. XMLName struct{} `xml:"xml" json:"-"`
  152. ToUserName string `xml:"ToUserName" json:"ToUserName"`
  153. EncryptedMsg string `xml:"Encrypt" json:"Encrypt"`
  154. }
  155. //ResponseEncryptedXMLMsg 需要返回的消息体
  156. type ResponseEncryptedXMLMsg struct {
  157. XMLName struct{} `xml:"xml" json:"-"`
  158. EncryptedMsg string `xml:"Encrypt" json:"Encrypt"`
  159. MsgSignature string `xml:"MsgSignature" json:"MsgSignature"`
  160. Timestamp int64 `xml:"TimeStamp" json:"TimeStamp"`
  161. Nonce string `xml:"Nonce" json:"Nonce"`
  162. }
  163. // CDATA 使用该类型,在序列化为 xml 文本时文本会被解析器忽略
  164. type CDATA string
  165. // MarshalXML 实现自己的序列化方法
  166. func (c CDATA) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
  167. return e.EncodeElement(struct {
  168. string `xml:",cdata"`
  169. }{string(c)}, start)
  170. }
  171. // CommonToken 消息中通用的结构
  172. type CommonToken struct {
  173. XMLName xml.Name `xml:"xml"`
  174. ToUserName CDATA `xml:"ToUserName"`
  175. FromUserName CDATA `xml:"FromUserName"`
  176. CreateTime int64 `xml:"CreateTime"`
  177. MsgType MsgType `xml:"MsgType"`
  178. }
  179. //SetToUserName set ToUserName
  180. func (msg *CommonToken) SetToUserName(toUserName CDATA) {
  181. msg.ToUserName = toUserName
  182. }
  183. //SetFromUserName set FromUserName
  184. func (msg *CommonToken) SetFromUserName(fromUserName CDATA) {
  185. msg.FromUserName = fromUserName
  186. }
  187. //SetCreateTime set createTime
  188. func (msg *CommonToken) SetCreateTime(createTime int64) {
  189. msg.CreateTime = createTime
  190. }
  191. //SetMsgType set MsgType
  192. func (msg *CommonToken) SetMsgType(msgType MsgType) {
  193. msg.MsgType = msgType
  194. }