refactor(captcha): 优化验证代码

This commit is contained in:
mh-swift 2024-07-29 13:54:06 +08:00
parent 804d5d5e59
commit 2e322e2606

View File

@ -7,12 +7,16 @@ package captcha
import ( import (
"context" "context"
"image/color"
"github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/text/gstr" "github.com/gogf/gf/v2/text/gstr"
"github.com/mojocn/base64Captcha" "github.com/mojocn/base64Captcha"
"image/color"
) )
// store 验证码存储方式
var store = base64Captcha.DefaultMemStore
// Generate 生成验证码 // Generate 生成验证码
func Generate(ctx context.Context) (id string, base64 string) { func Generate(ctx context.Context) (id string, base64 string) {
// 字符 // 字符
@ -47,7 +51,7 @@ func Generate(ctx context.Context) (id string, base64 string) {
Fonts: []string{"chromohv.ttf"}, Fonts: []string{"chromohv.ttf"},
} }
c := base64Captcha.NewCaptcha(driver.ConvertFonts(), base64Captcha.DefaultMemStore) c := base64Captcha.NewCaptcha(driver.ConvertFonts(), store)
id, base64, _, err := c.Generate() id, base64, _, err := c.Generate()
if err != nil { if err != nil {
g.Log().Errorf(ctx, "captcha.Generate err:%+v", err) g.Log().Errorf(ctx, "captcha.Generate err:%+v", err)
@ -60,6 +64,5 @@ func Verify(id, answer string) bool {
if id == "" || answer == "" { if id == "" || answer == "" {
return false return false
} }
c := base64Captcha.NewCaptcha(new(base64Captcha.DriverString), base64Captcha.DefaultMemStore) return store.Verify(id, gstr.ToLower(answer), true)
return c.Verify(id, gstr.ToLower(answer), true)
} }