2024-01-09 21:06:03 +08:00
|
|
|
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"
|
2023-12-26 15:34:38 +08:00
|
|
|
"PersonalMain/internal/model/entity"
|
|
|
|
"PersonalMain/internal/service/UserService"
|
|
|
|
"PersonalMain/utility/ErrorCode"
|
|
|
|
"PersonalMain/utility/ResultUtil"
|
2023-12-24 15:17:21 +08:00
|
|
|
"context"
|
2023-12-26 15:34:38 +08:00
|
|
|
"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{}
|
|
|
|
}
|
|
|
|
|
2023-12-26 15:34:38 +08:00
|
|
|
// AuthRegister
|
|
|
|
//
|
|
|
|
// 用户注册
|
2023-12-26 16:04:24 +08:00
|
|
|
func (*ControllerV1) AuthRegister(ctx context.Context, _ *request.RegisterReq) (res *request.RegisterRes, err error) {
|
2023-12-26 15:34:38 +08:00
|
|
|
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)
|
2023-12-26 15:34:38 +08:00
|
|
|
} else {
|
2023-12-27 01:48:24 +08:00
|
|
|
g.Log().Cat("Struct").Cat("Auth").Error(ctx, errStruct.Error())
|
2023-12-26 15:34:38 +08:00
|
|
|
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())
|
2023-12-26 15:34:38 +08:00
|
|
|
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) {
|
2023-12-26 15:34:38 +08:00
|
|
|
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)
|
2023-12-26 15:34:38 +08:00
|
|
|
} else {
|
2023-12-27 01:48:24 +08:00
|
|
|
g.Log().Cat("Struct").Cat("Auth").Error(ctx, errStruct.Error())
|
2023-12-26 15:34:38 +08:00
|
|
|
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
|
|
|
|
}
|