From 384677f071992a0880e252b87c6ddfb86c9b7d4f Mon Sep 17 00:00:00 2001 From: yanghao05 Date: Wed, 1 Feb 2023 18:50:59 +0800 Subject: [PATCH] fix issues --- config.toml | 9 ++++- modules/system/container/container.go | 2 +- modules/system/controller/captcha.go | 44 +++++++++++++++++++++++++ modules/system/controller/controller.go | 27 --------------- modules/system/dto/captcha.go | 8 +++++ modules/system/dto/name.go | 5 --- modules/system/routes/routes.go | 10 +++--- providers/config/loader.go | 1 + providers/config/section_captcha.go | 22 +++++++++++++ 9 files changed, 89 insertions(+), 39 deletions(-) create mode 100755 modules/system/controller/captcha.go delete mode 100644 modules/system/controller/controller.go create mode 100644 modules/system/dto/captcha.go delete mode 100644 modules/system/dto/name.go create mode 100644 providers/config/section_captcha.go diff --git a/config.toml b/config.toml index feb7629..0b7a802 100644 --- a/config.toml +++ b/config.toml @@ -1,6 +1,13 @@ [App] Mode = "debug" +[Captcha] +KeyLong = 6 +ImgWidth = 240 #验证码宽度 +ImgHeight = 80 # 验证码高度 +OpenCaptcha = 0 # 防爆破验证码开启此数,0代表每次登录都需要验证码,其他数字代表错误密码此数,如3代表错误三次后出现验证码 +OpenCaptchaTimeOut = 1h # 防爆破验证码超时时间,单位:s(秒) + [Storage] Driver = "local" @@ -90,7 +97,7 @@ Port = 3306 Database = "demos11" Username = "root" Password = "root" -Prefix="" +Prefix = "" Singular = false MaxIdleConns = 10 MaxOpenConns = 200 diff --git a/modules/system/container/container.go b/modules/system/container/container.go index be2ea6c..24fe7bb 100644 --- a/modules/system/container/container.go +++ b/modules/system/container/container.go @@ -20,7 +20,7 @@ func init() { log.Fatal(err) } - if err := container.Container.Provide(controller.NewController); err != nil { + if err := container.Container.Provide(controller.NewCaptchaController); err != nil { log.Fatal(err) } diff --git a/modules/system/controller/captcha.go b/modules/system/controller/captcha.go new file mode 100755 index 0000000..227e6a3 --- /dev/null +++ b/modules/system/controller/captcha.go @@ -0,0 +1,44 @@ +package controller + +import ( + "atom/modules/system/dto" + "atom/providers/config" + "errors" + + "github.com/gin-gonic/gin" + "github.com/mojocn/base64Captcha" +) + +type CaptchaController interface { + Show(*gin.Context) (dto.SysCaptchaResponse, error) +} + +type captchaControllerImpl struct { + conf *config.Config +} + +func NewCaptchaController(conf *config.Config) CaptchaController { + return &captchaControllerImpl{conf: conf} +} + +func (c *captchaControllerImpl) Show(ctx *gin.Context) (dto.SysCaptchaResponse, error) { + // 判断验证码是否开启 + var store = base64Captcha.DefaultMemStore + + // 字符,公式,验证码配置 + // 生成默认数字的driver + driver := base64Captcha.NewDriverDigit(c.conf.Captcha.ImgHeight, c.conf.Captcha.ImgWidth, c.conf.Captcha.KeyLong, 0.7, 80) + // cp := base64Captcha.NewCaptcha(driver, store.UseWithCtx(c)) // v8下使用redis + cp := base64Captcha.NewCaptcha(driver, store) + id, b64s, err := cp.Generate() + if err != nil { + return dto.SysCaptchaResponse{}, errors.New("验证码获取失败") + } + + return dto.SysCaptchaResponse{ + CaptchaId: id, + PicPath: b64s, + CaptchaLength: c.conf.Captcha.KeyLong, + OpenCaptcha: c.conf.Captcha.OpenCaptcha != 0, + }, nil +} diff --git a/modules/system/controller/controller.go b/modules/system/controller/controller.go deleted file mode 100644 index d100c2f..0000000 --- a/modules/system/controller/controller.go +++ /dev/null @@ -1,27 +0,0 @@ -package controller - -import ( - "atom/modules/system/dao" - "atom/modules/system/dto" - "atom/modules/system/service" - "atom/providers/config" - - "github.com/gin-gonic/gin" -) - -type Controller interface { - GetName(*gin.Context) (dto.Name, error) -} - -type ControllerImpl struct { - Conf *config.Config - svc service.SystemService -} - -func NewController(Conf *config.Config, dao dao.Dao, svc service.SystemService) Controller { - return &ControllerImpl{Conf: Conf, svc: svc} -} - -func (c *ControllerImpl) GetName(ctx *gin.Context) (dto.Name, error) { - return c.svc.GetName(ctx) -} diff --git a/modules/system/dto/captcha.go b/modules/system/dto/captcha.go new file mode 100644 index 0000000..7a24cf2 --- /dev/null +++ b/modules/system/dto/captcha.go @@ -0,0 +1,8 @@ +package dto + +type SysCaptchaResponse struct { + CaptchaId string `json:"captcha_id,omitempty"` + PicPath string `json:"pic_path,omitempty"` + CaptchaLength int `json:"captcha_length,omitempty"` + OpenCaptcha bool `json:"open_captcha,omitempty"` +} diff --git a/modules/system/dto/name.go b/modules/system/dto/name.go deleted file mode 100644 index 927cbf0..0000000 --- a/modules/system/dto/name.go +++ /dev/null @@ -1,5 +0,0 @@ -package dto - -type Name struct { - Name string `json:"name"` -} diff --git a/modules/system/routes/routes.go b/modules/system/routes/routes.go index 6ff74eb..338bd5a 100644 --- a/modules/system/routes/routes.go +++ b/modules/system/routes/routes.go @@ -9,14 +9,14 @@ import ( ) type Route struct { - controller controller.Controller - svc *http.Service + captcha controller.CaptchaController + svc *http.Service } -func NewRoute(c controller.Controller, svc *http.Service) contracts.Route { - return &Route{controller: c, svc: svc} +func NewRoute(captcha controller.CaptchaController, svc *http.Service) contracts.Route { + return &Route{captcha: captcha, svc: svc} } func (r *Route) Register() { - r.svc.Engine.GET("/name", gen.DataFunc(r.controller.GetName)) + r.svc.Engine.GET("/captcha", gen.DataFunc(r.captcha.GetName)) } diff --git a/providers/config/loader.go b/providers/config/loader.go index 8a84757..cb6d194 100644 --- a/providers/config/loader.go +++ b/providers/config/loader.go @@ -12,6 +12,7 @@ import ( type Config struct { App App + Captcha Captcha Http Http Log Log Database Database diff --git a/providers/config/section_captcha.go b/providers/config/section_captcha.go new file mode 100644 index 0000000..56ce875 --- /dev/null +++ b/providers/config/section_captcha.go @@ -0,0 +1,22 @@ +package config + +import ( + "log" + "time" +) + +type Captcha struct { + KeyLong int // 验证码长度 + ImgWidth int // 验证码宽度 + ImgHeight int // 验证码高度 + OpenCaptcha int // 防爆破验证码开启此数,0代表每次登录都需要验证码,其他数字代表错误密码此数,如3代表错误三次后出现验证码 + OpenCaptchaTimeOut string // 防爆破验证码超时时间,单位:s(秒) +} + +func (c *Captcha) OpenCaptchaTimeOutDuration() time.Duration { + d, err := time.ParseDuration(c.OpenCaptchaTimeOut) + if err != nil { + log.Panic(err) + } + return d +}