sys_api.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package apis
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "github.com/gin-gonic/gin/binding"
  5. "github.com/go-admin-team/go-admin-core/sdk/api"
  6. "github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth/user"
  7. _ "github.com/go-admin-team/go-admin-core/sdk/pkg/response"
  8. "go-admin/app/admin/models"
  9. "go-admin/app/admin/service"
  10. "go-admin/app/admin/service/dto"
  11. "go-admin/common/actions"
  12. )
  13. type SysApi struct {
  14. api.Api
  15. }
  16. // GetPage 获取接口管理列表
  17. // @Summary 获取接口管理列表
  18. // @Description 获取接口管理列表
  19. // @Tags 接口管理
  20. // @Param name query string false "名称"
  21. // @Param title query string false "标题"
  22. // @Param path query string false "地址"
  23. // @Param action query string false "类型"
  24. // @Param pageSize query int false "页条数"
  25. // @Param pageIndex query int false "页码"
  26. // @Success 200 {object} response.Response{data=response.Page{list=[]models.SysApi}} "{"code": 200, "data": [...]}"
  27. // @Router /api/v1/sys-api [get]
  28. // @Security Bearer
  29. func (e SysApi) GetPage(c *gin.Context) {
  30. s := service.SysApi{}
  31. req := dto.SysApiGetPageReq{}
  32. err := e.MakeContext(c).
  33. MakeOrm().
  34. Bind(&req, binding.Form).
  35. MakeService(&s.Service).
  36. Errors
  37. if err != nil {
  38. e.Logger.Error(err)
  39. e.Error(500, err, err.Error())
  40. return
  41. }
  42. //数据权限检查
  43. p := actions.GetPermissionFromContext(c)
  44. list := make([]models.SysApi, 0)
  45. var count int64
  46. err = s.GetPage(&req, p, &list, &count)
  47. if err != nil {
  48. e.Error(500, err, "查询失败")
  49. return
  50. }
  51. e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
  52. }
  53. // Get 获取接口管理
  54. // @Summary 获取接口管理
  55. // @Description 获取接口管理
  56. // @Tags 接口管理
  57. // @Param id path string false "id"
  58. // @Success 200 {object} response.Response{data=models.SysApi} "{"code": 200, "data": [...]}"
  59. // @Router /api/v1/sys-api/{id} [get]
  60. // @Security Bearer
  61. func (e SysApi) Get(c *gin.Context) {
  62. req := dto.SysApiGetReq{}
  63. s := service.SysApi{}
  64. err := e.MakeContext(c).
  65. MakeOrm().
  66. Bind(&req, nil).
  67. MakeService(&s.Service).
  68. Errors
  69. if err != nil {
  70. e.Logger.Error(err)
  71. e.Error(500, err, err.Error())
  72. return
  73. }
  74. p := actions.GetPermissionFromContext(c)
  75. var object models.SysApi
  76. err = s.Get(&req, p, &object).Error
  77. if err != nil {
  78. e.Error(500, err, "查询失败")
  79. return
  80. }
  81. e.OK(object, "查询成功")
  82. }
  83. // Update 修改接口管理
  84. // @Summary 修改接口管理
  85. // @Description 修改接口管理
  86. // @Tags 接口管理
  87. // @Accept application/json
  88. // @Product application/json
  89. // @Param data body dto.SysApiUpdateReq true "body"
  90. // @Success 200 {object} response.Response "{"code": 200, "message": "修改成功"}"
  91. // @Router /api/v1/sys-api/{id} [put]
  92. // @Security Bearer
  93. func (e SysApi) Update(c *gin.Context) {
  94. req := dto.SysApiUpdateReq{}
  95. s := service.SysApi{}
  96. err := e.MakeContext(c).
  97. MakeOrm().
  98. Bind(&req).
  99. MakeService(&s.Service).
  100. Errors
  101. if err != nil {
  102. e.Logger.Error(err)
  103. return
  104. }
  105. req.SetUpdateBy(user.GetUserId(c))
  106. p := actions.GetPermissionFromContext(c)
  107. err = s.Update(&req, p)
  108. if err != nil {
  109. e.Error(500, err, "更新失败")
  110. return
  111. }
  112. e.OK(req.GetId(), "更新成功")
  113. }
  114. // DeleteSysApi 删除接口管理
  115. // @Summary 删除接口管理
  116. // @Description 删除接口管理
  117. // @Tags 接口管理
  118. // @Param data body dto.SysApiDeleteReq true "body"
  119. // @Success 200 {object} response.Response "{"code": 200, "message": "删除成功"}"
  120. // @Router /api/v1/sys-api [delete]
  121. // @Security Bearer
  122. func (e SysApi) DeleteSysApi(c *gin.Context) {
  123. req := dto.SysApiDeleteReq{}
  124. s := service.SysApi{}
  125. err := e.MakeContext(c).
  126. MakeOrm().
  127. Bind(&req).
  128. MakeService(&s.Service).
  129. Errors
  130. if err != nil {
  131. e.Logger.Error(err)
  132. return
  133. }
  134. p := actions.GetPermissionFromContext(c)
  135. err = s.Remove(&req, p)
  136. if err != nil {
  137. e.Error(500, err, "删除失败")
  138. return
  139. }
  140. e.OK(req.GetId(), "删除成功")
  141. }