message.go 8.9 KB

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