You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

159 lines
5.5 KiB

  1. package v1
  2. import (
  3. "gin-vue-admin/global"
  4. "gin-vue-admin/model"
  5. "gin-vue-admin/model/request"
  6. "gin-vue-admin/model/response"
  7. "gin-vue-admin/service"
  8. "gin-vue-admin/utils"
  9. "github.com/gin-gonic/gin"
  10. "go.uber.org/zap"
  11. )
  12. // @Tags Authority
  13. // @Summary 创建角色
  14. // @Security ApiKeyAuth
  15. // @accept application/json
  16. // @Produce application/json
  17. // @Param data body model.SysAuthority true "权限id, 权限名, 父角色id"
  18. // @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
  19. // @Router /authority/createAuthority [post]
  20. func CreateAuthority(c *gin.Context) {
  21. var authority model.SysAuthority
  22. _ = c.ShouldBindJSON(&authority)
  23. if err := utils.Verify(authority, utils.AuthorityVerify); err != nil {
  24. response.FailWithMessage(err.Error(), c)
  25. return
  26. }
  27. if err, authBack := service.CreateAuthority(authority); err != nil {
  28. global.GVA_LOG.Error("创建失败!", zap.Any("err", err))
  29. response.FailWithMessage("创建失败"+err.Error(), c)
  30. } else {
  31. response.OkWithDetailed(response.SysAuthorityResponse{Authority: authBack}, "创建成功", c)
  32. }
  33. }
  34. // @Tags Authority
  35. // @Summary 拷贝角色
  36. // @Security ApiKeyAuth
  37. // @accept application/json
  38. // @Produce application/json
  39. // @Param data body response.SysAuthorityCopyResponse true "旧角色id, 新权限id, 新权限名, 新父角色id"
  40. // @Success 200 {string} string "{"success":true,"data":{},"msg":"拷贝成功"}"
  41. // @Router /authority/copyAuthority [post]
  42. func CopyAuthority(c *gin.Context) {
  43. var copyInfo response.SysAuthorityCopyResponse
  44. _ = c.ShouldBindJSON(&copyInfo)
  45. if err := utils.Verify(copyInfo, utils.OldAuthorityVerify); err != nil {
  46. response.FailWithMessage(err.Error(), c)
  47. return
  48. }
  49. if err := utils.Verify(copyInfo.Authority, utils.AuthorityVerify); err != nil {
  50. response.FailWithMessage(err.Error(), c)
  51. return
  52. }
  53. if err, authBack := service.CopyAuthority(copyInfo); err != nil {
  54. global.GVA_LOG.Error("拷贝失败!", zap.Any("err", err))
  55. response.FailWithMessage("拷贝失败"+err.Error(), c)
  56. } else {
  57. response.OkWithDetailed(response.SysAuthorityResponse{Authority: authBack}, "拷贝成功", c)
  58. }
  59. }
  60. // @Tags Authority
  61. // @Summary 删除角色
  62. // @Security ApiKeyAuth
  63. // @accept application/json
  64. // @Produce application/json
  65. // @Param data body model.SysAuthority true "删除角色"
  66. // @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
  67. // @Router /authority/deleteAuthority [post]
  68. func DeleteAuthority(c *gin.Context) {
  69. var authority model.SysAuthority
  70. _ = c.ShouldBindJSON(&authority)
  71. if err := utils.Verify(authority, utils.AuthorityIdVerify); err != nil {
  72. response.FailWithMessage(err.Error(), c)
  73. return
  74. }
  75. if err := service.DeleteAuthority(&authority); err != nil { // 删除角色之前需要判断是否有用户正在使用此角色
  76. global.GVA_LOG.Error("删除失败!", zap.Any("err", err))
  77. response.FailWithMessage("删除失败"+err.Error(), c)
  78. } else {
  79. response.OkWithMessage("删除成功", c)
  80. }
  81. }
  82. // @Tags Authority
  83. // @Summary 更新角色信息
  84. // @Security ApiKeyAuth
  85. // @accept application/json
  86. // @Produce application/json
  87. // @Param data body model.SysAuthority true "权限id, 权限名, 父角色id"
  88. // @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
  89. // @Router /authority/updateAuthority [post]
  90. func UpdateAuthority(c *gin.Context) {
  91. var auth model.SysAuthority
  92. _ = c.ShouldBindJSON(&auth)
  93. if err := utils.Verify(auth, utils.AuthorityVerify); err != nil {
  94. response.FailWithMessage(err.Error(), c)
  95. return
  96. }
  97. if err, authority := service.UpdateAuthority(auth); err != nil {
  98. global.GVA_LOG.Error("更新失败!", zap.Any("err", err))
  99. response.FailWithMessage("更新失败"+err.Error(), c)
  100. } else {
  101. response.OkWithDetailed(response.SysAuthorityResponse{Authority: authority}, "更新成功", c)
  102. }
  103. }
  104. // @Tags Authority
  105. // @Summary 分页获取角色列表
  106. // @Security ApiKeyAuth
  107. // @accept application/json
  108. // @Produce application/json
  109. // @Param data body request.PageInfo true "页码, 每页大小"
  110. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  111. // @Router /authority/getAuthorityList [post]
  112. func GetAuthorityList(c *gin.Context) {
  113. var pageInfo request.PageInfo
  114. _ = c.ShouldBindJSON(&pageInfo)
  115. if err := utils.Verify(pageInfo, utils.PageInfoVerify); err != nil {
  116. response.FailWithMessage(err.Error(), c)
  117. return
  118. }
  119. if err, list, total := service.GetAuthorityInfoList(pageInfo); err != nil {
  120. global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
  121. response.FailWithMessage("获取失败"+err.Error(), c)
  122. } else {
  123. response.OkWithDetailed(response.PageResult{
  124. List: list,
  125. Total: total,
  126. Page: pageInfo.Page,
  127. PageSize: pageInfo.PageSize,
  128. }, "获取成功", c)
  129. }
  130. }
  131. // @Tags Authority
  132. // @Summary 设置角色资源权限
  133. // @Security ApiKeyAuth
  134. // @accept application/json
  135. // @Produce application/json
  136. // @Param data body model.SysAuthority true "设置角色资源权限"
  137. // @Success 200 {string} string "{"success":true,"data":{},"msg":"设置成功"}"
  138. // @Router /authority/setDataAuthority [post]
  139. func SetDataAuthority(c *gin.Context) {
  140. var auth model.SysAuthority
  141. _ = c.ShouldBindJSON(&auth)
  142. if err := utils.Verify(auth, utils.AuthorityIdVerify); err != nil {
  143. response.FailWithMessage(err.Error(), c)
  144. return
  145. }
  146. if err := service.SetDataAuthority(auth); err != nil {
  147. global.GVA_LOG.Error("设置失败!", zap.Any("err", err))
  148. response.FailWithMessage("设置失败"+err.Error(), c)
  149. } else {
  150. response.OkWithMessage("设置成功", c)
  151. }
  152. }