v1
This commit is contained in:
67
internal/erpserver/handler/auth/auth.go
Normal file
67
internal/erpserver/handler/auth/auth.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"management/internal/erpserver/model/system/request"
|
||||
v1 "management/internal/erpserver/service/v1"
|
||||
authv1 "management/internal/erpserver/service/v1/auth"
|
||||
"management/internal/pkg/gin/gu"
|
||||
|
||||
"github.com/drhin/logger"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/zhang2092/browser"
|
||||
)
|
||||
|
||||
type app struct {
|
||||
log *logger.Logger
|
||||
captchaService v1.CaptchaService
|
||||
userService v1.UserService
|
||||
authService *authv1.Auth
|
||||
}
|
||||
|
||||
func newApp(
|
||||
log *logger.Logger,
|
||||
captchaService v1.CaptchaService,
|
||||
userService v1.UserService,
|
||||
authService *authv1.Auth,
|
||||
) *app {
|
||||
return &app{
|
||||
log: log,
|
||||
captchaService: captchaService,
|
||||
userService: userService,
|
||||
authService: authService,
|
||||
}
|
||||
}
|
||||
|
||||
func (a *app) login(c *gin.Context) {
|
||||
var req request.Login
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
gu.ValidatorErrors(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
if !a.captchaService.Verify(req.CaptchaID, req.Captcha, true) {
|
||||
gu.Failed(c, "验证码错误")
|
||||
return
|
||||
}
|
||||
|
||||
req.Ip = c.ClientIP()
|
||||
req.Url = c.Request.URL.String()
|
||||
req.Referrer = c.Request.Referer()
|
||||
br, err := browser.NewBrowser(c.Request.UserAgent())
|
||||
if err == nil {
|
||||
req.Os = br.Platform().Name()
|
||||
req.Browser = br.Name()
|
||||
}
|
||||
|
||||
risk, err := a.authService.Authenticate(c, req)
|
||||
if err != nil {
|
||||
gu.Failed(c, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
gu.Ok(c, risk)
|
||||
}
|
||||
|
||||
func (a *app) logout(c *gin.Context) {
|
||||
gu.Ok(c, nil)
|
||||
}
|
||||
25
internal/erpserver/handler/auth/route.go
Normal file
25
internal/erpserver/handler/auth/route.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
v1 "management/internal/erpserver/service/v1"
|
||||
"management/internal/erpserver/service/v1/auth"
|
||||
|
||||
"github.com/drhin/logger"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Log *logger.Logger
|
||||
CaptchaService v1.CaptchaService
|
||||
AuthService *auth.Auth
|
||||
UserService v1.UserService
|
||||
MenuService v1.MenuService
|
||||
}
|
||||
|
||||
func Routes(public *gin.RouterGroup, private *gin.RouterGroup, cfg Config) {
|
||||
app := newApp(cfg.Log, cfg.CaptchaService, cfg.UserService, cfg.AuthService)
|
||||
|
||||
public.POST("/login", app.login)
|
||||
private.GET("/logout", app.logout)
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user