mirror of
https://gitee.com/shikong-sk/gofiber-study
synced 2025-02-23 23:32:15 +08:00
Service 优化
分离 业务逻辑到 Service
This commit is contained in:
parent
53642665ed
commit
48a6d4d32e
@ -10,6 +10,7 @@ import (
|
|||||||
"gofiber.study.skcks.cn/common/utils"
|
"gofiber.study.skcks.cn/common/utils"
|
||||||
"gofiber.study.skcks.cn/controller"
|
"gofiber.study.skcks.cn/controller"
|
||||||
"gofiber.study.skcks.cn/global"
|
"gofiber.study.skcks.cn/global"
|
||||||
|
"gofiber.study.skcks.cn/services"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Run() {
|
func Run() {
|
||||||
@ -66,9 +67,13 @@ func reloadApp(c *config.BasicConfig) *fiber.App {
|
|||||||
routes(global.App)
|
routes(global.App)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 路由注册
|
||||||
controller.SwaggerHandler(global.App)
|
controller.SwaggerHandler(global.App)
|
||||||
controller.RegisterController(global.App)
|
|
||||||
controller.ErrorHandler(global.App)
|
controller.ErrorHandler(global.App)
|
||||||
|
controller.RegisterController(global.App)
|
||||||
|
|
||||||
|
// 初始化服务
|
||||||
|
services.Init()
|
||||||
|
|
||||||
global.App.Hooks().OnListen(func() error {
|
global.App.Hooks().OnListen(func() error {
|
||||||
utils.MainAppExec(func() {
|
utils.MainAppExec(func() {
|
||||||
|
@ -6,8 +6,7 @@ import (
|
|||||||
"gofiber.study.skcks.cn/common/response"
|
"gofiber.study.skcks.cn/common/response"
|
||||||
"gofiber.study.skcks.cn/controller/types"
|
"gofiber.study.skcks.cn/controller/types"
|
||||||
"gofiber.study.skcks.cn/dto"
|
"gofiber.study.skcks.cn/dto"
|
||||||
"gofiber.study.skcks.cn/global"
|
"gofiber.study.skcks.cn/services/auth"
|
||||||
"gofiber.study.skcks.cn/model/generic/models"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Controller struct {
|
type Controller struct {
|
||||||
@ -43,20 +42,7 @@ func (c *Controller) Login() {
|
|||||||
return ctx.JSON(err)
|
return ctx.JSON(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
user := &models.User{Account: login.Account, Password: login.Password}
|
token, err := auth.Services.Login(login)
|
||||||
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,
|
|
||||||
})
|
|
||||||
if err = errorx.ParseError(err); err != nil {
|
if err = errorx.ParseError(err); err != nil {
|
||||||
return ctx.JSON(err)
|
return ctx.JSON(err)
|
||||||
}
|
}
|
||||||
|
@ -38,7 +38,7 @@ func (c *Controller) GetByAccount() {
|
|||||||
c.Router.Get("/account", func(ctx *fiber.Ctx) error {
|
c.Router.Get("/account", func(ctx *fiber.Ctx) error {
|
||||||
account := ctx.Query("account")
|
account := ctx.Query("account")
|
||||||
|
|
||||||
u, err := user.GetUserByAccount(account)
|
u, err := user.Services.GetUserByAccount(account)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ctx.JSON(errorx.NewDefaultError(err.Error()))
|
return ctx.JSON(errorx.NewDefaultError(err.Error()))
|
||||||
}
|
}
|
||||||
|
42
services/auth/auth.go
Normal file
42
services/auth/auth.go
Normal file
@ -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
|
||||||
|
}
|
11
services/services.go
Normal file
11
services/services.go
Normal file
@ -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()
|
||||||
|
}
|
@ -10,7 +10,16 @@ var (
|
|||||||
NotExists = errors.New("用户不存在")
|
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{
|
u := &models.User{
|
||||||
Account: account,
|
Account: account,
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user