diff --git a/modules/auth/wechat/client.go b/modules/auth/wechat/client.go index bb1772b2b..6734977a1 100644 --- a/modules/auth/wechat/client.go +++ b/modules/auth/wechat/client.go @@ -62,6 +62,7 @@ func getWechatRestyClient() *resty.Client { return client } +// api doc:https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Get_access_token.html func callAccessToken() *AccessTokenResponse { client := getWechatRestyClient() @@ -79,6 +80,8 @@ func callAccessToken() *AccessTokenResponse { return &result } +//callQRCodeCreate call the wechat api to create qr-code, +// api doc: https://developers.weixin.qq.com/doc/offiaccount/Account_Management/Generating_a_Parametric_QR_Code.html func callQRCodeCreate(sceneStr string) (*QRCodeResponse, bool) { client := getWechatRestyClient() diff --git a/modules/auth/wechat/event_handle.go b/modules/auth/wechat/event_handle.go index c49c6d763..b40ab3101 100644 --- a/modules/auth/wechat/event_handle.go +++ b/modules/auth/wechat/event_handle.go @@ -37,6 +37,15 @@ type EventReply struct { 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 == "" { diff --git a/modules/auth/wechat/qr_code.go b/modules/auth/wechat/qr_code.go index 5506cecb7..8c2da55c9 100644 --- a/modules/auth/wechat/qr_code.go +++ b/modules/auth/wechat/qr_code.go @@ -1,8 +1,11 @@ package wechat +import "code.gitea.io/gitea/modules/log" + func GetWechatQRCode4Bind(sceneStr string) *QRCodeResponse { result, retryFlag := callQRCodeCreate(sceneStr) if retryFlag { + log.Info("retry wechat qr-code calling,sceneStr=%s", sceneStr) refreshAccessTokenCache() result, _ = callQRCodeCreate(sceneStr) } diff --git a/routers/authentication/wechat.go b/routers/authentication/wechat.go index 866825fce..7a62850e2 100644 --- a/routers/authentication/wechat.go +++ b/routers/authentication/wechat.go @@ -43,7 +43,7 @@ func GetQRCode4Bind(ctx *context.Context) { }) } -// GetBindStatus +// GetBindStatus the web page will poll the service to get bind status func GetBindStatus(ctx *context.Context) { sceneStr := ctx.Query("sceneStr") val, _ := redis_client.Get(redis_key.WechatBindingUserIdKey(sceneStr)) @@ -86,6 +86,7 @@ func GetBindPage(ctx *context.Context) { } func createQRCode4Bind(userId int64) (*QRCodeResponse, error) { + log.Info("start to create qr-code for binding.userId=%d", userId) sceneStr := gouuid.NewV4().String() r := wechat.GetWechatQRCode4Bind(sceneStr) if r == nil { @@ -98,7 +99,7 @@ func createQRCode4Bind(userId int64) (*QRCodeResponse, error) { }) isOk, err := redis_client.Setex(redis_key.WechatBindingUserIdKey(sceneStr), string(jsonStr), time.Duration(setting.WechatQRCodeExpireSeconds)*time.Second) if err != nil { - log.Error("createQRCode4Bind failed.e=%v", err) + log.Error("createQRCode4Bind failed.e=%+v", err) return nil, err } if !isOk { diff --git a/routers/authentication/wechat_event.go b/routers/authentication/wechat_event.go index 35ba23166..9b1cebec6 100644 --- a/routers/authentication/wechat_event.go +++ b/routers/authentication/wechat_event.go @@ -10,6 +10,8 @@ import ( ) // AcceptWechatEvent +// https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Receiving_event_pushes.html +// https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Passive_user_reply_message.html func AcceptWechatEvent(ctx *context.Context) { b, _ := ioutil.ReadAll(ctx.Req.Request.Body) we := wechat.WechatEvent{} @@ -17,8 +19,10 @@ func AcceptWechatEvent(ctx *context.Context) { log.Info("accept wechat event= %+v", we) var replyStr string - if we.Event == "subscribe" { + switch we.Event { + case wechat.WECHAT_EVENT_SUBSCRIBE, wechat.WECHAT_EVENT_SCAN: replyStr = wechat.HandleSubscribeEvent(we) + break } if replyStr == "" { @@ -29,7 +33,7 @@ func AcceptWechatEvent(ctx *context.Context) { ToUserName: we.FromUserName, FromUserName: we.ToUserName, CreateTime: time.Now().Unix(), - MsgType: "text", + MsgType: wechat.WECHAT_MSG_TYPE_TEXT, Content: replyStr, } ctx.XML(200, reply)