From 2e322e2606acc1b0106639c90bf171c86f89e9c1 Mon Sep 17 00:00:00 2001 From: mh-swift Date: Mon, 29 Jul 2024 13:54:06 +0800 Subject: [PATCH] =?UTF-8?q?refactor(captcha):=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E9=AA=8C=E8=AF=81=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/internal/library/captcha/captcha.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/server/internal/library/captcha/captcha.go b/server/internal/library/captcha/captcha.go index cdcccdc..3ee3863 100644 --- a/server/internal/library/captcha/captcha.go +++ b/server/internal/library/captcha/captcha.go @@ -7,12 +7,16 @@ package captcha import ( "context" + "image/color" + "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/text/gstr" "github.com/mojocn/base64Captcha" - "image/color" ) +// store 验证码存储方式 +var store = base64Captcha.DefaultMemStore + // Generate 生成验证码 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"}, } - c := base64Captcha.NewCaptcha(driver.ConvertFonts(), base64Captcha.DefaultMemStore) + c := base64Captcha.NewCaptcha(driver.ConvertFonts(), store) id, base64, _, err := c.Generate() if err != nil { g.Log().Errorf(ctx, "captcha.Generate err:%+v", err) @@ -60,6 +64,5 @@ func Verify(id, answer string) bool { if id == "" || answer == "" { return false } - c := base64Captcha.NewCaptcha(new(base64Captcha.DriverString), base64Captcha.DefaultMemStore) - return c.Verify(id, gstr.ToLower(answer), true) + return store.Verify(id, gstr.ToLower(answer), true) }