Skip to content

Commit e82cfa6

Browse files
author
liaoyongfu
committed
add captcha redis
1 parent bbf6549 commit e82cfa6

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

server/utils/captcha/redis.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package captcha
2+
3+
import (
4+
"gin-vue-admin/global"
5+
"time"
6+
7+
"github.com/mojocn/base64Captcha"
8+
"go.uber.org/zap"
9+
)
10+
11+
func NewDefaultRedisStore() base64Captcha.Store {
12+
return &RedisStore{
13+
Expiration: time.Second * 180,
14+
PreKey: "CAPTCHA_",
15+
}
16+
}
17+
18+
type RedisStore struct {
19+
Expiration time.Duration
20+
PreKey string
21+
}
22+
23+
func (rs *RedisStore) Set(id string, value string) {
24+
err := global.GVA_REDIS.Set(rs.PreKey+id, value, rs.Expiration).Err()
25+
if err != nil {
26+
global.GVA_LOG.Error("RedisStoreSetError!", zap.Error(err))
27+
}
28+
}
29+
30+
func (rs *RedisStore) Get(key string, clear bool) string {
31+
val, err := global.GVA_REDIS.Get(key).Result()
32+
if err != nil {
33+
global.GVA_LOG.Error("RedisStoreGetError!", zap.Error(err))
34+
return ""
35+
}
36+
if clear {
37+
err := global.GVA_REDIS.Del(key).Err()
38+
if err != nil {
39+
global.GVA_LOG.Error("RedisStoreClearError!", zap.Error(err))
40+
return ""
41+
}
42+
}
43+
return val
44+
}
45+
46+
func (rs *RedisStore) Verify(id, answer string, clear bool) bool {
47+
key := rs.PreKey + id
48+
v := rs.Get(key, clear)
49+
return v == answer
50+
}

0 commit comments

Comments
 (0)