123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- package services
- import (
- "eta/eta_mobile/utils"
- "fmt"
- "time"
- )
- // CaptchaRedis Redis验证器
- type CaptchaRedis struct{}
- // Set 实现验证器set方法
- func (r CaptchaRedis) Set(id string, value string) (err error) {
- if utils.Rc == nil {
- err = fmt.Errorf("redis config err")
- return
- }
- key := utils.CaptchaCachePrefix + id
- b := utils.Rc.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.Rc == nil {
- return
- }
- key := utils.CaptchaCachePrefix + id
- val, err := utils.Rc.RedisString(key)
- if err != nil {
- return
- }
- code = val
- // clear为true时验证通过删除验证码
- if clear {
- _ = utils.Rc.Delete(key)
- }
- return
- }
- // Verify 实现原验证器verify方法
- func (r CaptchaRedis) Verify(id, answer string, clear bool) bool {
- v := r.Get(id, clear)
- return v == answer
- }
|