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.

171 lines
4.7 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. //条件搜索后端看此api
  75. // @Tags Api
  76. // @Summary 分页获取API列表
  77. // @Security ApiKeyAuth
  78. // @accept application/json
  79. // @Produce application/json
  80. // @Param data body modelInterface.PageInfo true "分页获取API列表"
  81. // @Success 200 {string} json "{"success":true,"data":{},"msg":"获取成功"}"
  82. // @Router /api/getApiList [post]
  83. func GetApiList(c *gin.Context) {
  84. // 此结构体仅本方法使用
  85. type searchParams struct {
  86. dbModel.Api
  87. modelInterface.PageInfo
  88. }
  89. var sp searchParams
  90. _ = c.ShouldBindJSON(&sp)
  91. err, list, total := sp.Api.GetInfoList(sp.PageInfo)
  92. if err != nil {
  93. servers.ReportFormat(c, false, fmt.Sprintf("获取数据失败,%v", err), gin.H{})
  94. } else {
  95. servers.ReportFormat(c, true, "获取数据成功", gin.H{
  96. "list": list,
  97. "total": total,
  98. "page": sp.PageInfo.Page,
  99. "pageSize": sp.PageInfo.PageSize,
  100. })
  101. }
  102. }
  103. // @Tags Api
  104. // @Summary 根据id获取api
  105. // @Security ApiKeyAuth
  106. // @accept application/json
  107. // @Produce application/json
  108. // @Param data body modelInterface.PageInfo true "分页获取用户列表"
  109. // @Success 200 {string} json "{"success":true,"data":{},"msg":"获取成功"}"
  110. // @Router /api/getApiById [post]
  111. func GetApiById(c *gin.Context) {
  112. var idInfo GetById
  113. _ = c.BindJSON(&idInfo)
  114. err, api := new(dbModel.Api).GetApiById(idInfo.Id)
  115. if err != nil {
  116. servers.ReportFormat(c, false, fmt.Sprintf("获取数据失败,%v", err), gin.H{})
  117. } else {
  118. servers.ReportFormat(c, true, "获取数据成功", gin.H{
  119. "api": api,
  120. })
  121. }
  122. }
  123. // @Tags Api
  124. // @Summary 创建基础api
  125. // @Security ApiKeyAuth
  126. // @accept application/json
  127. // @Produce application/json
  128. // @Param data body api.CreateApiParams true "创建api"
  129. // @Success 200 {string} json "{"success":true,"data":{},"msg":"获取成功"}"
  130. // @Router /api/updataApi [post]
  131. func UpdataApi(c *gin.Context) {
  132. var api dbModel.Api
  133. _ = c.BindJSON(&api)
  134. err := api.UpdataApi()
  135. if err != nil {
  136. servers.ReportFormat(c, false, fmt.Sprintf("修改数据失败,%v", err), gin.H{})
  137. } else {
  138. servers.ReportFormat(c, true, "修改数据成功", gin.H{})
  139. }
  140. }
  141. // @Tags Api
  142. // @Summary 获取所有的Api 不分页
  143. // @Security ApiKeyAuth
  144. // @accept application/json
  145. // @Produce application/json
  146. // @Success 200 {string} json "{"success":true,"data":{},"msg":"获取成功"}"
  147. // @Router /api/getAllApis [post]
  148. func GetAllApis(c *gin.Context) {
  149. err, apis := new(dbModel.Api).GetAllApis()
  150. if err != nil {
  151. servers.ReportFormat(c, false, fmt.Sprintf("获取数据失败,%v", err), gin.H{})
  152. } else {
  153. servers.ReportFormat(c, true, "获取数据成功", gin.H{
  154. "apis": apis,
  155. })
  156. }
  157. }