sys_role.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package system
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "github.com/go-playground/validator/v10"
  5. "hongze/fms_api/controller/resp"
  6. "hongze/fms_api/global"
  7. "hongze/fms_api/models/base"
  8. "hongze/fms_api/models/system"
  9. systemService "hongze/fms_api/services/system"
  10. "hongze/fms_api/utils"
  11. )
  12. type SysRoleController struct {
  13. }
  14. // Add 添加角色
  15. func (s *SysRoleController) Add(c *gin.Context) {
  16. req := new(system.SysRoleAddReq)
  17. err := c.ShouldBind(&req)
  18. if err != nil {
  19. errs, ok := err.(validator.ValidationErrors)
  20. if !ok {
  21. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  22. return
  23. }
  24. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  25. return
  26. }
  27. err, errMsg := systemService.AddRole(req)
  28. if err != nil {
  29. resp.FailMsg(errMsg, err.Error(), c)
  30. return
  31. }
  32. resp.Ok("保存成功", c)
  33. return
  34. }
  35. // Edit 编辑角色
  36. func (s *SysRoleController) Edit(c *gin.Context) {
  37. req := new(system.SysRoleEditReq)
  38. err := c.ShouldBind(&req)
  39. if err != nil {
  40. errs, ok := err.(validator.ValidationErrors)
  41. if !ok {
  42. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  43. return
  44. }
  45. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  46. return
  47. }
  48. err, errMsg := systemService.EditRole(req)
  49. if err != nil {
  50. resp.FailMsg(errMsg, err.Error(), c)
  51. return
  52. }
  53. resp.Ok("保存成功", c)
  54. return
  55. }
  56. // 编辑角色菜单权限
  57. func (s *SysRoleController) EditMenu(c *gin.Context) {
  58. req := new(system.SysRoleMenuEditReq)
  59. err := c.ShouldBind(&req)
  60. if err != nil {
  61. errs, ok := err.(validator.ValidationErrors)
  62. if !ok {
  63. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  64. return
  65. }
  66. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  67. return
  68. }
  69. err, errMsg := systemService.EditRoleMenu(req)
  70. if err != nil {
  71. resp.FailMsg(errMsg, err.Error(), c)
  72. return
  73. }
  74. resp.Ok("保存成功", c)
  75. return
  76. }
  77. func (s *SysRoleController) List(c *gin.Context) {
  78. req := new(system.SysRoleListReq)
  79. err := c.BindQuery(&req)
  80. if err != nil {
  81. errs, ok := err.(validator.ValidationErrors)
  82. if !ok {
  83. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  84. return
  85. }
  86. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  87. return
  88. }
  89. page := new(base.Page)
  90. page.SetPageSize(req.PageSize)
  91. page.SetCurrent(req.Current)
  92. item := new(system.SysRole)
  93. var condition string
  94. var pars []interface{}
  95. if req.RoleName != "" {
  96. roleName := "%" + req.RoleName + "%"
  97. condition += " role_name like ?"
  98. pars = append(pars, roleName)
  99. }
  100. page.AddOrderItem(base.OrderItem{Column: "create_time", Asc: false})
  101. total, roles, err := item.SelectPage(page, condition, pars)
  102. if err != nil {
  103. resp.FailMsg("获取失败", "获取失败,Err:"+err.Error(), c)
  104. return
  105. }
  106. list := make([]*system.SysRoleListItem, 0)
  107. for _, v := range roles {
  108. tmp := new(system.SysRoleListItem)
  109. tmp.RoleId = v.RoleId
  110. tmp.RoleName = v.RoleName
  111. tmp.CreateTime = utils.TimeTransferString(utils.FormatDateTime, v.CreateTime)
  112. tmp.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, v.ModifyTime)
  113. list = append(list, tmp)
  114. }
  115. page.SetTotal(total)
  116. baseData := new(base.BaseData)
  117. baseData.SetPage(page)
  118. baseData.SetList(list)
  119. resp.OkData("获取成功", baseData, c)
  120. }
  121. func (s *SysRoleController) MenuList(c *gin.Context) {
  122. req := new(system.SysRoleMenuListReq)
  123. err := c.BindQuery(&req)
  124. if err != nil {
  125. errs, ok := err.(validator.ValidationErrors)
  126. if !ok {
  127. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  128. return
  129. }
  130. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  131. return
  132. }
  133. list, err, errMsg := systemService.RoleMenuList(req)
  134. if err != nil {
  135. resp.FailMsg(errMsg, err.Error(), c)
  136. return
  137. }
  138. resp.OkData("获取成功", list, c)
  139. }
  140. // Delete 删除角色
  141. func (s *SysRoleController) Delete(c *gin.Context) {
  142. req := new(system.SysRoleDelReq)
  143. err := c.ShouldBind(&req)
  144. if err != nil {
  145. errs, ok := err.(validator.ValidationErrors)
  146. if !ok {
  147. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  148. return
  149. }
  150. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  151. return
  152. }
  153. err, errMsg := systemService.DeleteRole(req)
  154. if err != nil {
  155. resp.FailMsg(errMsg, errMsg, c)
  156. return
  157. }
  158. resp.Ok("删除成功", c)
  159. return
  160. }