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