Browse Source

#1494

add log
tags/v1.22.2.2^2
Gitea 3 years ago
parent
commit
5490e9806b
5 changed files with 24 additions and 4 deletions
  1. +3
    -0
      modules/auth/wechat/client.go
  2. +9
    -0
      modules/auth/wechat/event_handle.go
  3. +3
    -0
      modules/auth/wechat/qr_code.go
  4. +3
    -2
      routers/authentication/wechat.go
  5. +6
    -2
      routers/authentication/wechat_event.go

+ 3
- 0
modules/auth/wechat/client.go View File

@@ -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()



+ 9
- 0
modules/auth/wechat/event_handle.go View File

@@ -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 == "" {


+ 3
- 0
modules/auth/wechat/qr_code.go View File

@@ -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)
}


+ 3
- 2
routers/authentication/wechat.go View File

@@ -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 {


+ 6
- 2
routers/authentication/wechat_event.go View File

@@ -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)


Loading…
Cancel
Save