sys_api.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. package system
  2. import (
  3. "github.com/sirupsen/logrus"
  4. "lc-base-frame/model/common/request"
  5. "lc-base-frame/model/common/response"
  6. "lc-base-frame/model/system"
  7. systemReq "lc-base-frame/model/system/request"
  8. systemRes "lc-base-frame/model/system/response"
  9. "lc-base-frame/utils"
  10. "github.com/gin-gonic/gin"
  11. "go.uber.org/zap"
  12. )
  13. type SystemApiApi struct{}
  14. // CreateApi
  15. // @Tags SysApi
  16. // @Summary 创建基础api
  17. // @Security ApiKeyAuth
  18. // @accept application/json
  19. // @Produce application/json
  20. // @Param data body system.SysApi true "api路径, api中文描述, api组, 方法"
  21. // @Success 200 {object} response.Response{msg=string} "创建基础api"
  22. // @Router /api/createApi [post]
  23. func (s *SystemApiApi) CreateApi(c *gin.Context) {
  24. var api system.SysApi
  25. err := c.ShouldBindJSON(&api)
  26. if err != nil {
  27. response.FailWithMessage(err.Error(), c)
  28. return
  29. }
  30. err = utils.Verify(api, utils.ApiVerify)
  31. if err != nil {
  32. response.FailWithMessage(err.Error(), c)
  33. return
  34. }
  35. err = apiService.CreateApi(api)
  36. if err != nil {
  37. logrus.Error("创建失败!", zap.Error(err))
  38. response.FailWithMessage("创建失败", c)
  39. return
  40. }
  41. response.OkWithMessage("创建成功", c)
  42. }
  43. // DeleteApi
  44. // @Tags SysApi
  45. // @Summary 删除api
  46. // @Security ApiKeyAuth
  47. // @accept application/json
  48. // @Produce application/json
  49. // @Param data body system.SysApi true "ID"
  50. // @Success 200 {object} response.Response{msg=string} "删除api"
  51. // @Router /api/deleteApi [post]
  52. func (s *SystemApiApi) DeleteApi(c *gin.Context) {
  53. var api system.SysApi
  54. err := c.ShouldBindJSON(&api)
  55. if err != nil {
  56. response.FailWithMessage(err.Error(), c)
  57. return
  58. }
  59. err = utils.Verify(api.GVA_MODEL, utils.IdVerify)
  60. if err != nil {
  61. response.FailWithMessage(err.Error(), c)
  62. return
  63. }
  64. err = apiService.DeleteApi(api)
  65. if err != nil {
  66. logrus.Error("删除失败!", zap.Error(err))
  67. response.FailWithMessage("删除失败", c)
  68. return
  69. }
  70. response.OkWithMessage("删除成功", c)
  71. }
  72. // GetApiList
  73. // @Tags SysApi
  74. // @Summary 分页获取API列表
  75. // @Security ApiKeyAuth
  76. // @accept application/json
  77. // @Produce application/json
  78. // @Param data body systemReq.SearchApiParams true "分页获取API列表"
  79. // @Success 200 {object} response.Response{data=response.PageResult,msg=string} "分页获取API列表,返回包括列表,总数,页码,每页数量"
  80. // @Router /api/getApiList [post]
  81. func (s *SystemApiApi) GetApiList(c *gin.Context) {
  82. var pageInfo systemReq.SearchApiParams
  83. err := c.ShouldBindJSON(&pageInfo)
  84. if err != nil {
  85. response.FailWithMessage(err.Error(), c)
  86. return
  87. }
  88. err = utils.Verify(pageInfo.PageInfo, utils.PageInfoVerify)
  89. if err != nil {
  90. response.FailWithMessage(err.Error(), c)
  91. return
  92. }
  93. list, total, err := apiService.GetAPIInfoList(pageInfo.SysApi, pageInfo.PageInfo, pageInfo.OrderKey, pageInfo.Desc)
  94. if err != nil {
  95. logrus.Error("获取失败!", zap.Error(err))
  96. response.FailWithMessage("获取失败", c)
  97. return
  98. }
  99. response.OkWithDetailed(response.PageResult{
  100. List: list,
  101. Total: total,
  102. Page: pageInfo.Page,
  103. PageSize: pageInfo.PageSize,
  104. }, "获取成功", c)
  105. }
  106. // GetApiById
  107. // @Tags SysApi
  108. // @Summary 根据id获取api
  109. // @Security ApiKeyAuth
  110. // @accept application/json
  111. // @Produce application/json
  112. // @Param data body request.GetById true "根据id获取api"
  113. // @Success 200 {object} response.Response{data=systemRes.SysAPIResponse} "根据id获取api,返回包括api详情"
  114. // @Router /api/getApiById [post]
  115. func (s *SystemApiApi) GetApiById(c *gin.Context) {
  116. var idInfo request.GetById
  117. err := c.ShouldBindJSON(&idInfo)
  118. if err != nil {
  119. response.FailWithMessage(err.Error(), c)
  120. return
  121. }
  122. err = utils.Verify(idInfo, utils.IdVerify)
  123. if err != nil {
  124. response.FailWithMessage(err.Error(), c)
  125. return
  126. }
  127. api, err := apiService.GetApiById(idInfo.ID)
  128. if err != nil {
  129. logrus.Error("获取失败!", zap.Error(err))
  130. response.FailWithMessage("获取失败", c)
  131. return
  132. }
  133. response.OkWithDetailed(systemRes.SysAPIResponse{Api: api}, "获取成功", c)
  134. }
  135. // UpdateApi
  136. // @Tags SysApi
  137. // @Summary 修改基础api
  138. // @Security ApiKeyAuth
  139. // @accept application/json
  140. // @Produce application/json
  141. // @Param data body system.SysApi true "api路径, api中文描述, api组, 方法"
  142. // @Success 200 {object} response.Response{msg=string} "修改基础api"
  143. // @Router /api/updateApi [post]
  144. func (s *SystemApiApi) UpdateApi(c *gin.Context) {
  145. var api system.SysApi
  146. err := c.ShouldBindJSON(&api)
  147. if err != nil {
  148. response.FailWithMessage(err.Error(), c)
  149. return
  150. }
  151. err = utils.Verify(api, utils.ApiVerify)
  152. if err != nil {
  153. response.FailWithMessage(err.Error(), c)
  154. return
  155. }
  156. err = apiService.UpdateApi(api)
  157. if err != nil {
  158. logrus.Error("修改失败!", zap.Error(err))
  159. response.FailWithMessage("修改失败", c)
  160. return
  161. }
  162. response.OkWithMessage("修改成功", c)
  163. }
  164. // GetAllApis
  165. // @Tags SysApi
  166. // @Summary 获取所有的Api 不分页
  167. // @Security ApiKeyAuth
  168. // @accept application/json
  169. // @Produce application/json
  170. // @Success 200 {object} response.Response{data=systemRes.SysAPIListResponse,msg=string} "获取所有的Api 不分页,返回包括api列表"
  171. // @Router /api/getAllApis [post]
  172. func (s *SystemApiApi) GetAllApis(c *gin.Context) {
  173. apis, err := apiService.GetAllApis()
  174. if err != nil {
  175. logrus.Error("获取失败!", zap.Error(err))
  176. response.FailWithMessage("获取失败", c)
  177. return
  178. }
  179. response.OkWithDetailed(systemRes.SysAPIListResponse{Apis: apis}, "获取成功", c)
  180. }
  181. // DeleteApisByIds
  182. // @Tags SysApi
  183. // @Summary 删除选中Api
  184. // @Security ApiKeyAuth
  185. // @accept application/json
  186. // @Produce application/json
  187. // @Param data body request.IdsReq true "ID"
  188. // @Success 200 {object} response.Response{msg=string} "删除选中Api"
  189. // @Router /api/deleteApisByIds [delete]
  190. func (s *SystemApiApi) DeleteApisByIds(c *gin.Context) {
  191. var ids request.IdsReq
  192. err := c.ShouldBindJSON(&ids)
  193. if err != nil {
  194. response.FailWithMessage(err.Error(), c)
  195. return
  196. }
  197. err = apiService.DeleteApisByIds(ids)
  198. if err != nil {
  199. logrus.Error("删除失败!", zap.Error(err))
  200. response.FailWithMessage("删除失败", c)
  201. return
  202. }
  203. response.OkWithMessage("删除成功", c)
  204. }
  205. // FreshCasbin
  206. // @Tags SysApi
  207. // @Summary 刷新casbin缓存
  208. // @accept application/json
  209. // @Produce application/json
  210. // @Success 200 {object} response.Response{msg=string} "刷新成功"
  211. // @Router /api/freshCasbin [get]
  212. func (s *SystemApiApi) FreshCasbin(c *gin.Context) {
  213. err := apiService.FreshCasbin()
  214. if err != nil {
  215. logrus.Error("刷新失败!", err)
  216. response.FailWithMessage("刷新失败", c)
  217. return
  218. }
  219. response.OkWithMessage("刷新成功", c)
  220. }