|
|
@@ -4,66 +4,29 @@ import ( |
|
|
|
"code.gitea.io/gitea/modules/auth/wechat" |
|
|
|
"code.gitea.io/gitea/modules/context" |
|
|
|
"code.gitea.io/gitea/modules/log" |
|
|
|
"code.gitea.io/gitea/modules/redis/redis_client" |
|
|
|
"code.gitea.io/gitea/modules/redis/redis_key" |
|
|
|
"encoding/xml" |
|
|
|
"io/ioutil" |
|
|
|
"strconv" |
|
|
|
"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 Xml struct { |
|
|
|
ToUserName string |
|
|
|
FromUserName string |
|
|
|
CreateTime int64 |
|
|
|
MsgType string |
|
|
|
Content string |
|
|
|
} |
|
|
|
|
|
|
|
// AcceptWechatEvent |
|
|
|
func AcceptWechatEvent(ctx *context.Context) { |
|
|
|
b, _ := ioutil.ReadAll(ctx.Req.Request.Body) |
|
|
|
we := WechatEvent{} |
|
|
|
we := wechat.WechatEvent{} |
|
|
|
xml.Unmarshal(b, &we) |
|
|
|
|
|
|
|
log.Info("accept wechat event= %+v", we) |
|
|
|
key := redis_key.WechatBindingUserIdKey(we.EventKey) |
|
|
|
val, _ := redis_client.Get(key) |
|
|
|
if val == "" { |
|
|
|
log.Error("sceneStr is not exist,sceneStr=%s", we.EventKey) |
|
|
|
ctx.XML(200, "") |
|
|
|
return |
|
|
|
var replyStr string |
|
|
|
if we.Event == "subscribe" { |
|
|
|
replyStr = wechat.HandleSubscribeEvent(we) |
|
|
|
} |
|
|
|
//todo 已绑定微信号的如何处理? |
|
|
|
//todo 取消关注事件记得过滤 |
|
|
|
userId, _ := strconv.ParseInt(val, 10, 64) |
|
|
|
//更新微信openId和流水 |
|
|
|
wechat.BindWechat(userId, we.EventKey) |
|
|
|
reply := &Xml{ |
|
|
|
|
|
|
|
reply := &wechat.Xml{ |
|
|
|
ToUserName: we.FromUserName, |
|
|
|
FromUserName: we.ToUserName, |
|
|
|
CreateTime: time.Now().Unix(), |
|
|
|
MsgType: "text", |
|
|
|
Content: "启智账号认证微信成功", |
|
|
|
Content: replyStr, |
|
|
|
} |
|
|
|
ctx.XML(200, reply) |
|
|
|
} |
|
|
|