mirror of
https://github.com/tiger1103/gfast.git
synced 2026-09-23 02:40:54 +00:00
20220304
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* @desc:
|
||||
* @company:云南奇讯科技有限公司
|
||||
* @Author: yixiaohu
|
||||
* @Date: 2022/3/4 18:19
|
||||
*/
|
||||
|
||||
package controller
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
)
|
||||
|
||||
type BaseController struct {
|
||||
}
|
||||
|
||||
// Init 自动执行的初始化方法
|
||||
func (c *BaseController) Init(r *ghttp.Request) {
|
||||
g.Log().Debug(r.GetCtx(), "AAAAAAAAAAAAAAAAAAAAAAAAA")
|
||||
}
|
||||
@@ -13,14 +13,17 @@ import (
|
||||
"github.com/tiger1103/gfast/v3/internal/app/common/service"
|
||||
)
|
||||
|
||||
var Captcha = cCaptcha{}
|
||||
var Captcha = captchaController{}
|
||||
|
||||
type cCaptcha struct {
|
||||
type captchaController struct {
|
||||
}
|
||||
|
||||
// Get 获取验证码
|
||||
func (c *cCaptcha) Get(ctx context.Context, req *common.CaptchaReq) (res *common.CaptchaRes, err error) {
|
||||
idKeyC, base64stringC := service.Captcha.GetVerifyImgString(ctx)
|
||||
func (c *captchaController) Get(ctx context.Context, req *common.CaptchaReq) (res *common.CaptchaRes, err error) {
|
||||
var (
|
||||
idKeyC, base64stringC string
|
||||
)
|
||||
idKeyC, base64stringC, err = service.Captcha.GetVerifyImgString(ctx)
|
||||
res = &common.CaptchaRes{
|
||||
Key: idKeyC,
|
||||
Img: base64stringC,
|
||||
|
||||
@@ -15,7 +15,7 @@ import (
|
||||
|
||||
func BindController(group *ghttp.RouterGroup) {
|
||||
group.Group("/pub", func(group *ghttp.RouterGroup) {
|
||||
group.Middleware(libMiddleware.ExceptionHandle)
|
||||
group.Middleware(libMiddleware.MiddlewareCORS)
|
||||
group.Group("/captcha", func(group *ghttp.RouterGroup) {
|
||||
group.Bind(
|
||||
controller.Captcha,
|
||||
|
||||
@@ -4,16 +4,20 @@ import (
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
"github.com/mojocn/base64Captcha"
|
||||
"github.com/tiger1103/gfast/v3/library/liberr"
|
||||
)
|
||||
|
||||
type captcha struct{}
|
||||
type ICaptcha interface {
|
||||
GetVerifyImgString(ctx context.Context) (idKeyC string, base64stringC string, err error)
|
||||
VerifyString(id, answer string) bool
|
||||
}
|
||||
|
||||
var Captcha = new(captcha)
|
||||
type captchaImpl struct {
|
||||
driver *base64Captcha.DriverString
|
||||
store base64Captcha.Store
|
||||
}
|
||||
|
||||
// GetVerifyImgString 获取字母数字混合验证码
|
||||
func (s *captcha) GetVerifyImgString(ctx context.Context) (idKeyC string, base64stringC string) {
|
||||
driver := &base64Captcha.DriverString{
|
||||
var Captcha ICaptcha = &captchaImpl{
|
||||
driver: &base64Captcha.DriverString{
|
||||
Height: 80,
|
||||
Width: 240,
|
||||
NoiseCount: 50,
|
||||
@@ -21,20 +25,21 @@ func (s *captcha) GetVerifyImgString(ctx context.Context) (idKeyC string, base64
|
||||
Length: 4,
|
||||
Source: "abcdefghjkmnpqrstuvwxyz23456789",
|
||||
Fonts: []string{"chromohv.ttf"},
|
||||
}
|
||||
driver = driver.ConvertFonts()
|
||||
store := base64Captcha.DefaultMemStore
|
||||
c := base64Captcha.NewCaptcha(driver, store)
|
||||
idKeyC, base64stringC, err := c.Generate()
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
},
|
||||
store: base64Captcha.DefaultMemStore,
|
||||
}
|
||||
|
||||
// GetVerifyImgString 获取字母数字混合验证码
|
||||
func (s *captchaImpl) GetVerifyImgString(ctx context.Context) (idKeyC string, base64stringC string, err error) {
|
||||
driver := s.driver.ConvertFonts()
|
||||
c := base64Captcha.NewCaptcha(driver, s.store)
|
||||
idKeyC, base64stringC, err = c.Generate()
|
||||
return
|
||||
}
|
||||
|
||||
// VerifyString 验证输入的验证码是否正确
|
||||
func (s *captcha) VerifyString(id, answer string) bool {
|
||||
driver := new(base64Captcha.DriverString)
|
||||
store := base64Captcha.DefaultMemStore
|
||||
c := base64Captcha.NewCaptcha(driver, store)
|
||||
func (s *captchaImpl) VerifyString(id, answer string) bool {
|
||||
c := base64Captcha.NewCaptcha(s.driver, s.store)
|
||||
answer = gstr.ToLower(answer)
|
||||
return c.Verify(id, answer, true)
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ import (
|
||||
|
||||
func BindController(group *ghttp.RouterGroup) {
|
||||
group.Group("/demo", func(group *ghttp.RouterGroup) {
|
||||
group.Middleware(libMiddleware.ExceptionHandle)
|
||||
group.Middleware(libMiddleware.MiddlewareCORS)
|
||||
group.Bind(
|
||||
controller.Demo,
|
||||
)
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* @desc:system base controller
|
||||
* @company:云南奇讯科技有限公司
|
||||
* @Author: yixiaohu
|
||||
* @Date: 2022/3/4 18:12
|
||||
*/
|
||||
|
||||
package controller
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
commonController "github.com/tiger1103/gfast/v3/internal/app/common/controller"
|
||||
)
|
||||
|
||||
type baseController struct {
|
||||
commonController.BaseController
|
||||
}
|
||||
|
||||
// Init 自动执行的初始化方法
|
||||
func (c *baseController) Init(r *ghttp.Request) {
|
||||
c.BaseController.Init(r)
|
||||
g.Log().Debug(r.GetCtx(), "BBBBBBBBBBBBBBBB")
|
||||
}
|
||||
@@ -2,15 +2,28 @@ package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/os/genv"
|
||||
"github.com/tiger1103/gfast/v3/apiv1/system"
|
||||
commonService "github.com/tiger1103/gfast/v3/internal/app/common/service"
|
||||
)
|
||||
|
||||
var (
|
||||
User = cUser{}
|
||||
User = UserController{}
|
||||
)
|
||||
|
||||
type cUser struct{}
|
||||
type UserController struct {
|
||||
baseController
|
||||
}
|
||||
|
||||
func (h *cUser) Login(ctx context.Context, req *system.UserLoginReq) (res *system.UserLoginRes, err error) {
|
||||
func (c *UserController) Login(ctx context.Context, req *system.UserLoginReq) (res *system.UserLoginRes, err error) {
|
||||
//判断验证码是否正确
|
||||
debug := genv.GetWithCmd("gf.debug")
|
||||
if debug.Int() != 1 {
|
||||
if !commonService.Captcha.VerifyString(req.VerifyKey, req.VerifyCode) {
|
||||
err = gerror.New("验证码输入错误")
|
||||
return
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ import (
|
||||
|
||||
func BindController(group *ghttp.RouterGroup) {
|
||||
group.Group("/system", func(group *ghttp.RouterGroup) {
|
||||
group.Middleware(libMiddleware.ExceptionHandle)
|
||||
group.Middleware(libMiddleware.MiddlewareCORS)
|
||||
group.Bind(
|
||||
controller.User,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user