liujiandao
2023-11-18 115bd9b51f5d8eade4658f844de37516486c60e7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package redisstore
 
import (
    "aps_crm/pkg/logx"
    "aps_crm/pkg/redisx"
    "context"
    "time"
)
 
type Captcha struct {
    Expiration time.Duration
    PreKey     string
    Context    context.Context
}
 
func NewCaptcha() *Captcha {
    return &Captcha{
        Expiration: time.Second * 180,
        PreKey:     "CAPTCHA_",
    }
}
 
//func (rs *Captcha) UseWithCtx(ctx context.Context) base64Captcha.Store {
//    rs.Context = ctx
//    return rs
//}
 
func (rs *Captcha) Set(id string, value string) {
    err := redisx.GetRedisClient().Set(rs.Context, rs.PreKey+id, value, rs.Expiration).Err()
    if err != nil {
        logx.Errorf("RedisStoreSetError! err:%v", err)
    }
}
 
func (rs *Captcha) Get(key string, clear bool) string {
    val, err := redisx.GetRedisClient().Get(rs.Context, key).Result()
    if err != nil {
        logx.Errorf("RedisStoreGetError! err:%v", err)
        return ""
    }
    if clear {
        err := redisx.GetRedisClient().Del(rs.Context, key).Err()
        if err != nil {
            logx.Errorf("RedisStoreClearError! err:%v", err)
            return ""
        }
    }
    return val
}
 
func (rs *Captcha) Verify(id, answer string, clear bool) bool {
    key := rs.PreKey + id
    v := rs.Get(key, clear)
    return v == answer
}