yxh пре 4 година
родитељ
комит
ed8453b004

+ 1 - 1
apiv1/common/captcha.go

@@ -13,7 +13,7 @@ type CaptchaReq struct {
 	g.Meta `path:"/get" tags:"get captcha" method:"get" summary:"获取验证码"`
 }
 type CaptchaRes struct {
-	g.Meta `mime:"application/json" example:"string"`
+	g.Meta `mime:"application/json" example:""`
 	Key    string `json:"key"`
 	Img    string `json:"img"`
 }

+ 2 - 2
apiv1/system/user.go

@@ -5,13 +5,13 @@ import (
 )
 
 type UserLoginReq struct {
-	g.Meta     `path:"/hello" tags:"Hello" method:"get" summary:"You first hello api"`
+	g.Meta     `path:"/login" tags:"login" method:"post" summary:"用户登录"`
 	Username   string `p:"username" v:"required#用户名不能为空"`
 	Password   string `p:"password" v:"required#密码不能为空"`
 	VerifyCode string `p:"verifyCode" v:"required#验证码不能为空"`
 	VerifyKey  string `p:"verifyKey"`
 }
 type UserLoginRes struct {
-	g.Meta `mime:"text/html" example:"string"`
+	g.Meta `mime:"text/html" example:""`
 	Data   g.Map `json:"data"`
 }

+ 21 - 0
internal/app/common/controller/base.go

@@ -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")
+}

+ 7 - 4
internal/app/common/controller/captcah.go

@@ -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,

+ 1 - 1
internal/app/common/router/router.go

@@ -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,

+ 21 - 16
internal/app/common/service/captcha.go

@@ -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)
 }

+ 1 - 1
internal/app/demo/router/router.go

@@ -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,
 		)

+ 24 - 0
internal/app/system/controller/base.go

@@ -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")
+}

+ 16 - 3
internal/app/system/controller/user.go

@@ -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
 }

+ 1 - 1
internal/app/system/router/router.go

@@ -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,
 		)

+ 6 - 16
library/libMiddleware/middleware.go

@@ -7,22 +7,12 @@
 
 package libMiddleware
 
-import (
-	"github.com/gogf/gf/v2/frame/g"
-	"github.com/gogf/gf/v2/net/ghttp"
-	"github.com/gogf/gf/v2/text/gstr"
-)
+import "github.com/gogf/gf/v2/net/ghttp"
 
-// ExceptionHandle 异常处理
-func ExceptionHandle(r *ghttp.Request) {
+func MiddlewareCORS(r *ghttp.Request) {
+	corsOptions := r.Response.DefaultCORSOptions()
+	// you can set options
+	//corsOptions.AllowDomain = []string{"goframe.org", "baidu.com"}
+	r.Response.CORS(corsOptions)
 	r.Middleware.Next()
-	if err := r.GetError(); err != nil {
-		msg := err.Error()
-		pos := gstr.Pos(msg, ":")
-		if pos > 0 {
-			msg = gstr.SubStr(msg, pos+2)
-		}
-		r.Response.ClearBuffer()
-		r.Response.WriteJson(g.Map{"code": 500, "message": msg})
-	}
 }