role.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package controller
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "iot_manager_service/app/system/dao"
  5. "iot_manager_service/app/system/model"
  6. "iot_manager_service/app/system/service"
  7. "iot_manager_service/util"
  8. "math"
  9. "net/http"
  10. "strconv"
  11. )
  12. var Role = new(role)
  13. type role struct{}
  14. func (c *role) GetDetail(ctx *gin.Context) {
  15. id := ctx.Query("id")
  16. iId, err := strconv.ParseInt(id, 10, 64)
  17. if err != nil {
  18. ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(err.Error(), nil))
  19. return
  20. }
  21. role, err := service.RoleService.Get(iId)
  22. if err != nil {
  23. ctx.JSON(http.StatusOK, err)
  24. return
  25. }
  26. ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, role))
  27. }
  28. func (c *role) List(ctx *gin.Context) {
  29. current, _ := strconv.Atoi(ctx.Query("current"))
  30. size, _ := strconv.Atoi(ctx.Query("size"))
  31. roleName := ctx.Query("roleName")
  32. tenantId := ctx.Query("tenantId")
  33. roleAlias := ctx.Query("roleAlias")
  34. if current == 0 {
  35. current = 1
  36. }
  37. if size <= 0 || size > 100 {
  38. size = 10
  39. }
  40. roles, err := service.RoleService.List(roleName, tenantId, roleAlias, current, size)
  41. if err != nil {
  42. ctx.JSON(http.StatusOK, err)
  43. return
  44. }
  45. pages := math.Ceil(float64(len(roles)) / float64(size))
  46. rsp := model.RsqRoleList{
  47. Current: current,
  48. Size: size,
  49. Total: len(roles),
  50. Pages: int(pages),
  51. Records: roles,
  52. }
  53. ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, rsp))
  54. }
  55. func (c *role) Tree(ctx *gin.Context) {
  56. role, err := service.RoleService.Tree()
  57. if err != nil {
  58. ctx.JSON(http.StatusOK, err)
  59. return
  60. }
  61. ctx.JSON(http.StatusOK, role)
  62. }
  63. func (c *role) Submit(ctx *gin.Context) {
  64. var req dao.Role
  65. if err := ctx.ShouldBindJSON(&req); err != nil {
  66. ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(err.Error(), nil))
  67. return
  68. }
  69. err := service.RoleService.Submit(req)
  70. ctx.JSON(http.StatusOK, err)
  71. }
  72. func (c *role) Remove(ctx *gin.Context) {
  73. var req *model.ReqRoleRemove
  74. if err := ctx.ShouldBindJSON(&req); err != nil {
  75. ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(err.Error(), nil))
  76. return
  77. }
  78. err := service.RoleService.Remove(req.IDs)
  79. ctx.JSON(http.StatusOK, err)
  80. }
  81. func (c *role) GetList(ctx *gin.Context) {
  82. roles, err := service.RoleService.GetList()
  83. if err != nil {
  84. ctx.JSON(http.StatusOK, util.FailResponse(err.Error(), nil))
  85. }
  86. ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, roles))
  87. }
  88. func (c *role) Grant(ctx *gin.Context) {
  89. var req *model.ReqRoleGrant
  90. if err := ctx.ShouldBindJSON(&req); err != nil {
  91. ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(err.Error(), nil))
  92. return
  93. }
  94. err := service.RoleService.Grant(req)
  95. if err != nil {
  96. ctx.JSON(http.StatusOK, util.FailResponse(err.Error(), nil))
  97. }
  98. ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, nil))
  99. }