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.

104 lines
3.0 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 CreateApiParams struct {
  10. AuthorityId uint `json:"-"`
  11. Path string `json:"path"`
  12. Description string `json:"description"`
  13. }
  14. type DeleteApiParams struct {
  15. AuthorityId uint `json:"-"`
  16. }
  17. // @Tags Api
  18. // @Summary 为指定角色创建api
  19. // @Security ApiKeyAuth
  20. // @accept application/json
  21. // @Produce application/json
  22. // @Param data body api.CreateApiParams true "创建api"
  23. // @Success 200 {string} json "{"success":true,"data":{},"msg":"获取成功"}"
  24. // @Router /api/createApi [post]
  25. func CreateApi(c *gin.Context) {
  26. var api dbModel.Api
  27. _ = c.BindJSON(&api)
  28. err := api.CreateApi()
  29. if err != nil {
  30. servers.ReportFormat(c, false, fmt.Sprintf("创建失败:%v", err), gin.H{})
  31. } else {
  32. servers.ReportFormat(c, true, "创建成功", gin.H{})
  33. }
  34. }
  35. // @Tags Api
  36. // @Summary 删除指定角色api
  37. // @Security ApiKeyAuth
  38. // @accept application/json
  39. // @Produce application/json
  40. // @Param data body api.DeleteApiParams true "删除api"
  41. // @Success 200 {string} json "{"success":true,"data":{},"msg":"获取成功"}"
  42. // @Router /api/deleteApi [post]
  43. func DeleteApi(c *gin.Context) {
  44. var a dbModel.Api
  45. _ = c.BindJSON(&a)
  46. err := a.DeleteApi()
  47. if err != nil {
  48. servers.ReportFormat(c, false, fmt.Sprintf("删除失败:%v", err), gin.H{})
  49. } else {
  50. servers.ReportFormat(c, true, "删除成功", gin.H{})
  51. }
  52. }
  53. type AuthAndPathIn struct {
  54. AuthorityId string `json:"authorityId"`
  55. Apis []dbModel.Api `json:"apis"`
  56. }
  57. // @Tags Api
  58. // @Summary 创建api和角色关系
  59. // @Security ApiKeyAuth
  60. // @accept application/json
  61. // @Produce application/json
  62. // @Param data body api.AuthAndPathIn true "创建api和角色关系"
  63. // @Success 200 {string} json "{"success":true,"data":{},"msg":"获取成功"}"
  64. // @Router /api/setAuthAndPath [post]
  65. func SetAuthAndPath(c *gin.Context){
  66. var authAndPathIn AuthAndPathIn
  67. _ = c.BindJSON(&authAndPathIn)
  68. err:=new(dbModel.ApiAuthority).SetAuthAndPath(authAndPathIn.AuthorityId,authAndPathIn.Apis)
  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. }
  74. }
  75. // @Tags api
  76. // @Summary 分页获取角色列表
  77. // @Security ApiKeyAuth
  78. // @accept application/json
  79. // @Produce application/json
  80. // @Param data body modelInterface.PageInfo true "分页获取用户列表"
  81. // @Success 200 {string} json "{"success":true,"data":{},"msg":"获取成功"}"
  82. // @Router /api/getApiList [post]
  83. func GetApiList(c *gin.Context) {
  84. var pageInfo modelInterface.PageInfo
  85. _ = c.BindJSON(&pageInfo)
  86. err, list, total := new(dbModel.Api).GetInfoList(pageInfo)
  87. if err != nil {
  88. servers.ReportFormat(c, false, fmt.Sprintf("获取数据失败,%v", err), gin.H{})
  89. } else {
  90. servers.ReportFormat(c, true, "获取数据成功", gin.H{
  91. "list": list,
  92. "total": total,
  93. "page": pageInfo.Page,
  94. "pageSize": pageInfo.PageSize,
  95. })
  96. }
  97. }