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 1.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
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package wechat
  2. import (
  3. "code.gitea.io/gitea/models"
  4. "code.gitea.io/gitea/modules/notification"
  5. "code.gitea.io/gitea/modules/redis/redis_client"
  6. "code.gitea.io/gitea/modules/redis/redis_key"
  7. "encoding/json"
  8. "encoding/xml"
  9. "strings"
  10. "time"
  11. )
  12. //<xml>
  13. // <ToUserName><![CDATA[toUser]]></ToUserName>
  14. // <FromUserName><![CDATA[FromUser]]></FromUserName>
  15. // <CreateTime>123456789</CreateTime>
  16. // <MsgType><![CDATA[event]]></MsgType>
  17. // <Event><![CDATA[SCAN]]></Event>
  18. // <EventKey><![CDATA[SCENE_VALUE]]></EventKey>
  19. // <Ticket><![CDATA[TICKET]]></Ticket>
  20. //</xml>
  21. type WechatEvent struct {
  22. ToUserName string
  23. FromUserName string
  24. CreateTime int64
  25. MsgType string
  26. Event string
  27. EventKey string
  28. Ticket string
  29. }
  30. type EventReply struct {
  31. XMLName xml.Name `xml:"xml"`
  32. ToUserName string
  33. FromUserName string
  34. CreateTime int64
  35. MsgType string
  36. Content string
  37. }
  38. const (
  39. WECHAT_EVENT_SUBSCRIBE = "subscribe"
  40. WECHAT_EVENT_SCAN = "SCAN"
  41. )
  42. const (
  43. WECHAT_MSG_TYPE_TEXT = "text"
  44. )
  45. func HandleSubscribeEvent(we WechatEvent) string {
  46. eventKey := we.EventKey
  47. if eventKey == "" {
  48. return ""
  49. }
  50. sceneStr := strings.TrimPrefix(eventKey, "qrscene_")
  51. key := redis_key.WechatBindingUserIdKey(sceneStr)
  52. val, _ := redis_client.Get(key)
  53. if val == "" {
  54. return ""
  55. }
  56. qrCache := new(QRCode4BindCache)
  57. json.Unmarshal([]byte(val), qrCache)
  58. if qrCache.Status == BIND_STATUS_UNBIND {
  59. err := BindWechat(qrCache.UserId, we.FromUserName)
  60. if err != nil {
  61. if err, ok := err.(WechatBindError); ok {
  62. return err.Reply
  63. }
  64. return BIND_REPLY_FAILED_DEFAULT
  65. }
  66. qrCache.Status = BIND_STATUS_BOUND
  67. jsonStr, _ := json.Marshal(qrCache)
  68. redis_client.Setex(redis_key.WechatBindingUserIdKey(sceneStr), string(jsonStr), 60*time.Second)
  69. }
  70. u, err := models.GetUserByID(qrCache.UserId)
  71. if err == nil {
  72. notification.NotifyWechatBind(u, we.FromUserName)
  73. }
  74. return BIND_REPLY_SUCCESS
  75. }