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.

185 lines
5.8 KiB

  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. AuthorityIdVerifyErr := utils.Verify(a, utils.CustomizeMap["AuthorityIdVerify"])
  88. if AuthorityIdVerifyErr != nil {
  89. response.FailWithMessage(AuthorityIdVerifyErr.Error(), c)
  90. return
  91. }
  92. //删除角色之前需要判断是否有用户正在使用此角色
  93. err := service.DeleteAuthority(&a)
  94. if err != nil {
  95. response.FailWithMessage(fmt.Sprintf("删除失败,%v", err), c)
  96. } else {
  97. response.OkWithMessage("删除成功", c)
  98. }
  99. }
  100. // @Tags authority
  101. // @Summary 设置角色资源权限
  102. // @Security ApiKeyAuth
  103. // @accept application/json
  104. // @Produce application/json
  105. // @Param data body model.SysAuthority true "设置角色资源权限"
  106. // @Success 200 {string} string "{"success":true,"data":{},"msg":"设置成功"}"
  107. // @Router /authority/updateAuthority [post]
  108. func UpdateAuthority(c *gin.Context) {
  109. var auth model.SysAuthority
  110. _ = c.ShouldBindJSON(&auth)
  111. AuthorityVerify := utils.Rules{
  112. "AuthorityId": {utils.NotEmpty()},
  113. "AuthorityName": {utils.NotEmpty()},
  114. "ParentId": {utils.NotEmpty()},
  115. }
  116. AuthorityVerifyErr := utils.Verify(auth, AuthorityVerify)
  117. if AuthorityVerifyErr != nil {
  118. response.FailWithMessage(AuthorityVerifyErr.Error(), c)
  119. return
  120. }
  121. err, authority := service.UpdateAuthority(auth)
  122. if err != nil {
  123. response.FailWithMessage(fmt.Sprintf("更新失败,%v", err), c)
  124. } else {
  125. response.OkWithData(resp.SysAuthorityResponse{authority}, c)
  126. }
  127. }
  128. // @Tags authority
  129. // @Summary 分页获取角色列表
  130. // @Security ApiKeyAuth
  131. // @accept application/json
  132. // @Produce application/json
  133. // @Param data body request.PageInfo true "分页获取用户列表"
  134. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  135. // @Router /authority/getAuthorityList [post]
  136. func GetAuthorityList(c *gin.Context) {
  137. var pageInfo request.PageInfo
  138. _ = c.ShouldBindJSON(&pageInfo)
  139. PageVerifyErr := utils.Verify(pageInfo, utils.CustomizeMap["PageVerify"])
  140. if PageVerifyErr != nil {
  141. response.FailWithMessage(PageVerifyErr.Error(), c)
  142. return
  143. }
  144. err, list, total := service.GetAuthorityInfoList(pageInfo)
  145. if err != nil {
  146. response.FailWithMessage(fmt.Sprintf("获取数据失败,%v", err), c)
  147. } else {
  148. response.OkWithData(resp.PageResult{
  149. List: list,
  150. Total: total,
  151. Page: pageInfo.Page,
  152. PageSize: pageInfo.PageSize,
  153. }, c)
  154. }
  155. }
  156. // @Tags authority
  157. // @Summary 设置角色资源权限
  158. // @Security ApiKeyAuth
  159. // @accept application/json
  160. // @Produce application/json
  161. // @Param data body model.SysAuthority true "设置角色资源权限"
  162. // @Success 200 {string} string "{"success":true,"data":{},"msg":"设置成功"}"
  163. // @Router /authority/setDataAuthority [post]
  164. func SetDataAuthority(c *gin.Context) {
  165. var auth model.SysAuthority
  166. _ = c.ShouldBindJSON(&auth)
  167. AuthorityIdVerifyErr := utils.Verify(auth, utils.CustomizeMap["AuthorityIdVerify"])
  168. if AuthorityIdVerifyErr != nil {
  169. response.FailWithMessage(AuthorityIdVerifyErr.Error(), c)
  170. return
  171. }
  172. err := service.SetDataAuthority(auth)
  173. if err != nil {
  174. response.FailWithMessage(fmt.Sprintf("设置关联失败,%v", err), c)
  175. } else {
  176. response.Ok(c)
  177. }
  178. }