sys_api.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 SysApi
  13. // @Summary 创建基础api
  14. // @Security ApiKeyAuth
  15. // @accept application/json
  16. // @Produce application/json
  17. // @Param data body model.SysApi true "创建api"
  18. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  19. // @Router /api/createApi [post]
  20. func CreateApi(c *gin.Context) {
  21. var api model.SysApi
  22. _ = c.ShouldBindJSON(&api)
  23. ApiVerify := utils.Rules{
  24. "Path": {utils.NotEmpty()},
  25. "Description": {utils.NotEmpty()},
  26. "ApiGroup": {utils.NotEmpty()},
  27. "Method": {utils.NotEmpty()},
  28. }
  29. ApiVerifyErr := utils.Verify(api, ApiVerify)
  30. if ApiVerifyErr != nil {
  31. response.FailWithMessage(ApiVerifyErr.Error(), c)
  32. return
  33. }
  34. err := service.CreateApi(api)
  35. if err != nil {
  36. response.FailWithMessage(fmt.Sprintf("创建失败,%v", err), c)
  37. } else {
  38. response.OkWithMessage("创建成功", c)
  39. }
  40. }
  41. // @Tags SysApi
  42. // @Summary 删除指定api
  43. // @Security ApiKeyAuth
  44. // @accept application/json
  45. // @Produce application/json
  46. // @Param data body model.SysApi true "删除api"
  47. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  48. // @Router /api/deleteApi [post]
  49. func DeleteApi(c *gin.Context) {
  50. var a model.SysApi
  51. _ = c.ShouldBindJSON(&a)
  52. ApiVerify := utils.Rules{
  53. "ID": {utils.NotEmpty()},
  54. }
  55. ApiVerifyErr := utils.Verify(a.Model, ApiVerify)
  56. if ApiVerifyErr != nil {
  57. response.FailWithMessage(ApiVerifyErr.Error(), c)
  58. return
  59. }
  60. err := service.DeleteApi(a)
  61. if err != nil {
  62. response.FailWithMessage(fmt.Sprintf("删除失败,%v", err), c)
  63. } else {
  64. response.OkWithMessage("删除成功", c)
  65. }
  66. }
  67. //条件搜索后端看此api
  68. // @Tags SysApi
  69. // @Summary 分页获取API列表
  70. // @Security ApiKeyAuth
  71. // @accept application/json
  72. // @Produce application/json
  73. // @Param data body request.SearchApiParams true "分页获取API列表"
  74. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  75. // @Router /api/getApiList [post]
  76. func GetApiList(c *gin.Context) {
  77. // 此结构体仅本方法使用
  78. var sp request.SearchApiParams
  79. _ = c.ShouldBindJSON(&sp)
  80. PageVerifyErr := utils.Verify(sp.PageInfo, utils.CustomizeMap["PageVerify"])
  81. if PageVerifyErr != nil {
  82. response.FailWithMessage(PageVerifyErr.Error(), c)
  83. return
  84. }
  85. err, list, total := service.GetAPIInfoList(sp.SysApi, sp.PageInfo, sp.OrderKey, sp.Desc)
  86. if err != nil {
  87. response.FailWithMessage(fmt.Sprintf("获取数据失败,%v", err), c)
  88. } else {
  89. response.OkWithData(resp.PageResult{
  90. List: list,
  91. Total: total,
  92. Page: sp.PageInfo.Page,
  93. PageSize: sp.PageInfo.PageSize,
  94. }, c)
  95. }
  96. }
  97. // @Tags SysApi
  98. // @Summary 根据id获取api
  99. // @Security ApiKeyAuth
  100. // @accept application/json
  101. // @Produce application/json
  102. // @Param data body request.GetById true "根据id获取api"
  103. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  104. // @Router /api/getApiById [post]
  105. func GetApiById(c *gin.Context) {
  106. var idInfo request.GetById
  107. _ = c.ShouldBindJSON(&idInfo)
  108. ApiVerify := utils.Rules{
  109. "Id": {utils.NotEmpty()},
  110. }
  111. ApiVerifyErr := utils.Verify(idInfo, ApiVerify)
  112. if ApiVerifyErr != nil {
  113. response.FailWithMessage(ApiVerifyErr.Error(), c)
  114. return
  115. }
  116. err, api := service.GetApiById(idInfo.Id)
  117. if err != nil {
  118. response.FailWithMessage(fmt.Sprintf("获取数据失败,%v", err), c)
  119. } else {
  120. response.OkWithData(resp.SysAPIResponse{Api: api}, c)
  121. }
  122. }
  123. // @Tags SysApi
  124. // @Summary 创建基础api
  125. // @Security ApiKeyAuth
  126. // @accept application/json
  127. // @Produce application/json
  128. // @Param data body model.SysApi true "创建api"
  129. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  130. // @Router /api/updateApi [post]
  131. func UpdateApi(c *gin.Context) {
  132. var api model.SysApi
  133. _ = c.ShouldBindJSON(&api)
  134. ApiVerify := utils.Rules{
  135. "Path": {utils.NotEmpty()},
  136. "Description": {utils.NotEmpty()},
  137. "ApiGroup": {utils.NotEmpty()},
  138. "Method": {utils.NotEmpty()},
  139. }
  140. ApiVerifyErr := utils.Verify(api, ApiVerify)
  141. if ApiVerifyErr != nil {
  142. response.FailWithMessage(ApiVerifyErr.Error(), c)
  143. return
  144. }
  145. err := service.UpdateApi(api)
  146. if err != nil {
  147. response.FailWithMessage(fmt.Sprintf("修改数据失败,%v", err), c)
  148. } else {
  149. response.OkWithMessage("修改数据成功", c)
  150. }
  151. }
  152. // @Tags SysApi
  153. // @Summary 获取所有的Api 不分页
  154. // @Security ApiKeyAuth
  155. // @accept application/json
  156. // @Produce application/json
  157. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  158. // @Router /api/getAllApis [post]
  159. func GetAllApis(c *gin.Context) {
  160. err, apis := service.GetAllApis()
  161. if err != nil {
  162. response.FailWithMessage(fmt.Sprintf("获取数据失败,%v", err), c)
  163. } else {
  164. response.OkWithData(resp.SysAPIListResponse{Apis: apis}, c)
  165. }
  166. }