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.

149 lines
4.6 KiB

5 years ago
  1. package v1
  2. import (
  3. "gin-vue-admin/global"
  4. "gin-vue-admin/model"
  5. "gin-vue-admin/model/request"
  6. "gin-vue-admin/model/response"
  7. "gin-vue-admin/service"
  8. "gin-vue-admin/utils"
  9. "github.com/gin-gonic/gin"
  10. "go.uber.org/zap"
  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路径, api中文描述, 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. if err := utils.Verify(api, utils.ApiVerify); err != nil {
  24. response.FailWithMessage(err.Error(), c)
  25. return
  26. }
  27. if err := service.CreateApi(api); err != nil {
  28. global.GVA_LOG.Error("创建失败!", zap.Any("err", err))
  29. response.FailWithMessage("创建失败", c)
  30. } else {
  31. response.OkWithMessage("创建成功", c)
  32. }
  33. }
  34. // @Tags SysApi
  35. // @Summary 删除api
  36. // @Security ApiKeyAuth
  37. // @accept application/json
  38. // @Produce application/json
  39. // @Param data body model.SysApi true "ID"
  40. // @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
  41. // @Router /api/deleteApi [post]
  42. func DeleteApi(c *gin.Context) {
  43. var api model.SysApi
  44. _ = c.ShouldBindJSON(&api)
  45. if err := utils.Verify(api.GVA_MODEL, utils.IdVerify); err != nil {
  46. response.FailWithMessage(err.Error(), c)
  47. return
  48. }
  49. if err := service.DeleteApi(api); err != nil {
  50. global.GVA_LOG.Error("删除失败!", zap.Any("err", err))
  51. response.FailWithMessage("删除失败", c)
  52. } else {
  53. response.OkWithMessage("删除成功", c)
  54. }
  55. }
  56. // @Tags SysApi
  57. // @Summary 分页获取API列表
  58. // @Security ApiKeyAuth
  59. // @accept application/json
  60. // @Produce application/json
  61. // @Param data body request.SearchApiParams true "分页获取API列表"
  62. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  63. // @Router /api/getApiList [post]
  64. func GetApiList(c *gin.Context) {
  65. var pageInfo request.SearchApiParams
  66. _ = c.ShouldBindJSON(&pageInfo)
  67. if err := utils.Verify(pageInfo.PageInfo, utils.PageInfoVerify); err != nil {
  68. response.FailWithMessage(err.Error(), c)
  69. return
  70. }
  71. if err, list, total := service.GetAPIInfoList(pageInfo.SysApi, pageInfo.PageInfo, pageInfo.OrderKey, pageInfo.Desc); err != nil {
  72. global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
  73. response.FailWithMessage("获取失败", c)
  74. } else {
  75. response.OkWithDetailed(response.PageResult{
  76. List: list,
  77. Total: total,
  78. Page: pageInfo.Page,
  79. PageSize: pageInfo.PageSize,
  80. }, "获取成功", c)
  81. }
  82. }
  83. // @Tags SysApi
  84. // @Summary 根据id获取api
  85. // @Security ApiKeyAuth
  86. // @accept application/json
  87. // @Produce application/json
  88. // @Param data body request.GetById true "根据id获取api"
  89. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  90. // @Router /api/getApiById [post]
  91. func GetApiById(c *gin.Context) {
  92. var idInfo request.GetById
  93. _ = c.ShouldBindJSON(&idInfo)
  94. if err := utils.Verify(idInfo, utils.IdVerify); err != nil {
  95. response.FailWithMessage(err.Error(), c)
  96. return
  97. }
  98. err, api := service.GetApiById(idInfo.Id)
  99. if err != nil {
  100. global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
  101. response.FailWithMessage("获取失败", c)
  102. } else {
  103. response.OkWithData(response.SysAPIResponse{Api: api}, c)
  104. }
  105. }
  106. // @Tags SysApi
  107. // @Summary 创建基础api
  108. // @Security ApiKeyAuth
  109. // @accept application/json
  110. // @Produce application/json
  111. // @Param data body model.SysApi true "api路径, api中文描述, api组, 方法"
  112. // @Success 200 {string} string "{"success":true,"data":{},"msg":"修改成功"}"
  113. // @Router /api/updateApi [post]
  114. func UpdateApi(c *gin.Context) {
  115. var api model.SysApi
  116. _ = c.ShouldBindJSON(&api)
  117. if err := utils.Verify(api, utils.ApiVerify); err != nil {
  118. response.FailWithMessage(err.Error(), c)
  119. return
  120. }
  121. if err := service.UpdateApi(api); err != nil {
  122. global.GVA_LOG.Error("修改失败!", zap.Any("err", err))
  123. response.FailWithMessage("修改失败", c)
  124. } else {
  125. response.OkWithMessage("修改成功", c)
  126. }
  127. }
  128. // @Tags SysApi
  129. // @Summary 获取所有的Api 不分页
  130. // @Security ApiKeyAuth
  131. // @accept application/json
  132. // @Produce application/json
  133. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  134. // @Router /api/getAllApis [post]
  135. func GetAllApis(c *gin.Context) {
  136. if err, apis := service.GetAllApis(); err != nil {
  137. global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
  138. response.FailWithMessage("获取失败", c)
  139. } else {
  140. response.OkWithDetailed(response.SysAPIListResponse{Apis: apis}, "获取成功", c)
  141. }
  142. }