You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.0 KiB

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