|
- package wechat
-
- import (
- "code.gitea.io/gitea/models"
- "code.gitea.io/gitea/modules/notification"
- "code.gitea.io/gitea/modules/redis/redis_client"
- "code.gitea.io/gitea/modules/redis/redis_key"
- "encoding/json"
- "encoding/xml"
- "strings"
- "time"
- )
-
- //<xml>
- // <ToUserName><![CDATA[toUser]]></ToUserName>
- // <FromUserName><![CDATA[FromUser]]></FromUserName>
- // <CreateTime>123456789</CreateTime>
- // <MsgType><![CDATA[event]]></MsgType>
- // <Event><![CDATA[SCAN]]></Event>
- // <EventKey><![CDATA[SCENE_VALUE]]></EventKey>
- // <Ticket><![CDATA[TICKET]]></Ticket>
- //</xml>
- type WechatEvent struct {
- ToUserName string
- FromUserName string
- CreateTime int64
- MsgType string
- Event string
- EventKey string
- Ticket string
- }
-
- type EventReply struct {
- XMLName xml.Name `xml:"xml"`
- ToUserName string
- FromUserName string
- CreateTime int64
- MsgType string
- Content string
- }
-
- const (
- WECHAT_EVENT_SUBSCRIBE = "subscribe"
- WECHAT_EVENT_SCAN = "SCAN"
- )
-
- const (
- WECHAT_MSG_TYPE_TEXT = "text"
- )
-
- func HandleSubscribeEvent(we WechatEvent) string {
- eventKey := we.EventKey
- if eventKey == "" {
- return ""
- }
- sceneStr := strings.TrimPrefix(eventKey, "qrscene_")
- key := redis_key.WechatBindingUserIdKey(sceneStr)
- val, _ := redis_client.Get(key)
- if val == "" {
- return ""
- }
- qrCache := new(QRCode4BindCache)
- json.Unmarshal([]byte(val), qrCache)
- if qrCache.Status == BIND_STATUS_UNBIND {
- err := BindWechat(qrCache.UserId, we.FromUserName)
- if err != nil {
- if err, ok := err.(WechatBindError); ok {
- return err.Reply
- }
- return BIND_REPLY_FAILED_DEFAULT
- }
- qrCache.Status = BIND_STATUS_BOUND
- jsonStr, _ := json.Marshal(qrCache)
- redis_client.Setex(redis_key.WechatBindingUserIdKey(sceneStr), string(jsonStr), 60*time.Second)
- }
- u, err := models.GetUserByID(qrCache.UserId)
- if err == nil {
- notification.NotifyWechatBind(u, we.FromUserName)
- }
-
- return BIND_REPLY_SUCCESS
- }
|