Browse Source

#1494

update
tags/v1.22.2.2^2
Gitea 3 years ago
parent
commit
13665f17c5
3 changed files with 47 additions and 45 deletions
  1. +12
    -10
      modules/auth/wechat/bind.go
  2. +12
    -5
      modules/auth/wechat/event_handle.go
  3. +23
    -30
      routers/authentication/wechat.go

+ 12
- 10
modules/auth/wechat/bind.go View File

@@ -4,6 +4,18 @@ import (
"code.gitea.io/gitea/models"
)

type QRCode4BindCache struct {
UserId int64
Status int
}

const (
BIND_STATUS_UNBIND = 0
BIND_STATUS_SCANNED = 1
BIND_STATUS_BOUND = 2
BIND_STATUS_EXPIRED = 9
)

func BindWechat(userId int64, wechatOpenId string) error {
return models.BindWechatOpenId(userId, wechatOpenId)
}
@@ -11,13 +23,3 @@ func BindWechat(userId int64, wechatOpenId string) error {
func UnbindWechat(userId int64) error {
return models.UnbindWechatOpenId(userId)
}

func IsUserFinishBind(sceneStr string) bool {
//val, _ := redis_client.Get(redis_key.WechatBindingUserIdKey(sceneStr))
//if val == "" {
// return false
//}
//userId, _ := strconv.ParseInt(val, 10, 64)
return false

}

+ 12
- 5
modules/auth/wechat/event_handle.go View File

@@ -3,10 +3,13 @@ package wechat
import (
"code.gitea.io/gitea/modules/redis/redis_client"
"code.gitea.io/gitea/modules/redis/redis_key"
"strconv"
"encoding/json"
"strings"
"time"
)

const BIND_REPLY = "启智账号认证微信成功"

//<xml>
// <ToUserName><![CDATA[toUser]]></ToUserName>
// <FromUserName><![CDATA[FromUser]]></FromUserName>
@@ -45,10 +48,14 @@ func HandleSubscribeEvent(we WechatEvent) string {
if val == "" {
return ""
}
qrCache := new(QRCode4BindCache)
json.Unmarshal([]byte(val), qrCache)
//todo 已绑定微信号的如何处理?
//todo 取消关注事件记得过滤
userId, _ := strconv.ParseInt(val, 10, 64)
//更新微信openId和流水
BindWechat(userId, we.EventKey)
return "启智账号认证微信成功"
BindWechat(qrCache.UserId, we.FromUserName)

qrCache.Status = BIND_STATUS_BOUND
jsonStr, _ := json.Marshal(qrCache)
redis_client.Setex(redis_key.WechatBindingUserIdKey(sceneStr), string(jsonStr), 60*time.Second)
return BIND_REPLY
}

+ 23
- 30
routers/authentication/wechat.go View File

@@ -7,8 +7,8 @@ import (
"code.gitea.io/gitea/modules/redis/redis_client"
"code.gitea.io/gitea/modules/redis/redis_key"
"code.gitea.io/gitea/modules/setting"
"encoding/json"
"errors"
"fmt"
gouuid "github.com/satori/go.uuid"
"time"
)
@@ -42,37 +42,26 @@ func GetQRCode4Bind(ctx *context.Context) {

// GetQRCode4Bind get QR code for wechat binding
func GetBindStatus(ctx *context.Context) {
//var status int
//sceneStr := ctx.Query("sceneStr")
//val, _ := redis_client.Get(redis_key.WechatBindingUserIdKey(sceneStr))
//if val == "" {
// ctx.JSON(200, map[string]interface{}{
// "code": "9999",
// "msg": "Get QR code failed",
// "data":
// //todo 继续完善查询接口,注意性能
// //todo 二维码重定向页面需要给一下
// //todo 微信推送Ng转发验证
// //todo 整体联调
// })
// return
//}
//
//userId, _ := strconv.ParseInt(val, 10, 64)
//
//r, err := createQRCode4Bind(userId)
//if err != nil {
// ctx.JSON(200, map[string]interface{}{
// "code": "9999",
// "msg": "Get QR code failed",
// })
// return
//}

sceneStr := ctx.Query("sceneStr")
val, _ := redis_client.Get(redis_key.WechatBindingUserIdKey(sceneStr))
if val == "" {
ctx.JSON(200, map[string]interface{}{
"code": "00",
"msg": "QR code expired",
"data": map[string]interface{}{
"status": wechat.BIND_STATUS_EXPIRED,
},
})
return
}
qrCache := new(wechat.QRCode4BindCache)
json.Unmarshal([]byte(val), qrCache)
ctx.JSON(200, map[string]interface{}{
"code": "00",
"msg": "success",
//"data": r,
"data": map[string]interface{}{
"status": qrCache.Status,
},
})
}

@@ -83,7 +72,11 @@ func createQRCode4Bind(userId int64) (*QRCodeResponse, error) {
return nil, errors.New("createQRCode4Bind failed")
}

isOk, err := redis_client.Setex(redis_key.WechatBindingUserIdKey(sceneStr), fmt.Sprint(userId), time.Duration(setting.WechatQRCodeExpireSeconds)*time.Second)
jsonStr, _ := json.Marshal(&wechat.QRCode4BindCache{
UserId: userId,
Status: wechat.BIND_STATUS_UNBIND,
})
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)
return nil, err


Loading…
Cancel
Save