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" "sundynix-go/utils/auth" "github.com/gin-gonic/gin" "go.uber.org/zap" ) type UserApi struct { } // CurrentUser // @tags 用户管理 // @Summary 当前登录用户 // @Security BasicAuth // @Produce json // @Success 200 {object} response.Response "{"code": 200, "data": {}, "msg": "添加成功"}" // @Router /user/info [get] func (u *UserApi) CurrentUser(c *gin.Context) { userId := auth.GetUserId(c) user, err := userService.GetUserById(userId) if err != nil { global.Logger.Error("获取用户信息失败!", zap.Error(err)) response.FailWithMsg(err.Error(), c) return } response.OkWithData(user, c) } // SaveUser // @tags 用户管理 // @Summary 新增用户 // @Security BasicAuth // @accept json // @Produce json // @Param data body system.User true "用户信息" // @Success 200 {object} response.Response "{"code": 200, "data": {}, "msg": "添加成功"}" // @Router /user/save [post] 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) } } // UpdateUser // @tags 用户管理 // @Summary 更新用户 // @Security BasicAuth // @accept application/json // @Produce application/json // @Param data body system.User true "用户ID,用户信息" // @Success 200 {object} response.Response "{"code": 200, "data": [...]}" // @Router /user/update [post] 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) } } // GetUserList // @tags 用户管理 // @Summary 获取用户列表 // @Security BasicAuth // @accept application/json // @Produce application/json // @Param data body systemReq.GetUserList true "页码, 每页大小, 搜索条件" // @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取用户列表,返回包括列表,总数,页码,每页大小" // @Router /user/getUserList [post] 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) } // Delete // @Tags 用户管理 // @Summary 删除用户 // @Security BasicAuth // @accept application/json // @Produce application/json // @Param data body request.IdsReq true "批量删除用户" // @Success 200 {object} response.Response{msg=string} "删除用户" // @Router /user/delete [post] 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) } // Detail // @Tags 用户管理 // @Summary 获取用户详情 // @Security BasicAuth // @Produce application/json // @Param id query string true "id" // @Success 200 {object} response.Response{data=system.User} "获取用户详情成功" // @Router /user/detail [get] 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) } // ChangePassword // @Tags 用户管理 // @Summary 修改密码 // @Security BasicAuth // @Description 修改密码 // @accept json // @Produce application/json // @Param data body request.ChangePwd true "用户id" // @Success 200 {object} response.Response{data=system.User} "修改密码成功" // @Router /user/changePassword [post] 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) } // GrantRole // @Tags 用户管理 // @Summary 给用户分配角色 // @Security BasicAuth // @accept application/json // @Produce application/json // @Param data body systemReq.GrantRole true "用户ID, 角色ID" // @Success 200 {object} response.Response "{"code": 200, "data": [...]}" // @Router /user/grantRole [post] 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) }