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.9 KiB

5 years ago
5 years ago
  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.GVA_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. IdVerifyErr := utils.Verify(idInfo, utils.CustomizeMap["IdVerify"])
  109. if IdVerifyErr != nil {
  110. response.FailWithMessage(IdVerifyErr.Error(), c)
  111. return
  112. }
  113. err, api := service.GetApiById(idInfo.Id)
  114. if err != nil {
  115. response.FailWithMessage(fmt.Sprintf("获取数据失败,%v", err), c)
  116. } else {
  117. response.OkWithData(resp.SysAPIResponse{Api: api}, c)
  118. }
  119. }
  120. // @Tags SysApi
  121. // @Summary 创建基础api
  122. // @Security ApiKeyAuth
  123. // @accept application/json
  124. // @Produce application/json
  125. // @Param data body model.SysApi true "创建api"
  126. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  127. // @Router /api/updateApi [post]
  128. func UpdateApi(c *gin.Context) {
  129. var api model.SysApi
  130. _ = c.ShouldBindJSON(&api)
  131. ApiVerify := utils.Rules{
  132. "Path": {utils.NotEmpty()},
  133. "Description": {utils.NotEmpty()},
  134. "ApiGroup": {utils.NotEmpty()},
  135. "Method": {utils.NotEmpty()},
  136. }
  137. ApiVerifyErr := utils.Verify(api, ApiVerify)
  138. if ApiVerifyErr != nil {
  139. response.FailWithMessage(ApiVerifyErr.Error(), c)
  140. return
  141. }
  142. err := service.UpdateApi(api)
  143. if err != nil {
  144. response.FailWithMessage(fmt.Sprintf("修改数据失败,%v", err), c)
  145. } else {
  146. response.OkWithMessage("修改数据成功", c)
  147. }
  148. }
  149. // @Tags SysApi
  150. // @Summary 获取所有的Api 不分页
  151. // @Security ApiKeyAuth
  152. // @accept application/json
  153. // @Produce application/json
  154. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  155. // @Router /api/getAllApis [post]
  156. func GetAllApis(c *gin.Context) {
  157. err, apis := service.GetAllApis()
  158. if err != nil {
  159. response.FailWithMessage(fmt.Sprintf("获取数据失败,%v", err), c)
  160. } else {
  161. response.OkWithData(resp.SysAPIListResponse{Apis: apis}, c)
  162. }
  163. }