You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

event_handle.go 3.9 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package wechat
  2. import (
  3. "code.gitea.io/gitea/models"
  4. "code.gitea.io/gitea/modules/auth/wechat"
  5. "code.gitea.io/gitea/modules/notification"
  6. "code.gitea.io/gitea/modules/redis/redis_client"
  7. "code.gitea.io/gitea/modules/redis/redis_key"
  8. "encoding/json"
  9. "encoding/xml"
  10. "strings"
  11. "time"
  12. )
  13. //<xml>
  14. // <ToUserName><![CDATA[toUser]]></ToUserName>
  15. // <FromUserName><![CDATA[FromUser]]></FromUserName>
  16. // <CreateTime>123456789</CreateTime>
  17. // <MsgType><![CDATA[event]]></MsgType>
  18. // <Event><![CDATA[SCAN]]></Event>
  19. // <EventKey><![CDATA[SCENE_VALUE]]></EventKey>
  20. // <Ticket><![CDATA[TICKET]]></Ticket>
  21. //</xml>
  22. type WechatMsg struct {
  23. ToUserName string
  24. FromUserName string
  25. CreateTime int64
  26. MsgType string
  27. Event string
  28. EventKey string
  29. Ticket string
  30. Content string
  31. MsgId string
  32. MsgDataId string
  33. Idx string
  34. }
  35. type MsgReply struct {
  36. XMLName xml.Name `xml:"xml"`
  37. ToUserName string
  38. FromUserName string
  39. CreateTime int64
  40. MsgType string
  41. Content string
  42. }
  43. type TextMsgReply struct {
  44. XMLName xml.Name `xml:"xml"`
  45. ToUserName string
  46. FromUserName string
  47. CreateTime int64
  48. MsgType string
  49. Content string
  50. }
  51. type ImageMsgReply struct {
  52. XMLName xml.Name `xml:"xml"`
  53. ToUserName string
  54. FromUserName string
  55. CreateTime int64
  56. MsgType string
  57. Image ImageContent
  58. }
  59. type VoiceMsgReply struct {
  60. XMLName xml.Name `xml:"xml"`
  61. ToUserName string
  62. FromUserName string
  63. CreateTime int64
  64. MsgType string
  65. Voice VoiceContent
  66. }
  67. type VideoMsgReply struct {
  68. XMLName xml.Name `xml:"xml"`
  69. ToUserName string
  70. FromUserName string
  71. CreateTime int64
  72. MsgType string
  73. Video VideoContent
  74. }
  75. type MusicMsgReply struct {
  76. XMLName xml.Name `xml:"xml"`
  77. ToUserName string
  78. FromUserName string
  79. CreateTime int64
  80. MsgType string
  81. Music MusicContent
  82. }
  83. type NewsMsgReply struct {
  84. XMLName xml.Name `xml:"xml"`
  85. ToUserName string
  86. FromUserName string
  87. CreateTime int64
  88. MsgType string
  89. ArticleCount int
  90. Articles ArticleItem
  91. }
  92. type ArticleItem struct {
  93. Item []ArticlesContent
  94. }
  95. type ImageContent struct {
  96. MediaId string
  97. }
  98. type VoiceContent struct {
  99. MediaId string
  100. }
  101. type VideoContent struct {
  102. MediaId string
  103. Title string
  104. Description string
  105. }
  106. type MusicContent struct {
  107. Title string
  108. Description string
  109. MusicUrl string
  110. HQMusicUrl string
  111. ThumbMediaId string
  112. }
  113. type ArticlesContent struct {
  114. XMLName xml.Name `xml:"item"`
  115. Title string
  116. Description string
  117. PicUrl string
  118. Url string
  119. }
  120. const (
  121. WECHAT_EVENT_SUBSCRIBE = "subscribe"
  122. WECHAT_EVENT_SCAN = "SCAN"
  123. )
  124. const (
  125. WECHAT_MSG_TYPE_TEXT = "text"
  126. WECHAT_MSG_TYPE_EVENT = "event"
  127. )
  128. func HandleScanEvent(we WechatMsg) string {
  129. eventKey := we.EventKey
  130. if eventKey == "" {
  131. return ""
  132. }
  133. sceneStr := strings.TrimPrefix(eventKey, "qrscene_")
  134. key := redis_key.WechatBindingUserIdKey(sceneStr)
  135. val, _ := redis_client.Get(key)
  136. if val == "" {
  137. return ""
  138. }
  139. qrCache := new(wechat.QRCode4BindCache)
  140. json.Unmarshal([]byte(val), qrCache)
  141. if qrCache.Status == wechat.BIND_STATUS_UNBIND {
  142. err := wechat.BindWechat(qrCache.UserId, we.FromUserName)
  143. if err != nil {
  144. if err, ok := err.(wechat.WechatBindError); ok {
  145. return err.Reply
  146. }
  147. return wechat.BIND_REPLY_FAILED_DEFAULT
  148. }
  149. qrCache.Status = wechat.BIND_STATUS_BOUND
  150. jsonStr, _ := json.Marshal(qrCache)
  151. redis_client.Setex(redis_key.WechatBindingUserIdKey(sceneStr), string(jsonStr), 60*time.Second)
  152. }
  153. u, err := models.GetUserByID(qrCache.UserId)
  154. if err == nil {
  155. notification.NotifyWechatBind(u, we.FromUserName)
  156. }
  157. return wechat.BIND_REPLY_SUCCESS
  158. }
  159. func HandleSubscribeEvent(we WechatMsg) *WechatReplyContent {
  160. r, err := LoadReplyFromCacheAndDisk(SubscribeReply)
  161. if err != nil || len(r) == 0 {
  162. return nil
  163. }
  164. return r[0]
  165. }