message.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. //微信开放平台需要用到
  68. // InfoTypeVerifyTicket 返回ticket
  69. InfoTypeVerifyTicket InfoType = "component_verify_ticket"
  70. // InfoTypeAuthorized 授权
  71. InfoTypeAuthorized = "authorized"
  72. // InfoTypeUnauthorized 取消授权
  73. InfoTypeUnauthorized = "unauthorized"
  74. // InfoTypeUpdateAuthorized 更新授权
  75. InfoTypeUpdateAuthorized = "updateauthorized"
  76. )
  77. //MixMessage 存放所有微信发送过来的消息和事件
  78. type MixMessage struct {
  79. CommonToken
  80. //基本消息
  81. MsgID int64 `xml:"MsgId"`
  82. Content string `xml:"Content"`
  83. Recognition string `xml:"Recognition"`
  84. PicURL string `xml:"PicUrl"`
  85. MediaID string `xml:"MediaId"`
  86. Format string `xml:"Format"`
  87. ThumbMediaID string `xml:"ThumbMediaId"`
  88. LocationX float64 `xml:"Location_X"`
  89. LocationY float64 `xml:"Location_Y"`
  90. Scale float64 `xml:"Scale"`
  91. Label string `xml:"Label"`
  92. Title string `xml:"Title"`
  93. Description string `xml:"Description"`
  94. URL string `xml:"Url"`
  95. //事件相关
  96. Event EventType `xml:"Event"`
  97. EventKey string `xml:"EventKey"`
  98. Ticket string `xml:"Ticket"`
  99. Latitude string `xml:"Latitude"`
  100. Longitude string `xml:"Longitude"`
  101. Precision string `xml:"Precision"`
  102. MenuID string `xml:"MenuId"`
  103. Status string `xml:"Status"`
  104. SessionFrom string `xml:"SessionFrom"`
  105. ScanCodeInfo struct {
  106. ScanType string `xml:"ScanType"`
  107. ScanResult string `xml:"ScanResult"`
  108. } `xml:"ScanCodeInfo"`
  109. SendPicsInfo struct {
  110. Count int32 `xml:"Count"`
  111. PicList []EventPic `xml:"PicList>item"`
  112. } `xml:"SendPicsInfo"`
  113. SendLocationInfo struct {
  114. LocationX float64 `xml:"Location_X"`
  115. LocationY float64 `xml:"Location_Y"`
  116. Scale float64 `xml:"Scale"`
  117. Label string `xml:"Label"`
  118. Poiname string `xml:"Poiname"`
  119. }
  120. // 第三方平台相关
  121. InfoType InfoType `xml:"InfoType"`
  122. AppID string `xml:"AppId"`
  123. ComponentVerifyTicket string `xml:"ComponentVerifyTicket"`
  124. AuthorizerAppid string `xml:"AuthorizerAppid"`
  125. AuthorizationCode string `xml:"AuthorizationCode"`
  126. AuthorizationCodeExpiredTime int64 `xml:"AuthorizationCodeExpiredTime"`
  127. PreAuthCode string `xml:"PreAuthCode"`
  128. // 卡券相关
  129. CardID string `xml:"CardId"`
  130. RefuseReason string `xml:"RefuseReason"`
  131. IsGiveByFriend int32 `xml:"IsGiveByFriend"`
  132. FriendUserName string `xml:"FriendUserName"`
  133. UserCardCode string `xml:"UserCardCode"`
  134. OldUserCardCode string `xml:"OldUserCardCode"`
  135. OuterStr string `xml:"OuterStr"`
  136. IsRestoreMemberCard int32 `xml:"IsRestoreMemberCard"`
  137. UnionID string `xml:"UnionId"`
  138. // 内容审核相关
  139. IsRisky bool `xml:"isrisky"`
  140. ExtraInfoJSON string `xml:"extra_info_json"`
  141. TraceID string `xml:"trace_id"`
  142. StatusCode int `xml:"status_code"`
  143. //设备相关
  144. device.MsgDevice
  145. }
  146. //EventPic 发图事件推送
  147. type EventPic struct {
  148. PicMd5Sum string `xml:"PicMd5Sum"`
  149. }
  150. //EncryptedXMLMsg 安全模式下的消息体
  151. type EncryptedXMLMsg struct {
  152. XMLName struct{} `xml:"xml" json:"-"`
  153. ToUserName string `xml:"ToUserName" json:"ToUserName"`
  154. EncryptedMsg string `xml:"Encrypt" json:"Encrypt"`
  155. }
  156. //ResponseEncryptedXMLMsg 需要返回的消息体
  157. type ResponseEncryptedXMLMsg struct {
  158. XMLName struct{} `xml:"xml" json:"-"`
  159. EncryptedMsg string `xml:"Encrypt" json:"Encrypt"`
  160. MsgSignature string `xml:"MsgSignature" json:"MsgSignature"`
  161. Timestamp int64 `xml:"TimeStamp" json:"TimeStamp"`
  162. Nonce string `xml:"Nonce" json:"Nonce"`
  163. }
  164. // CDATA 使用该类型,在序列化为 xml 文本时文本会被解析器忽略
  165. type CDATA string
  166. // MarshalXML 实现自己的序列化方法
  167. func (c CDATA) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
  168. return e.EncodeElement(struct {
  169. string `xml:",cdata"`
  170. }{string(c)}, start)
  171. }
  172. // CommonToken 消息中通用的结构
  173. type CommonToken struct {
  174. XMLName xml.Name `xml:"xml"`
  175. ToUserName CDATA `xml:"ToUserName"`
  176. FromUserName CDATA `xml:"FromUserName"`
  177. CreateTime int64 `xml:"CreateTime"`
  178. MsgType MsgType `xml:"MsgType"`
  179. }
  180. //SetToUserName set ToUserName
  181. func (msg *CommonToken) SetToUserName(toUserName CDATA) {
  182. msg.ToUserName = toUserName
  183. }
  184. //SetFromUserName set FromUserName
  185. func (msg *CommonToken) SetFromUserName(fromUserName CDATA) {
  186. msg.FromUserName = fromUserName
  187. }
  188. //SetCreateTime set createTime
  189. func (msg *CommonToken) SetCreateTime(createTime int64) {
  190. msg.CreateTime = createTime
  191. }
  192. //SetMsgType set MsgType
  193. func (msg *CommonToken) SetMsgType(msgType MsgType) {
  194. msg.MsgType = msgType
  195. }