sys_api.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. package system
  2. import (
  3. "github.com/flipped-aurora/gin-vue-admin/server/global"
  4. "github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
  5. "github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
  6. "github.com/flipped-aurora/gin-vue-admin/server/model/system"
  7. systemReq "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
  8. systemRes "github.com/flipped-aurora/gin-vue-admin/server/model/system/response"
  9. "github.com/flipped-aurora/gin-vue-admin/server/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. global.GVA_LOG.Error("创建失败!", zap.Error(err))
  38. response.FailWithMessage("创建失败", c)
  39. return
  40. }
  41. response.OkWithMessage("创建成功", c)
  42. }
  43. // SyncApi
  44. // @Tags SysApi
  45. // @Summary 同步API
  46. // @Security ApiKeyAuth
  47. // @accept application/json
  48. // @Produce application/json
  49. // @Success 200 {object} response.Response{msg=string} "同步API"
  50. // @Router /api/syncApi [get]
  51. func (s *SystemApiApi) SyncApi(c *gin.Context) {
  52. newApis, deleteApis, ignoreApis, err := apiService.SyncApi()
  53. if err != nil {
  54. global.GVA_LOG.Error("同步失败!", zap.Error(err))
  55. response.FailWithMessage("同步失败", c)
  56. return
  57. }
  58. response.OkWithData(gin.H{
  59. "newApis": newApis,
  60. "deleteApis": deleteApis,
  61. "ignoreApis": ignoreApis,
  62. }, c)
  63. }
  64. // GetApiGroups
  65. // @Tags SysApi
  66. // @Summary 获取API分组
  67. // @Security ApiKeyAuth
  68. // @accept application/json
  69. // @Produce application/json
  70. // @Success 200 {object} response.Response{msg=string} "获取API分组"
  71. // @Router /api/getApiGroups [get]
  72. func (s *SystemApiApi) GetApiGroups(c *gin.Context) {
  73. groups, apiGroupMap, err := apiService.GetApiGroups()
  74. if err != nil {
  75. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  76. response.FailWithMessage("获取失败", c)
  77. return
  78. }
  79. response.OkWithData(gin.H{
  80. "groups": groups,
  81. "apiGroupMap": apiGroupMap,
  82. }, c)
  83. }
  84. // IgnoreApi
  85. // @Tags IgnoreApi
  86. // @Summary 忽略API
  87. // @Security ApiKeyAuth
  88. // @accept application/json
  89. // @Produce application/json
  90. // @Success 200 {object} response.Response{msg=string} "同步API"
  91. // @Router /api/ignoreApi [post]
  92. func (s *SystemApiApi) IgnoreApi(c *gin.Context) {
  93. var ignoreApi system.SysIgnoreApi
  94. err := c.ShouldBindJSON(&ignoreApi)
  95. if err != nil {
  96. response.FailWithMessage(err.Error(), c)
  97. return
  98. }
  99. err = apiService.IgnoreApi(ignoreApi)
  100. if err != nil {
  101. global.GVA_LOG.Error("忽略失败!", zap.Error(err))
  102. response.FailWithMessage("忽略失败", c)
  103. return
  104. }
  105. response.Ok(c)
  106. }
  107. // EnterSyncApi
  108. // @Tags SysApi
  109. // @Summary 确认同步API
  110. // @Security ApiKeyAuth
  111. // @accept application/json
  112. // @Produce application/json
  113. // @Success 200 {object} response.Response{msg=string} "确认同步API"
  114. // @Router /api/enterSyncApi [post]
  115. func (s *SystemApiApi) EnterSyncApi(c *gin.Context) {
  116. var syncApi systemRes.SysSyncApis
  117. err := c.ShouldBindJSON(&syncApi)
  118. if err != nil {
  119. response.FailWithMessage(err.Error(), c)
  120. return
  121. }
  122. err = apiService.EnterSyncApi(syncApi)
  123. if err != nil {
  124. global.GVA_LOG.Error("忽略失败!", zap.Error(err))
  125. response.FailWithMessage("忽略失败", c)
  126. return
  127. }
  128. response.Ok(c)
  129. }
  130. // DeleteApi
  131. // @Tags SysApi
  132. // @Summary 删除api
  133. // @Security ApiKeyAuth
  134. // @accept application/json
  135. // @Produce application/json
  136. // @Param data body system.SysApi true "ID"
  137. // @Success 200 {object} response.Response{msg=string} "删除api"
  138. // @Router /api/deleteApi [post]
  139. func (s *SystemApiApi) DeleteApi(c *gin.Context) {
  140. var api system.SysApi
  141. err := c.ShouldBindJSON(&api)
  142. if err != nil {
  143. response.FailWithMessage(err.Error(), c)
  144. return
  145. }
  146. err = utils.Verify(api.GVA_MODEL, utils.IdVerify)
  147. if err != nil {
  148. response.FailWithMessage(err.Error(), c)
  149. return
  150. }
  151. err = apiService.DeleteApi(api)
  152. if err != nil {
  153. global.GVA_LOG.Error("删除失败!", zap.Error(err))
  154. response.FailWithMessage("删除失败", c)
  155. return
  156. }
  157. response.OkWithMessage("删除成功", c)
  158. }
  159. // GetApiList
  160. // @Tags SysApi
  161. // @Summary 分页获取API列表
  162. // @Security ApiKeyAuth
  163. // @accept application/json
  164. // @Produce application/json
  165. // @Param data body systemReq.SearchApiParams true "分页获取API列表"
  166. // @Success 200 {object} response.Response{data=response.PageResult,msg=string} "分页获取API列表,返回包括列表,总数,页码,每页数量"
  167. // @Router /api/getApiList [post]
  168. func (s *SystemApiApi) GetApiList(c *gin.Context) {
  169. var pageInfo systemReq.SearchApiParams
  170. err := c.ShouldBindJSON(&pageInfo)
  171. if err != nil {
  172. response.FailWithMessage(err.Error(), c)
  173. return
  174. }
  175. err = utils.Verify(pageInfo.PageInfo, utils.PageInfoVerify)
  176. if err != nil {
  177. response.FailWithMessage(err.Error(), c)
  178. return
  179. }
  180. list, total, err := apiService.GetAPIInfoList(pageInfo.SysApi, pageInfo.PageInfo, pageInfo.OrderKey, pageInfo.Desc)
  181. if err != nil {
  182. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  183. response.FailWithMessage("获取失败", c)
  184. return
  185. }
  186. response.OkWithDetailed(response.PageResult{
  187. List: list,
  188. Total: total,
  189. Page: pageInfo.Page,
  190. PageSize: pageInfo.PageSize,
  191. }, "获取成功", c)
  192. }
  193. // GetApiById
  194. // @Tags SysApi
  195. // @Summary 根据id获取api
  196. // @Security ApiKeyAuth
  197. // @accept application/json
  198. // @Produce application/json
  199. // @Param data body request.GetById true "根据id获取api"
  200. // @Success 200 {object} response.Response{data=systemRes.SysAPIResponse} "根据id获取api,返回包括api详情"
  201. // @Router /api/getApiById [post]
  202. func (s *SystemApiApi) GetApiById(c *gin.Context) {
  203. var idInfo request.GetById
  204. err := c.ShouldBindJSON(&idInfo)
  205. if err != nil {
  206. response.FailWithMessage(err.Error(), c)
  207. return
  208. }
  209. err = utils.Verify(idInfo, utils.IdVerify)
  210. if err != nil {
  211. response.FailWithMessage(err.Error(), c)
  212. return
  213. }
  214. api, err := apiService.GetApiById(idInfo.ID)
  215. if err != nil {
  216. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  217. response.FailWithMessage("获取失败", c)
  218. return
  219. }
  220. response.OkWithDetailed(systemRes.SysAPIResponse{Api: api}, "获取成功", c)
  221. }
  222. // UpdateApi
  223. // @Tags SysApi
  224. // @Summary 修改基础api
  225. // @Security ApiKeyAuth
  226. // @accept application/json
  227. // @Produce application/json
  228. // @Param data body system.SysApi true "api路径, api中文描述, api组, 方法"
  229. // @Success 200 {object} response.Response{msg=string} "修改基础api"
  230. // @Router /api/updateApi [post]
  231. func (s *SystemApiApi) UpdateApi(c *gin.Context) {
  232. var api system.SysApi
  233. err := c.ShouldBindJSON(&api)
  234. if err != nil {
  235. response.FailWithMessage(err.Error(), c)
  236. return
  237. }
  238. err = utils.Verify(api, utils.ApiVerify)
  239. if err != nil {
  240. response.FailWithMessage(err.Error(), c)
  241. return
  242. }
  243. err = apiService.UpdateApi(api)
  244. if err != nil {
  245. global.GVA_LOG.Error("修改失败!", zap.Error(err))
  246. response.FailWithMessage("修改失败", c)
  247. return
  248. }
  249. response.OkWithMessage("修改成功", c)
  250. }
  251. // GetAllApis
  252. // @Tags SysApi
  253. // @Summary 获取所有的Api 不分页
  254. // @Security ApiKeyAuth
  255. // @accept application/json
  256. // @Produce application/json
  257. // @Success 200 {object} response.Response{data=systemRes.SysAPIListResponse,msg=string} "获取所有的Api 不分页,返回包括api列表"
  258. // @Router /api/getAllApis [post]
  259. func (s *SystemApiApi) GetAllApis(c *gin.Context) {
  260. authorityID := utils.GetUserAuthorityId(c)
  261. apis, err := apiService.GetAllApis(authorityID)
  262. if err != nil {
  263. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  264. response.FailWithMessage("获取失败", c)
  265. return
  266. }
  267. response.OkWithDetailed(systemRes.SysAPIListResponse{Apis: apis}, "获取成功", c)
  268. }
  269. // DeleteApisByIds
  270. // @Tags SysApi
  271. // @Summary 删除选中Api
  272. // @Security ApiKeyAuth
  273. // @accept application/json
  274. // @Produce application/json
  275. // @Param data body request.IdsReq true "ID"
  276. // @Success 200 {object} response.Response{msg=string} "删除选中Api"
  277. // @Router /api/deleteApisByIds [delete]
  278. func (s *SystemApiApi) DeleteApisByIds(c *gin.Context) {
  279. var ids request.IdsReq
  280. err := c.ShouldBindJSON(&ids)
  281. if err != nil {
  282. response.FailWithMessage(err.Error(), c)
  283. return
  284. }
  285. err = apiService.DeleteApisByIds(ids)
  286. if err != nil {
  287. global.GVA_LOG.Error("删除失败!", zap.Error(err))
  288. response.FailWithMessage("删除失败", c)
  289. return
  290. }
  291. response.OkWithMessage("删除成功", c)
  292. }
  293. // FreshCasbin
  294. // @Tags SysApi
  295. // @Summary 刷新casbin缓存
  296. // @accept application/json
  297. // @Produce application/json
  298. // @Success 200 {object} response.Response{msg=string} "刷新成功"
  299. // @Router /api/freshCasbin [get]
  300. func (s *SystemApiApi) FreshCasbin(c *gin.Context) {
  301. err := casbinService.FreshCasbin()
  302. if err != nil {
  303. global.GVA_LOG.Error("刷新失败!", zap.Error(err))
  304. response.FailWithMessage("刷新失败", c)
  305. return
  306. }
  307. response.OkWithMessage("刷新成功", c)
  308. }