优化
This commit is contained in:
@@ -23,8 +23,16 @@ func NewUser(user *service.UserService, auth *service.AuthService) *User {
|
||||
|
||||
func (h *User) List(c *gin.Context) {
|
||||
var req dto.UserListRequest
|
||||
if c.ShouldBindJSON(&req) != nil {
|
||||
httpx.Fail(c, "参数错误")
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
httpx.Fail(c, err.Error())
|
||||
return
|
||||
}
|
||||
if req.Page == 0 {
|
||||
httpx.Fail(c, "Page值不能为空")
|
||||
return
|
||||
}
|
||||
if req.PageSize == 0 {
|
||||
httpx.Fail(c, "PageSize值不能为空")
|
||||
return
|
||||
}
|
||||
items, total, err := h.service.ListUsersRequest(c.Request.Context(), &req)
|
||||
@@ -37,8 +45,24 @@ func (h *User) List(c *gin.Context) {
|
||||
|
||||
func (h *User) Create(c *gin.Context) {
|
||||
var req dto.UserRequest
|
||||
if c.ShouldBindJSON(&req) != nil || req.Username == "" || req.Password == "" {
|
||||
httpx.Fail(c, "参数错误")
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
httpx.Fail(c, err.Error())
|
||||
return
|
||||
}
|
||||
if req.Username == "" {
|
||||
httpx.Fail(c, "Username值不能为空")
|
||||
return
|
||||
}
|
||||
if req.Password == "" {
|
||||
httpx.Fail(c, "Password值不能为空")
|
||||
return
|
||||
}
|
||||
if req.NickName == "" {
|
||||
httpx.Fail(c, "NickName值不能为空")
|
||||
return
|
||||
}
|
||||
if req.AuthorityID == 0 {
|
||||
httpx.Fail(c, "AuthorityId值不能为空")
|
||||
return
|
||||
}
|
||||
result, err := h.service.CreateUserRequest(c.Request.Context(), &req)
|
||||
@@ -51,8 +75,12 @@ func (h *User) Create(c *gin.Context) {
|
||||
|
||||
func (h *User) Update(c *gin.Context) {
|
||||
var req dto.UserRequest
|
||||
if c.ShouldBindJSON(&req) != nil {
|
||||
httpx.Fail(c, "参数错误")
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
httpx.Fail(c, err.Error())
|
||||
return
|
||||
}
|
||||
if req.ID == 0 {
|
||||
httpx.Fail(c, "ID值不能为空")
|
||||
return
|
||||
}
|
||||
if err := h.service.UpdateUserRequest(c.Request.Context(), &req); err != nil {
|
||||
@@ -69,8 +97,8 @@ func (h *User) UpdateSelf(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
var req dto.SelfUserRequest
|
||||
if c.ShouldBindJSON(&req) != nil {
|
||||
httpx.Fail(c, "参数错误")
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
httpx.Fail(c, err.Error())
|
||||
return
|
||||
}
|
||||
value := service.UserInput{ID: claims.ID, NickName: req.NickName, HeaderImg: req.HeaderImg, Phone: req.Phone, Email: req.Email, Enable: req.Enable}
|
||||
@@ -83,8 +111,12 @@ func (h *User) UpdateSelf(c *gin.Context) {
|
||||
|
||||
func (h *User) Delete(c *gin.Context) {
|
||||
var req dto.IDRequest
|
||||
if c.ShouldBind(&req) != nil || req.ID == 0 {
|
||||
httpx.Fail(c, "参数错误")
|
||||
if err := c.ShouldBind(&req); err != nil {
|
||||
httpx.Fail(c, err.Error())
|
||||
return
|
||||
}
|
||||
if req.ID == 0 {
|
||||
httpx.Fail(c, "ID值不能为空")
|
||||
return
|
||||
}
|
||||
claims := middleware.Claims(c)
|
||||
@@ -101,8 +133,8 @@ func (h *User) Delete(c *gin.Context) {
|
||||
|
||||
func (h *User) ResetPassword(c *gin.Context) {
|
||||
var req dto.ResetPasswordRequest
|
||||
if c.ShouldBindJSON(&req) != nil {
|
||||
httpx.Fail(c, "参数错误")
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
httpx.Fail(c, err.Error())
|
||||
return
|
||||
}
|
||||
if err := h.service.ResetPassword(c.Request.Context(), req.ID, req.Password); err != nil {
|
||||
@@ -115,8 +147,20 @@ func (h *User) ResetPassword(c *gin.Context) {
|
||||
func (h *User) ChangePassword(c *gin.Context) {
|
||||
claims := middleware.Claims(c)
|
||||
var req dto.ChangePasswordRequest
|
||||
if claims == nil || c.ShouldBindJSON(&req) != nil {
|
||||
httpx.Fail(c, "参数错误")
|
||||
if claims == nil {
|
||||
httpx.NoAuth(c, "未登录或非法访问")
|
||||
return
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
httpx.Fail(c, err.Error())
|
||||
return
|
||||
}
|
||||
if req.Password == "" {
|
||||
httpx.Fail(c, "Password值不能为空")
|
||||
return
|
||||
}
|
||||
if req.NewPassword == "" {
|
||||
httpx.Fail(c, "NewPassword值不能为空")
|
||||
return
|
||||
}
|
||||
if err := h.service.ChangePassword(c.Request.Context(), claims.ID, req.Password, req.NewPassword); err != nil {
|
||||
@@ -129,8 +173,12 @@ func (h *User) ChangePassword(c *gin.Context) {
|
||||
func (h *User) SetSelfSetting(c *gin.Context) {
|
||||
claims := middleware.Claims(c)
|
||||
var setting map[string]any
|
||||
if claims == nil || c.ShouldBindJSON(&setting) != nil {
|
||||
httpx.Fail(c, "参数错误")
|
||||
if claims == nil {
|
||||
httpx.NoAuth(c, "未登录或非法访问")
|
||||
return
|
||||
}
|
||||
if err := c.ShouldBindJSON(&setting); err != nil {
|
||||
httpx.Fail(c, err.Error())
|
||||
return
|
||||
}
|
||||
if err := h.service.SetUserSetting(c.Request.Context(), claims.ID, setting); err != nil {
|
||||
@@ -142,8 +190,8 @@ func (h *User) SetSelfSetting(c *gin.Context) {
|
||||
|
||||
func (h *User) SetAuthorities(c *gin.Context) {
|
||||
var req dto.UserAuthoritiesRequest
|
||||
if c.ShouldBindJSON(&req) != nil || req.ID == 0 || len(req.AuthorityIDs) == 0 {
|
||||
httpx.Fail(c, "参数错误")
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
httpx.Fail(c, err.Error())
|
||||
return
|
||||
}
|
||||
if err := h.service.SetUserAuthorities(c.Request.Context(), req.ID, req.AuthorityIDs); err != nil {
|
||||
@@ -156,11 +204,19 @@ func (h *User) SetAuthorities(c *gin.Context) {
|
||||
func (h *User) SwitchAuthority(c *gin.Context) {
|
||||
claims := middleware.Claims(c)
|
||||
var req dto.SwitchAuthorityRequest
|
||||
if claims == nil || c.ShouldBindJSON(&req) != nil || req.AuthorityID == 0 {
|
||||
httpx.Fail(c, "参数错误")
|
||||
if claims == nil {
|
||||
httpx.NoAuth(c, "未登录或非法访问")
|
||||
return
|
||||
}
|
||||
login, err := h.auth.SwitchAuthority(c.Request.Context(), claims.ID, req.AuthorityID)
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
httpx.Fail(c, err.Error())
|
||||
return
|
||||
}
|
||||
if req.AuthorityID == 0 {
|
||||
httpx.Fail(c, "AuthorityId值不能为空")
|
||||
return
|
||||
}
|
||||
login, err := h.auth.SwitchAuthority(c.Request.Context(), claims, req.AuthorityID)
|
||||
if err != nil {
|
||||
httpx.Fail(c, err.Error())
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user