mirror of
https://gitee.com/dromara/mayfly-go
synced 2026-02-03 11:25:47 +08:00
feat: 支持redis缓存权限等信息
This commit is contained in:
@@ -2,15 +2,25 @@ package captcha
|
||||
|
||||
import (
|
||||
"mayfly-go/pkg/biz"
|
||||
"mayfly-go/pkg/rediscli"
|
||||
"time"
|
||||
|
||||
"github.com/mojocn/base64Captcha"
|
||||
)
|
||||
|
||||
var store = base64Captcha.DefaultMemStore
|
||||
var store base64Captcha.Store
|
||||
var driver base64Captcha.Driver = base64Captcha.DefaultDriverDigit
|
||||
|
||||
// 生成验证码
|
||||
func Generate() (string, string) {
|
||||
if store == nil {
|
||||
if rediscli.GetCli() != nil {
|
||||
store = new(RedisStore)
|
||||
} else {
|
||||
store = base64Captcha.DefaultMemStore
|
||||
}
|
||||
}
|
||||
|
||||
c := base64Captcha.NewCaptcha(driver, store)
|
||||
// 获取
|
||||
id, b64s, err := c.Generate()
|
||||
@@ -26,3 +36,34 @@ func Verify(id string, val string) bool {
|
||||
// 同时清理掉这个图片
|
||||
return store.Verify(id, val, true)
|
||||
}
|
||||
|
||||
type RedisStore struct {
|
||||
}
|
||||
|
||||
const CAPTCHA = "mayfly:captcha:"
|
||||
|
||||
// 实现设置captcha的方法
|
||||
func (r RedisStore) Set(id string, value string) error {
|
||||
//time.Minute*2:有效时间2分钟
|
||||
rediscli.Set(CAPTCHA+id, value, time.Minute*2)
|
||||
return nil
|
||||
}
|
||||
|
||||
// 实现获取captcha的方法
|
||||
func (r RedisStore) Get(id string, clear bool) string {
|
||||
key := CAPTCHA + id
|
||||
val, err := rediscli.Get(key)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
if clear {
|
||||
//clear为true,验证通过,删除这个验证码
|
||||
rediscli.Del(key)
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
// 实现验证captcha的方法
|
||||
func (r RedisStore) Verify(id, answer string, clear bool) bool {
|
||||
return r.Get(id, clear) == answer
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user