File tree Expand file tree Collapse file tree 1 file changed +50
-0
lines changed Expand file tree Collapse file tree 1 file changed +50
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments