From 48a6d4d32e2413e399b0f610bdcf03c5a7940e97 Mon Sep 17 00:00:00 2001 From: Shikong <919411476@qq.com> Date: Thu, 6 Oct 2022 17:58:11 +0800 Subject: [PATCH] =?UTF-8?q?Service=20=E4=BC=98=E5=8C=96=20=E5=88=86?= =?UTF-8?q?=E7=A6=BB=20=E4=B8=9A=E5=8A=A1=E9=80=BB=E8=BE=91=E5=88=B0=20Ser?= =?UTF-8?q?vice?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/app.go | 7 ++++++- controller/auth/auth.go | 18 ++---------------- controller/user/user.go | 2 +- services/auth/auth.go | 42 +++++++++++++++++++++++++++++++++++++++++ services/services.go | 11 +++++++++++ services/user/user.go | 11 ++++++++++- 6 files changed, 72 insertions(+), 19 deletions(-) create mode 100644 services/auth/auth.go create mode 100644 services/services.go diff --git a/app/app.go b/app/app.go index 7a5ef6e..d1b5ec6 100644 --- a/app/app.go +++ b/app/app.go @@ -10,6 +10,7 @@ import ( "gofiber.study.skcks.cn/common/utils" "gofiber.study.skcks.cn/controller" "gofiber.study.skcks.cn/global" + "gofiber.study.skcks.cn/services" ) func Run() { @@ -66,9 +67,13 @@ func reloadApp(c *config.BasicConfig) *fiber.App { routes(global.App) } + // 路由注册 controller.SwaggerHandler(global.App) - controller.RegisterController(global.App) controller.ErrorHandler(global.App) + controller.RegisterController(global.App) + + // 初始化服务 + services.Init() global.App.Hooks().OnListen(func() error { utils.MainAppExec(func() { diff --git a/controller/auth/auth.go b/controller/auth/auth.go index f73fe6b..8c5fd56 100644 --- a/controller/auth/auth.go +++ b/controller/auth/auth.go @@ -6,8 +6,7 @@ import ( "gofiber.study.skcks.cn/common/response" "gofiber.study.skcks.cn/controller/types" "gofiber.study.skcks.cn/dto" - "gofiber.study.skcks.cn/global" - "gofiber.study.skcks.cn/model/generic/models" + "gofiber.study.skcks.cn/services/auth" ) type Controller struct { @@ -43,20 +42,7 @@ func (c *Controller) Login() { return ctx.JSON(err) } - user := &models.User{Account: login.Account, Password: login.Password} - exist, err := global.DataSources.Get(user) - if err = errorx.ParseError(err); err != nil { - return ctx.JSON(err) - } - - if !exist { - return ctx.JSON(errorx.NewDefaultError("账号或密码错误")) - } - - token, err := global.GetToken(global.UserClaims{ - Id: user.Id, - Account: user.Account, - }) + token, err := auth.Services.Login(login) if err = errorx.ParseError(err); err != nil { return ctx.JSON(err) } diff --git a/controller/user/user.go b/controller/user/user.go index a269cc2..5983540 100644 --- a/controller/user/user.go +++ b/controller/user/user.go @@ -38,7 +38,7 @@ func (c *Controller) GetByAccount() { c.Router.Get("/account", func(ctx *fiber.Ctx) error { account := ctx.Query("account") - u, err := user.GetUserByAccount(account) + u, err := user.Services.GetUserByAccount(account) if err != nil { return ctx.JSON(errorx.NewDefaultError(err.Error())) } diff --git a/services/auth/auth.go b/services/auth/auth.go new file mode 100644 index 0000000..81e5947 --- /dev/null +++ b/services/auth/auth.go @@ -0,0 +1,42 @@ +package auth + +import ( + "errors" + "gofiber.study.skcks.cn/common/errorx" + "gofiber.study.skcks.cn/dto" + "gofiber.study.skcks.cn/global" + "gofiber.study.skcks.cn/model/generic/models" +) + +var ( + AuthFailed = errors.New("账号或密码错误") +) + +type Service struct { +} + +var Services *Service + +func InitService() { + Services = &Service{} +} + +func (s *Service) Login(login *dto.Login) (token string, err error) { + user := &models.User{Account: login.Account, Password: login.Password} + exist, err := global.DataSources.Get(user) + if err != nil { + return + } + + if !exist { + return token, AuthFailed + } + + token, err = global.GetToken(global.UserClaims{ + Id: user.Id, + Account: user.Account, + }) + + err = errorx.ParseError(err) + return +} diff --git a/services/services.go b/services/services.go new file mode 100644 index 0000000..b703a29 --- /dev/null +++ b/services/services.go @@ -0,0 +1,11 @@ +package services + +import ( + "gofiber.study.skcks.cn/services/auth" + "gofiber.study.skcks.cn/services/user" +) + +func Init() { + auth.InitService() + user.InitService() +} diff --git a/services/user/user.go b/services/user/user.go index b5b9e11..0b25cb7 100644 --- a/services/user/user.go +++ b/services/user/user.go @@ -10,7 +10,16 @@ var ( NotExists = errors.New("用户不存在") ) -func GetUserByAccount(account string) (*models.User, error) { +type Service struct { +} + +var Services *Service + +func InitService() { + Services = &Service{} +} + +func (s *Service) GetUserByAccount(account string) (*models.User, error) { u := &models.User{ Account: account, }