Skip to content

Commit 2342a47

Browse files
author
蒋吉兆
committed
修复 v8下redis 验证码模式 存在的未传入context的bug
1 parent 186ecbf commit 2342a47

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

server/api/v1/system/sys_captcha.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
// 当开启多服务器部署时,替换下面的配置,使用redis共享存储验证码
13-
// var store = captcha.NewDefaultRedisStore()
13+
//var store = captcha.NewDefaultRedisStore()
1414
var store = base64Captcha.DefaultMemStore
1515

1616
type BaseApi struct {
@@ -27,6 +27,7 @@ func (b *BaseApi) Captcha(c *gin.Context) {
2727
// 字符,公式,验证码配置
2828
// 生成默认数字的driver
2929
driver := base64Captcha.NewDriverDigit(global.GVA_CONFIG.Captcha.ImgHeight, global.GVA_CONFIG.Captcha.ImgWidth, global.GVA_CONFIG.Captcha.KeyLong, 0.7, 80)
30+
//cp := base64Captcha.NewCaptcha(driver, store.UseWithCtx(c)) // v8下使用redis
3031
cp := base64Captcha.NewCaptcha(driver, store)
3132
if id, b64s, err := cp.Generate(); err != nil {
3233
global.GVA_LOG.Error("验证码获取失败!", zap.Any("err", err))

server/utils/captcha/redis.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package captcha
22

33
import (
4+
"context"
45
"gin-vue-admin/global"
5-
"time"
6-
76
"github.com/mojocn/base64Captcha"
87
"go.uber.org/zap"
8+
"time"
99
)
1010

11-
func NewDefaultRedisStore() base64Captcha.Store {
11+
func NewDefaultRedisStore() *RedisStore {
1212
return &RedisStore{
1313
Expiration: time.Second * 180,
1414
PreKey: "CAPTCHA_",
@@ -18,23 +18,29 @@ func NewDefaultRedisStore() base64Captcha.Store {
1818
type RedisStore struct {
1919
Expiration time.Duration
2020
PreKey string
21+
Context context.Context
22+
}
23+
24+
func (rs *RedisStore) UseWithCtx(ctx context.Context) base64Captcha.Store {
25+
rs.Context = ctx
26+
return rs
2127
}
2228

2329
func (rs *RedisStore) Set(id string, value string) {
24-
err := global.GVA_REDIS.Set(rs.PreKey+id, value, rs.Expiration).Err()
30+
err := global.GVA_REDIS.Set(rs.Context, rs.PreKey+id, value, rs.Expiration).Err()
2531
if err != nil {
2632
global.GVA_LOG.Error("RedisStoreSetError!", zap.Error(err))
2733
}
2834
}
2935

3036
func (rs *RedisStore) Get(key string, clear bool) string {
31-
val, err := global.GVA_REDIS.Get(key).Result()
37+
val, err := global.GVA_REDIS.Get(rs.Context, key).Result()
3238
if err != nil {
3339
global.GVA_LOG.Error("RedisStoreGetError!", zap.Error(err))
3440
return ""
3541
}
3642
if clear {
37-
err := global.GVA_REDIS.Del(key).Err()
43+
err := global.GVA_REDIS.Del(rs.Context, key).Err()
3844
if err != nil {
3945
global.GVA_LOG.Error("RedisStoreClearError!", zap.Error(err))
4046
return ""

0 commit comments

Comments
 (0)