Files
mayfly-go/server/internal/auth/pkg/captcha/captcha.go
meilin.huang e56788af3e refactor: dbm
2024-12-08 13:04:23 +08:00

68 lines
1.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package captcha
import (
"mayfly-go/pkg/rediscli"
"time"
"github.com/mojocn/base64Captcha"
)
var store base64Captcha.Store
var driver base64Captcha.Driver = base64Captcha.DefaultDriverDigit
// 生成验证码
func Generate() (string, string, error) {
if store == nil {
if rediscli.GetCli() != nil {
store = new(RedisStore)
} else {
store = base64Captcha.DefaultMemStore
}
}
c := base64Captcha.NewCaptcha(driver, store)
// 获取
id, b64s, _, err := c.Generate()
return id, b64s, err
}
// 验证验证码
func Verify(id string, val string) bool {
if store == nil || id == "" || val == "" {
return false
}
// 同时清理掉这个图片
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
}