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.

110 lines
3.2 KiB

  1. package api
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. "main/controller/servers"
  6. "main/model/dbModel"
  7. "main/model/modelInterface"
  8. )
  9. type CreateAuthorityPatams struct {
  10. AuthorityId string `json:"authorityId"`
  11. AuthorityName string `json:"authorityName"`
  12. }
  13. // @Tags authority
  14. // @Summary 创建角色
  15. // @Security ApiKeyAuth
  16. // @accept application/json
  17. // @Produce application/json
  18. // @Param data body api.CreateAuthorityPatams true "创建角色"
  19. // @Success 200 {string} json "{"success":true,"data":{},"msg":"获取成功"}"
  20. // @Router /authority/createAuthority [post]
  21. func CreateAuthority(c *gin.Context) {
  22. var auth dbModel.Authority
  23. _ = c.ShouldBind(&auth)
  24. err, authBack := auth.CreateAuthority()
  25. if err != nil {
  26. servers.ReportFormat(c, false, fmt.Sprintf("创建失败:%v", err), gin.H{
  27. "authority": authBack,
  28. })
  29. } else {
  30. servers.ReportFormat(c, true, "创建成功", gin.H{
  31. "authority": authBack,
  32. })
  33. }
  34. }
  35. type DeleteAuthorityPatams struct {
  36. AuthorityId uint `json:"authorityId"`
  37. }
  38. // @Tags authority
  39. // @Summary 删除角色
  40. // @Security ApiKeyAuth
  41. // @accept application/json
  42. // @Produce application/json
  43. // @Param data body api.DeleteAuthorityPatams true "删除角色"
  44. // @Success 200 {string} json "{"success":true,"data":{},"msg":"获取成功"}"
  45. // @Router /authority/deleteAuthority [post]
  46. func DeleteAuthority(c *gin.Context) {
  47. var a dbModel.Authority
  48. _ = c.BindJSON(&a)
  49. //删除角色之前需要判断是否有用户正在使用此角色
  50. err := a.DeleteAuthority()
  51. if err != nil {
  52. servers.ReportFormat(c, false, fmt.Sprintf("删除失败:%v", err), gin.H{})
  53. } else {
  54. servers.ReportFormat(c, true, "删除成功", gin.H{})
  55. }
  56. }
  57. // @Tags authority
  58. // @Summary 分页获取角色列表
  59. // @Security ApiKeyAuth
  60. // @accept application/json
  61. // @Produce application/json
  62. // @Param data body modelInterface.PageInfo true "分页获取用户列表"
  63. // @Success 200 {string} json "{"success":true,"data":{},"msg":"获取成功"}"
  64. // @Router /authority/getAuthorityList [post]
  65. func GetAuthorityList(c *gin.Context){
  66. var pageInfo modelInterface.PageInfo
  67. _ = c.BindJSON(&pageInfo)
  68. err, list, total := new(dbModel.Authority).GetInfoList(pageInfo)
  69. if err != nil {
  70. servers.ReportFormat(c, false, fmt.Sprintf("获取数据失败,%v", err), gin.H{})
  71. } else {
  72. servers.ReportFormat(c, true, "获取数据成功", gin.H{
  73. "list": list,
  74. "total": total,
  75. "page": pageInfo.Page,
  76. "pageSize": pageInfo.PageSize,
  77. })
  78. }
  79. }
  80. type GetAuthorityId struct {
  81. AuthorityId string `json:"authorityId"`
  82. }
  83. // @Tags authority
  84. // @Summary 获取本角色所有有权限的apiId
  85. // @Security ApiKeyAuth
  86. // @accept application/json
  87. // @Produce application/json
  88. // @Param data body api.GetAuthorityId true "获取本角色所有有权限的apiId"
  89. // @Success 200 {string} json "{"success":true,"data":{},"msg":"获取成功"}"
  90. // @Router /authority/getAuthAndApi [post]
  91. func GetAuthAndApi(c *gin.Context){
  92. var idInfo GetAuthorityId
  93. _ = c.BindJSON(&idInfo)
  94. err,apis := new(dbModel.ApiAuthority).GetAuthAndApi(idInfo.AuthorityId)
  95. if err != nil {
  96. servers.ReportFormat(c, false, fmt.Sprintf("获取数据失败,%v", err), gin.H{})
  97. } else {
  98. servers.ReportFormat(c, true, "获取数据成功", gin.H{
  99. "apis": apis,
  100. })
  101. }
  102. }