package system import ( "github.com/gin-gonic/gin" "github.com/go-playground/validator/v10" "hongze/fms_api/controller/resp" "hongze/fms_api/global" "hongze/fms_api/models/base" "hongze/fms_api/models/system" systemService "hongze/fms_api/services/system" "hongze/fms_api/utils" ) type SysRoleController struct { } // Add 添加角色 func (s *SysRoleController) Add(c *gin.Context) { req := new(system.SysRoleAddReq) err := c.ShouldBind(&req) if err != nil { errs, ok := err.(validator.ValidationErrors) if !ok { resp.FailData("参数解析失败", "Err:"+err.Error(), c) return } resp.FailData("参数解析失败", errs.Translate(global.Trans), c) return } err, errMsg := systemService.AddRole(req) if err != nil { resp.FailMsg(errMsg, err.Error(), c) return } resp.Ok("保存成功", c) return } // Edit 编辑角色 func (s *SysRoleController) Edit(c *gin.Context) { req := new(system.SysRoleEditReq) err := c.ShouldBind(&req) if err != nil { errs, ok := err.(validator.ValidationErrors) if !ok { resp.FailData("参数解析失败", "Err:"+err.Error(), c) return } resp.FailData("参数解析失败", errs.Translate(global.Trans), c) return } err, errMsg := systemService.EditRole(req) if err != nil { resp.FailMsg(errMsg, err.Error(), c) return } resp.Ok("保存成功", c) return } // 编辑角色菜单权限 func (s *SysRoleController) EditMenu(c *gin.Context) { req := new(system.SysRoleMenuEditReq) err := c.ShouldBind(&req) if err != nil { errs, ok := err.(validator.ValidationErrors) if !ok { resp.FailData("参数解析失败", "Err:"+err.Error(), c) return } resp.FailData("参数解析失败", errs.Translate(global.Trans), c) return } err, errMsg := systemService.EditRoleMenu(req) if err != nil { resp.FailMsg(errMsg, err.Error(), c) return } resp.Ok("保存成功", c) return } func (s *SysRoleController) List(c *gin.Context) { req := new(system.SysRoleListReq) err := c.BindQuery(&req) if err != nil { errs, ok := err.(validator.ValidationErrors) if !ok { resp.FailData("参数解析失败", "Err:"+err.Error(), c) return } resp.FailData("参数解析失败", errs.Translate(global.Trans), c) return } page := new(base.Page) page.SetPageSize(req.PageSize) page.SetCurrent(req.Current) item := new(system.SysRole) var condition string var pars []interface{} if req.RoleName != "" { roleName := "%" + req.RoleName + "%" condition += " role_name like ?" pars = append(pars, roleName) } page.AddOrderItem(base.OrderItem{Column: "create_time", Asc: false}) total, roles, err := item.SelectPage(page, condition, pars) if err != nil { resp.FailMsg("获取失败", "获取失败,Err:"+err.Error(), c) return } list := make([]*system.SysRoleListItem, 0) for _, v := range roles { tmp := new(system.SysRoleListItem) tmp.RoleId = v.RoleId tmp.RoleName = v.RoleName tmp.CreateTime = utils.TimeTransferString(utils.FormatDateTime, v.CreateTime) tmp.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, v.ModifyTime) list = append(list, tmp) } page.SetTotal(total) baseData := new(base.BaseData) baseData.SetPage(page) baseData.SetList(list) resp.OkData("获取成功", baseData, c) } func (s *SysRoleController) MenuList(c *gin.Context) { req := new(system.SysRoleMenuListReq) err := c.BindQuery(&req) if err != nil { errs, ok := err.(validator.ValidationErrors) if !ok { resp.FailData("参数解析失败", "Err:"+err.Error(), c) return } resp.FailData("参数解析失败", errs.Translate(global.Trans), c) return } list, err, errMsg := systemService.RoleMenuList(req) if err != nil { resp.FailMsg(errMsg, err.Error(), c) return } resp.OkData("获取成功", list, c) } // Delete 删除角色 func (s *SysRoleController) Delete(c *gin.Context) { req := new(system.SysRoleDelReq) err := c.ShouldBind(&req) if err != nil { errs, ok := err.(validator.ValidationErrors) if !ok { resp.FailData("参数解析失败", "Err:"+err.Error(), c) return } resp.FailData("参数解析失败", errs.Translate(global.Trans), c) return } err, errMsg := systemService.DeleteRole(req) if err != nil { resp.FailMsg(errMsg, errMsg, c) return } resp.Ok("删除成功", c) return }