12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- package services
- import (
- "eta/eta_mini_api/services/go_redis"
- "eta/eta_mini_api/utils"
- "fmt"
- "time"
- )
- // CaptchaRedis Redis验证器
- type CaptchaRedis struct{}
- // Set 实现验证器set方法
- func (r CaptchaRedis) Set(id string, value string) (err error) {
- if utils.Redis == nil {
- err = fmt.Errorf("redis config err")
- return
- }
- key := utils.CaptchaCachePrefix + id
- b := go_redis.SetNX(key, value, time.Minute*5)
- if !b {
- err = fmt.Errorf("redis setnx err")
- }
- return
- }
- // Get 实现原验证器get方法
- func (r CaptchaRedis) Get(id string, clear bool) (code string) {
- if utils.Redis == nil {
- return
- }
- key := utils.CaptchaCachePrefix + id
- val, err := go_redis.RedisString(key)
- if err != nil {
- return
- }
- code = val
- // clear为true时验证通过删除验证码
- if clear {
- _ = go_redis.Delete(key)
- }
- return
- }
- // Verify 实现原验证器verify方法
- func (r CaptchaRedis) Verify(id, answer string, clear bool) bool {
- v := r.Get(id, clear)
- return v == answer
- }
|