sys_authority.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package v1
  2. import (
  3. "fmt"
  4. "gin-vue-admin/global/response"
  5. "gin-vue-admin/model"
  6. "gin-vue-admin/model/request"
  7. resp "gin-vue-admin/model/response"
  8. "gin-vue-admin/service"
  9. "gin-vue-admin/utils"
  10. "github.com/gin-gonic/gin"
  11. )
  12. // @Tags authority
  13. // @Summary 创建角色
  14. // @Security ApiKeyAuth
  15. // @accept application/json
  16. // @Produce application/json
  17. // @Param data body model.SysAuthority true "创建角色"
  18. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  19. // @Router /authority/createAuthority [post]
  20. func CreateAuthority(c *gin.Context) {
  21. var auth model.SysAuthority
  22. _ = c.ShouldBindJSON(&auth)
  23. AuthorityVerify := utils.Rules{
  24. "AuthorityId": {utils.NotEmpty()},
  25. "AuthorityName": {utils.NotEmpty()},
  26. "ParentId": {utils.NotEmpty()},
  27. }
  28. AuthorityVerifyErr := utils.Verify(auth, AuthorityVerify)
  29. if AuthorityVerifyErr!=nil {
  30. response.FailWithMessage(AuthorityVerifyErr.Error(), c)
  31. return
  32. }
  33. err, authBack := service.CreateAuthority(auth)
  34. if err != nil {
  35. response.FailWithMessage(fmt.Sprintf("创建失败,%v", err), c)
  36. } else {
  37. response.OkWithData(resp.SysAuthorityResponse{Authority: authBack}, c)
  38. }
  39. }
  40. // @Tags authority
  41. // @Summary 拷贝角色
  42. // @Security ApiKeyAuth
  43. // @accept application/json
  44. // @Produce application/json
  45. // @Param data body response.SysAuthorityCopyResponse true "拷贝角色"
  46. // @Success 200 {string} string "{"success":true,"data":{},"msg":"拷贝成功"}"
  47. // @Router /authority/copyAuthority [post]
  48. func CopyAuthority(c *gin.Context) {
  49. var copyInfo resp.SysAuthorityCopyResponse
  50. _ = c.ShouldBindJSON(&copyInfo)
  51. OldAuthorityVerify := utils.Rules{
  52. "OldAuthorityId": {utils.NotEmpty()},
  53. }
  54. OldAuthorityVerifyErr := utils.Verify(copyInfo, OldAuthorityVerify)
  55. if OldAuthorityVerifyErr!=nil {
  56. response.FailWithMessage(OldAuthorityVerifyErr.Error(), c)
  57. return
  58. }
  59. AuthorityVerify := utils.Rules{
  60. "AuthorityId": {utils.NotEmpty()},
  61. "AuthorityName": {utils.NotEmpty()},
  62. "ParentId": {utils.NotEmpty()},
  63. }
  64. AuthorityVerifyErr := utils.Verify(copyInfo.Authority, AuthorityVerify)
  65. if AuthorityVerifyErr!=nil {
  66. response.FailWithMessage(AuthorityVerifyErr.Error(), c)
  67. return
  68. }
  69. err, authBack := service.CopyAuthority(copyInfo)
  70. if err != nil {
  71. response.FailWithMessage(fmt.Sprintf("拷贝失败,%v", err), c)
  72. } else {
  73. response.OkWithData(resp.SysAuthorityResponse{Authority: authBack}, c)
  74. }
  75. }
  76. // @Tags authority
  77. // @Summary 删除角色
  78. // @Security ApiKeyAuth
  79. // @accept application/json
  80. // @Produce application/json
  81. // @Param data body model.SysAuthority true "删除角色"
  82. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  83. // @Router /authority/deleteAuthority [post]
  84. func DeleteAuthority(c *gin.Context) {
  85. var a model.SysAuthority
  86. _ = c.ShouldBindJSON(&a)
  87. AuthorityVerify := utils.Rules{
  88. "AuthorityId": {utils.NotEmpty()},
  89. }
  90. AuthorityVerifyErr := utils.Verify(a, AuthorityVerify)
  91. if AuthorityVerifyErr!=nil {
  92. response.FailWithMessage(AuthorityVerifyErr.Error(), c)
  93. return
  94. }
  95. //删除角色之前需要判断是否有用户正在使用此角色
  96. err := service.DeleteAuthority(&a)
  97. if err != nil {
  98. response.FailWithMessage(fmt.Sprintf("删除失败,%v", err), c)
  99. } else {
  100. response.OkWithMessage("删除成功", c)
  101. }
  102. }
  103. // @Tags authority
  104. // @Summary 设置角色资源权限
  105. // @Security ApiKeyAuth
  106. // @accept application/json
  107. // @Produce application/json
  108. // @Param data body model.SysAuthority true "设置角色资源权限"
  109. // @Success 200 {string} string "{"success":true,"data":{},"msg":"设置成功"}"
  110. // @Router /authority/updateAuthority [post]
  111. func UpdateAuthority(c *gin.Context) {
  112. var auth model.SysAuthority
  113. _ = c.ShouldBindJSON(&auth)
  114. AuthorityVerify := utils.Rules{
  115. "AuthorityId": {utils.NotEmpty()},
  116. "AuthorityName": {utils.NotEmpty()},
  117. "ParentId": {utils.NotEmpty()},
  118. }
  119. AuthorityVerifyErr := utils.Verify(auth, AuthorityVerify)
  120. if AuthorityVerifyErr!=nil {
  121. response.FailWithMessage(AuthorityVerifyErr.Error(), c)
  122. return
  123. }
  124. err, authority := service.UpdateAuthority(auth)
  125. if err != nil {
  126. response.FailWithMessage(fmt.Sprintf("更新失败,%v", err), c)
  127. } else {
  128. response.OkWithData(resp.SysAuthorityResponse{authority}, c)
  129. }
  130. }
  131. // @Tags authority
  132. // @Summary 分页获取角色列表
  133. // @Security ApiKeyAuth
  134. // @accept application/json
  135. // @Produce application/json
  136. // @Param data body request.PageInfo true "分页获取用户列表"
  137. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  138. // @Router /authority/getAuthorityList [post]
  139. func GetAuthorityList(c *gin.Context) {
  140. var pageInfo request.PageInfo
  141. _ = c.ShouldBindJSON(&pageInfo)
  142. AuthorityVerify := utils.Rules{
  143. "Page": {utils.NotEmpty()},
  144. "PageSize": {utils.NotEmpty()},
  145. }
  146. AuthorityVerifyErr := utils.Verify(pageInfo, AuthorityVerify)
  147. if AuthorityVerifyErr!=nil {
  148. response.FailWithMessage(AuthorityVerifyErr.Error(), c)
  149. return
  150. }
  151. err, list, total := service.GetAuthorityInfoList(pageInfo)
  152. if err != nil {
  153. response.FailWithMessage(fmt.Sprintf("获取数据失败,%v", err), c)
  154. } else {
  155. response.OkWithData(resp.PageResult{
  156. List: list,
  157. Total: total,
  158. Page: pageInfo.Page,
  159. PageSize: pageInfo.PageSize,
  160. }, c)
  161. }
  162. }
  163. // @Tags authority
  164. // @Summary 设置角色资源权限
  165. // @Security ApiKeyAuth
  166. // @accept application/json
  167. // @Produce application/json
  168. // @Param data body model.SysAuthority true "设置角色资源权限"
  169. // @Success 200 {string} string "{"success":true,"data":{},"msg":"设置成功"}"
  170. // @Router /authority/setDataAuthority [post]
  171. func SetDataAuthority(c *gin.Context) {
  172. var auth model.SysAuthority
  173. _ = c.ShouldBindJSON(&auth)
  174. AuthorityVerify := utils.Rules{
  175. "AuthorityId": {utils.NotEmpty()},
  176. }
  177. AuthorityVerifyErr := utils.Verify(auth, AuthorityVerify)
  178. if AuthorityVerifyErr!=nil {
  179. response.FailWithMessage(AuthorityVerifyErr.Error(), c)
  180. return
  181. }
  182. err := service.SetDataAuthority(auth)
  183. if err != nil {
  184. response.FailWithMessage(fmt.Sprintf("设置关联失败,%v", err), c)
  185. } else {
  186. response.Ok(c)
  187. }
  188. }