message.go 7.2 KB

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