support captcha

This commit is contained in:
yanghao05
2023-02-04 16:24:08 +08:00
parent 384677f071
commit 1c2b861ac7
13 changed files with 138 additions and 57 deletions

View File

@@ -1,44 +1,31 @@
package controller
import (
"atom/modules/system/dto"
"atom/providers/captcha"
"atom/providers/config"
"errors"
"github.com/gin-gonic/gin"
"github.com/mojocn/base64Captcha"
)
type CaptchaController interface {
Show(*gin.Context) (dto.SysCaptchaResponse, error)
Show(*gin.Context) (*captcha.CaptchaResponse, error)
}
type captchaControllerImpl struct {
conf *config.Config
conf *config.Config
captcha *captcha.Captcha
}
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("验证码获取失败")
func NewCaptchaController(
conf *config.Config,
captcha *captcha.Captcha,
) CaptchaController {
return &captchaControllerImpl{
conf: conf,
captcha: captcha,
}
return dto.SysCaptchaResponse{
CaptchaId: id,
PicPath: b64s,
CaptchaLength: c.conf.Captcha.KeyLong,
OpenCaptcha: c.conf.Captcha.OpenCaptcha != 0,
}, nil
}
func (c *captchaControllerImpl) Show(ctx *gin.Context) (*captcha.CaptchaResponse, error) {
return c.captcha.Generate()
}

View File

@@ -1,8 +1 @@
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"`
}

View File

@@ -18,5 +18,5 @@ func NewRoute(captcha controller.CaptchaController, svc *http.Service) contracts
}
func (r *Route) Register() {
r.svc.Engine.GET("/captcha", gen.DataFunc(r.captcha.GetName))
r.svc.Engine.GET("/captcha", gen.DataFunc(r.captcha.Show))
}

View File

@@ -2,12 +2,9 @@ package service
import (
"atom/modules/system/dao"
"atom/modules/system/dto"
"context"
)
type SystemService interface {
GetName(ctx context.Context) (dto.Name, error)
}
type systemService struct {
@@ -17,10 +14,3 @@ type systemService struct {
func NewSystemService(dao dao.Dao) SystemService {
return &systemService{dao: dao}
}
func (svc *systemService) GetName(ctx context.Context) (dto.Name, error) {
if err := svc.dao.Release(ctx, 10, "Rogee"); err != nil {
return dto.Name{}, err
}
return dto.Name{Name: "System.GetName"}, nil
}