PersonalMain-Golang/internal/controller/auth/authController.go

88 lines
2.4 KiB
Go
Raw Permalink Normal View History

package auth
2023-12-24 15:17:21 +08:00
import (
2023-12-27 01:48:24 +08:00
"PersonalMain/api"
2023-12-24 15:17:21 +08:00
"PersonalMain/api/request"
"PersonalMain/internal/model/entity"
"PersonalMain/internal/service/UserService"
"PersonalMain/utility/ErrorCode"
"PersonalMain/utility/ResultUtil"
2023-12-24 15:17:21 +08:00
"context"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
2023-12-24 15:17:21 +08:00
)
2023-12-26 16:29:04 +08:00
func userService() UserService.UserService {
return UserService.NewUserService()
}
2023-12-27 01:48:24 +08:00
type ControllerV1 struct{}
func NewAuthV1() api.IAuthV1 {
return &ControllerV1{}
}
// AuthRegister
//
// 用户注册
2023-12-26 16:04:24 +08:00
func (*ControllerV1) AuthRegister(ctx context.Context, _ *request.RegisterReq) (res *request.RegisterRes, err error) {
userRegister := entity.UserRegisterVO{}
req := ghttp.RequestFromCtx(ctx)
// 获取 model 表单信息
errStruct := req.GetRequestStruct(&userRegister)
if errStruct == nil {
errStruct := g.Validator().Data(userRegister).Run(ctx)
if errStruct == nil {
// 进行用户注册
2023-12-26 16:29:04 +08:00
userService().UserRegister(req, &userRegister)
} else {
2023-12-27 01:48:24 +08:00
g.Log().Cat("Struct").Cat("Auth").Error(ctx, errStruct.Error())
ResultUtil.Error(req, ErrorCode.RequestBodyMismatching, errStruct.Map())
}
} else {
2023-12-27 01:48:24 +08:00
g.Log().Cat("Struct").Cat("Auth").Error(ctx, errStruct.Error())
ResultUtil.Error(req, ErrorCode.RequestBodyError, errStruct.Error())
}
return res, err
}
2023-12-26 16:04:24 +08:00
func (*ControllerV1) AuthLogin(ctx context.Context, _ *request.LoginReq) (res *request.LoginRes, err error) {
userLogin := entity.UserLoginVO{}
req := ghttp.RequestFromCtx(ctx)
// 获取 model 表单信息
errStruct := req.GetRequestStruct(&userLogin)
if errStruct == nil {
errStruct := g.Validator().Data(userLogin).Run(ctx)
if errStruct == nil {
// 进行用户注册
2023-12-26 16:29:04 +08:00
userService().UserLogin(req, &userLogin)
} else {
2023-12-27 01:48:24 +08:00
g.Log().Cat("Struct").Cat("Auth").Error(ctx, errStruct.Error())
ResultUtil.Error(req, ErrorCode.RequestBodyMismatching, errStruct.Map())
}
} else {
ResultUtil.Error(req, ErrorCode.RequestBodyError, errStruct.Error())
2023-12-24 15:17:21 +08:00
}
2023-12-25 02:15:46 +08:00
return res, err
2023-12-24 15:17:21 +08:00
}
2023-12-26 16:04:24 +08:00
// AuthCheck
//
// 检查登录
func (*ControllerV1) AuthCheck(ctx context.Context, _ *request.CheckReq) (res *request.CheckRes, err error) {
req := ghttp.RequestFromCtx(ctx)
// 获取数据库中用户信息
2023-12-26 16:29:04 +08:00
userService().CheckLogin(req)
return res, err
}
// AuthLogout
//
// 用户登出
func (*ControllerV1) AuthLogout(ctx context.Context, _ *request.LogoutReq) (res *request.LogoutRes, err error) {
req := ghttp.RequestFromCtx(ctx)
// 获取数据库中用户信息
userService().UserLogout(req)
2023-12-26 16:04:24 +08:00
return res, err
}