代码拉取完成,页面将自动刷新
package captcha
import (
"context"
"gitee.com/study-helper/common/config"
"github.com/mojocn/base64Captcha"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/stores/redis"
"time"
)
// NewRedisStore returns a redis store for captcha.
func NewRedisStore(r *redis.Redis) *RedisStore {
return &RedisStore{
Expiration: time.Minute * 5,
PreKey: config.RedisCaptchaPrefix,
Redis: r,
}
}
// RedisStore stores captcha data.
type RedisStore struct {
Expiration time.Duration
PreKey string
Context context.Context
Redis *redis.Redis
}
// UseWithCtx add context for captcha.
func (r *RedisStore) UseWithCtx(ctx context.Context) base64Captcha.Store {
r.Context = ctx
return r
}
// Set sets the captcha KV to redis.
func (r *RedisStore) Set(id string, value string) error {
err := r.Redis.Setex(r.PreKey+id, value, int(r.Expiration.Seconds()))
if err != nil {
logx.Errorw("error occurs when captcha key sets to redis", logx.Field("detail", err))
return err
}
return nil
}
// Get gets the captcha KV from redis.
func (r *RedisStore) Get(key string, clear bool) string {
val, err := r.Redis.Get(key)
if err != nil {
logx.Errorw("error occurs when captcha key gets from redis", logx.Field("detail", err))
return ""
}
if clear {
_, err := r.Redis.Del(key)
if err != nil {
logx.Errorw("error occurs when captcha key deletes from redis", logx.Field("detail", err))
return ""
}
}
return val
}
// Verify verifies the captcha whether it is correct.
func (r *RedisStore) Verify(id, answer string, clear bool) bool {
key := r.PreKey + id
v := r.Get(key, clear)
return v == answer
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。