package system import ( "sundynix-go/global" "sundynix-go/model/commom/request" "sundynix-go/model/commom/response" "sundynix-go/model/system" systemReq "sundynix-go/model/system/request" "github.com/gin-gonic/gin" "go.uber.org/zap" ) type UserApi struct { } func (u *UserApi) SaveUser(c *gin.Context) { var user system.User err := c.ShouldBindJSON(&user) if err != nil { response.FailWithMsg("参数错误:"+err.Error(), c) return } if err = userService.SaveUser(user); err != nil { global.Logger.Error("保存用户失败!", zap.Error(err)) response.FailWithMsg(err.Error(), c) } else { response.OkWithMsg("保存用户成功!", c) } } func (u *UserApi) UpdateUser(c *gin.Context) { var user system.User err := c.ShouldBindJSON(&user) if err != nil { response.FailWithMsg("参数错误:"+err.Error(), c) return } if err = userService.UpdateUser(&user); err != nil { global.Logger.Error("更新用户失败!", zap.Error(err)) response.FailWithMsg(err.Error(), c) } else { response.OkWithMsg("更新用户成功!", c) } } func (u *UserApi) GetUserList(c *gin.Context) { var pageInfo systemReq.GetUserList err := c.ShouldBindJSON(&pageInfo) if err != nil { response.FailWithMsg(err.Error(), c) return } list, total, err := userService.GetUserList(pageInfo) if err != nil { global.Logger.Error("获取用户列表失败!", zap.Error(err)) response.FailWithMsg(err.Error(), c) return } response.OkWithData(response.PageResult{ List: list, Total: total, Page: pageInfo.Current, PageSize: pageInfo.PageSize, }, c) } func (u *UserApi) Delete(c *gin.Context) { var ids request.IdsReq err := c.ShouldBindJSON(&ids) if err != nil { response.FailWithMsg(err.Error(), c) return } err = userService.DeleteUserByIds(ids) if err != nil { global.Logger.Error("删除用户失败!", zap.Error(err)) response.FailWithMsg(err.Error(), c) return } response.OkWithMsg("删除用户成功!", c) } func (u *UserApi) Detail(c *gin.Context) { id := c.Query("id") user, err := userService.GetUserById(id) if err != nil { global.Logger.Error("获取用户详情失败!", zap.Error(err)) response.FailWithMsg(err.Error(), c) return } response.OkWithData(user, c) } func (u *UserApi) ChangePassword(c *gin.Context) { var changePwd systemReq.ChangePwd err := c.ShouldBindJSON(&changePwd) if err != nil { response.FailWithMsg(err.Error(), c) return } err = userService.ChangePassword(changePwd.Id, changePwd.NewPwd) if err != nil { global.Logger.Error("修改密码失败!", zap.Error(err)) response.FailWithMsg(err.Error(), c) return } response.OkWithMsg("修改密码成功", c) } func (u *UserApi) GrantRole(c *gin.Context) { var grantRole systemReq.GrantRole err := c.ShouldBindJSON(&grantRole) if err != nil { response.FailWithMsg(err.Error(), c) return } err = roleService.GrantRole(grantRole.UserId, grantRole.RoleIds) if err != nil { global.Logger.Error("授权角色失败!", zap.Error(err)) response.FailWithMsg(err.Error(), c) return } response.OkWithMsg("授权角色成功!", c) }