message.go 8.2 KB

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