From 6cb0fcfd93e48a6edd2bf971624e020bd93087d6 Mon Sep 17 00:00:00 2001 From: yazzyk Date: Wed, 25 Jun 2025 17:33:28 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=AA=8C=E8=AF=81=E7=A0=81?= =?UTF-8?q?=E9=80=89=E6=8B=A9=EF=BC=8C=E5=8F=AF=E8=87=AA=E7=94=B1=E9=80=89?= =?UTF-8?q?=E6=8B=A9=E5=AD=97=E7=AC=A6=E6=88=96=E8=AE=A1=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/internal/consts/captcha.go | 6 ++ .../internal/controller/admin/common/site.go | 6 +- server/internal/library/captcha/captcha.go | 71 +++++++++++-------- server/internal/model/config.go | 1 + 4 files changed, 52 insertions(+), 32 deletions(-) create mode 100644 server/internal/consts/captcha.go diff --git a/server/internal/consts/captcha.go b/server/internal/consts/captcha.go new file mode 100644 index 0000000..c5c7a75 --- /dev/null +++ b/server/internal/consts/captcha.go @@ -0,0 +1,6 @@ +package consts + +const ( + CaptchaTypeString = iota + 1 // 字符串 + CaptchaTypeMath // 数字计算 +) diff --git a/server/internal/controller/admin/common/site.go b/server/internal/controller/admin/common/site.go index da3462d..67a09c7 100644 --- a/server/internal/controller/admin/common/site.go +++ b/server/internal/controller/admin/common/site.go @@ -95,7 +95,11 @@ func (c *cSite) LoginConfig(ctx context.Context, _ *common.SiteLoginConfigReq) ( // Captcha 登录验证码 func (c *cSite) Captcha(ctx context.Context, _ *common.LoginCaptchaReq) (res *common.LoginCaptchaRes, err error) { - cid, base64 := captcha.Generate(ctx) + loginConf, err := service.SysConfig().GetLogin(ctx) + if err != nil { + return + } + cid, base64 := captcha.Generate(ctx, loginConf.CaptchaType) res = &common.LoginCaptchaRes{Cid: cid, Base64: base64} return } diff --git a/server/internal/library/captcha/captcha.go b/server/internal/library/captcha/captcha.go index 3ee3863..6cdca8c 100644 --- a/server/internal/library/captcha/captcha.go +++ b/server/internal/library/captcha/captcha.go @@ -7,6 +7,7 @@ package captcha import ( "context" + "hotgo/internal/consts" "image/color" "github.com/gogf/gf/v2/frame/g" @@ -18,44 +19,52 @@ import ( var store = base64Captcha.DefaultMemStore // Generate 生成验证码 -func Generate(ctx context.Context) (id string, base64 string) { - // 字符 - // driver := &base64Captcha.DriverString{ - // Height: 42, - // Width: 100, - // //NoiseCount: 50, - // //ShowLineOptions: 20, - // Length: 4, - // BgColor: &color.RGBA{ - // R: 255, - // G: 250, - // B: 250, - // A: 250, - // }, - // Source: "0123456789", // abcdefghjkmnpqrstuvwxyz23456789 - // Fonts: []string{"chromohv.ttf"}, - // } +func Generate(ctx context.Context, captchaType int) (id string, base64 string) { + var err error + switch captchaType { // 算数 - driver := &base64Captcha.DriverMath{ - Height: 42, - Width: 100, - NoiseCount: 0, - ShowLineOptions: 0, - BgColor: &color.RGBA{ - R: 255, - G: 250, - B: 250, - A: 250, - }, - Fonts: []string{"chromohv.ttf"}, + case consts.CaptchaTypeMath: + driver := &base64Captcha.DriverMath{ + Height: 42, + Width: 100, + NoiseCount: 0, + ShowLineOptions: 0, + BgColor: &color.RGBA{ + R: 255, + G: 250, + B: 250, + A: 250, + }, + Fonts: []string{"chromohv.ttf"}, + } + c := base64Captcha.NewCaptcha(driver.ConvertFonts(), store) + id, base64, _, err = c.Generate() + // 字符 + default: + driver := &base64Captcha.DriverString{ + Height: 42, + Width: 100, + //NoiseCount: 50, + //ShowLineOptions: 20, + Length: 4, + BgColor: &color.RGBA{ + R: 255, + G: 250, + B: 250, + A: 250, + }, + Source: "abcdefghjkmnpqrstuvwxyz23456789", // abcdefghjkmnpqrstuvwxyz23456789 + Fonts: []string{"chromohv.ttf"}, + } + c := base64Captcha.NewCaptcha(driver.ConvertFonts(), store) + id, base64, _, err = c.Generate() } - c := base64Captcha.NewCaptcha(driver.ConvertFonts(), store) - id, base64, _, err := c.Generate() if err != nil { g.Log().Errorf(ctx, "captcha.Generate err:%+v", err) } + return } diff --git a/server/internal/model/config.go b/server/internal/model/config.go index 2ada6cb..dc333e6 100644 --- a/server/internal/model/config.go +++ b/server/internal/model/config.go @@ -176,6 +176,7 @@ type WechatConfig struct { type LoginConfig struct { RegisterSwitch int `json:"loginRegisterSwitch"` CaptchaSwitch int `json:"loginCaptchaSwitch"` + CaptchaType int `json:"loginCaptchaType"` Avatar string `json:"loginAvatar"` RoleId int64 `json:"loginRoleId"` DeptId int64 `json:"loginDeptId"`