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.

164 lines
4.6 KiB

5 years ago
5 years ago
  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. Path string `json:"path"`
  11. Description string `json:"description"`
  12. }
  13. type DeleteApiParams struct {
  14. ID uint `json:"id"`
  15. }
  16. // @Tags Api
  17. // @Summary 创建基础api
  18. // @Security ApiKeyAuth
  19. // @accept application/json
  20. // @Produce application/json
  21. // @Param data body api.CreateApiParams true "创建api"
  22. // @Success 200 {string} json "{"success":true,"data":{},"msg":"获取成功"}"
  23. // @Router /api/createApi [post]
  24. func CreateApi(c *gin.Context) {
  25. var api dbModel.Api
  26. _ = c.BindJSON(&api)
  27. err := api.CreateApi()
  28. if err != nil {
  29. servers.ReportFormat(c, false, fmt.Sprintf("创建失败:%v", err), gin.H{})
  30. } else {
  31. servers.ReportFormat(c, true, "创建成功", gin.H{})
  32. }
  33. }
  34. // @Tags Api
  35. // @Summary 删除指定api
  36. // @Security ApiKeyAuth
  37. // @accept application/json
  38. // @Produce application/json
  39. // @Param data body dbModel.Api true "删除api"
  40. // @Success 200 {string} json "{"success":true,"data":{},"msg":"获取成功"}"
  41. // @Router /api/deleteApi [post]
  42. func DeleteApi(c *gin.Context) {
  43. var a dbModel.Api
  44. _ = c.BindJSON(&a)
  45. err := a.DeleteApi()
  46. if err != nil {
  47. servers.ReportFormat(c, false, fmt.Sprintf("删除失败:%v", err), gin.H{})
  48. } else {
  49. servers.ReportFormat(c, true, "删除成功", gin.H{})
  50. }
  51. }
  52. type AuthAndPathIn struct {
  53. AuthorityId string `json:"authorityId"`
  54. ApiIds []uint `json:"apiIds"`
  55. }
  56. // @Tags Api
  57. // @Summary 创建api和角色关系
  58. // @Security ApiKeyAuth
  59. // @accept application/json
  60. // @Produce application/json
  61. // @Param data body api.AuthAndPathIn true "创建api和角色关系"
  62. // @Success 200 {string} json "{"success":true,"data":{},"msg":"获取成功"}"
  63. // @Router /api/setAuthAndApi [post]
  64. func SetAuthAndApi(c *gin.Context) {
  65. var authAndPathIn AuthAndPathIn
  66. _ = c.BindJSON(&authAndPathIn)
  67. err := new(dbModel.ApiAuthority).SetAuthAndApi(authAndPathIn.AuthorityId, authAndPathIn.ApiIds)
  68. if err != nil {
  69. servers.ReportFormat(c, false, fmt.Sprintf("添加失败:%v", err), gin.H{})
  70. } else {
  71. servers.ReportFormat(c, true, "添加成功", gin.H{})
  72. }
  73. }
  74. // @Tags Api
  75. // @Summary 分页获取API列表
  76. // @Security ApiKeyAuth
  77. // @accept application/json
  78. // @Produce application/json
  79. // @Param data body modelInterface.PageInfo true "分页获取API列表"
  80. // @Success 200 {string} json "{"success":true,"data":{},"msg":"获取成功"}"
  81. // @Router /api/getApiList [post]
  82. func GetApiList(c *gin.Context) {
  83. var pageInfo modelInterface.PageInfo
  84. _ = c.BindJSON(&pageInfo)
  85. err, list, total := new(dbModel.Api).GetInfoList(pageInfo)
  86. if err != nil {
  87. servers.ReportFormat(c, false, fmt.Sprintf("获取数据失败,%v", err), gin.H{})
  88. } else {
  89. servers.ReportFormat(c, true, "获取数据成功", gin.H{
  90. "list": list,
  91. "total": total,
  92. "page": pageInfo.Page,
  93. "pageSize": pageInfo.PageSize,
  94. })
  95. }
  96. }
  97. // @Tags Api
  98. // @Summary 根据id获取api
  99. // @Security ApiKeyAuth
  100. // @accept application/json
  101. // @Produce application/json
  102. // @Param data body modelInterface.PageInfo true "分页获取用户列表"
  103. // @Success 200 {string} json "{"success":true,"data":{},"msg":"获取成功"}"
  104. // @Router /api/getApiById [post]
  105. func GetApiById(c *gin.Context) {
  106. var idInfo GetById
  107. _ = c.BindJSON(&idInfo)
  108. err, api := new(dbModel.Api).GetApiById(idInfo.Id)
  109. if err != nil {
  110. servers.ReportFormat(c, false, fmt.Sprintf("获取数据失败,%v", err), gin.H{})
  111. } else {
  112. servers.ReportFormat(c, true, "获取数据成功", gin.H{
  113. "api": api,
  114. })
  115. }
  116. }
  117. // @Tags Api
  118. // @Summary 创建基础api
  119. // @Security ApiKeyAuth
  120. // @accept application/json
  121. // @Produce application/json
  122. // @Param data body api.CreateApiParams true "创建api"
  123. // @Success 200 {string} json "{"success":true,"data":{},"msg":"获取成功"}"
  124. // @Router /api/updataApi [post]
  125. func UpdataApi(c *gin.Context) {
  126. var api dbModel.Api
  127. _ = c.BindJSON(&api)
  128. err := api.UpdataApi()
  129. if err != nil {
  130. servers.ReportFormat(c, false, fmt.Sprintf("修改数据失败,%v", err), gin.H{})
  131. } else {
  132. servers.ReportFormat(c, true, "修改数据成功", gin.H{})
  133. }
  134. }
  135. // @Tags Api
  136. // @Summary 获取所有的Api 不分页
  137. // @Security ApiKeyAuth
  138. // @accept application/json
  139. // @Produce application/json
  140. // @Success 200 {string} json "{"success":true,"data":{},"msg":"获取成功"}"
  141. // @Router /api/getAllApis [post]
  142. func GetAllApis(c *gin.Context) {
  143. err, apis := new(dbModel.Api).GetAllApis()
  144. if err != nil {
  145. servers.ReportFormat(c, false, fmt.Sprintf("获取数据失败,%v", err), gin.H{})
  146. } else {
  147. servers.ReportFormat(c, true, "获取数据成功", gin.H{
  148. "apis": apis,
  149. })
  150. }
  151. }