sys_api.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package system
  2. import (
  3. "errors"
  4. "gorm.io/gorm"
  5. "server/dao"
  6. "server/global"
  7. "server/model/common/request"
  8. )
  9. //@author: [piexlmax](https://github.com/piexlmax)
  10. //@function: CreateApi
  11. //@description: 新增基础api
  12. //@param: api model.SysApi
  13. //@return: err error
  14. type ApiService struct{}
  15. var ApiServiceApp = new(ApiService)
  16. func (apiService *ApiService) CreateApi(api dao.SysApi) (err error) {
  17. _, err = dao.QueryApiByPathMethod(api.Path, api.Method)
  18. if !errors.Is(err, gorm.ErrRecordNotFound) {
  19. return errors.New("存在相同api")
  20. }
  21. return api.CreateApi()
  22. }
  23. //@author: [piexlmax](https://github.com/piexlmax)
  24. //@function: DeleteApi
  25. //@description: 删除基础api
  26. //@param: api model.SysApi
  27. //@return: err error
  28. func (apiService *ApiService) DeleteApi(api dao.SysApi) (err error) {
  29. entity, err := dao.QueryApiById(api.ID)
  30. if errors.Is(err, gorm.ErrRecordNotFound) { // api记录不存在
  31. return err
  32. }
  33. err = api.DeleteApi()
  34. if err != nil {
  35. return err
  36. }
  37. CasbinServiceApp.ClearCasbin(1, entity.Path, entity.Method)
  38. if err != nil {
  39. return err
  40. }
  41. return nil
  42. }
  43. //@author: [piexlmax](https://github.com/piexlmax)
  44. //@function: GetAPIInfoList
  45. //@description: 分页获取数据,
  46. //@param: api model.SysApi, info request.PageInfo, order string, desc bool
  47. //@return: list interface{}, total int64, err error
  48. func (apiService *ApiService) GetAPIInfoList(api dao.SysApi, info request.PageInfo, order string, desc bool) (list interface{}, total int64, err error) {
  49. limit := info.PageSize
  50. offset := info.PageSize * (info.Page - 1)
  51. return api.GetAPIInfoList(limit, offset, order, desc)
  52. }
  53. //@author: [piexlmax](https://github.com/piexlmax)
  54. //@function: GetAllApis
  55. //@description: 获取所有的api
  56. //@return: apis []model.SysApi, err error
  57. func (apiService *ApiService) GetAllApis() (apis []dao.SysApi, err error) {
  58. return dao.GetAllApis()
  59. }
  60. //@author: [piexlmax](https://github.com/piexlmax)
  61. //@function: GetApiById
  62. //@description: 根据id获取api
  63. //@param: id float64
  64. //@return: api model.SysApi, err error
  65. func (apiService *ApiService) GetApiById(id int) (api dao.SysApi, err error) {
  66. return dao.QueryApiById(uint(id))
  67. }
  68. //@author: [piexlmax](https://github.com/piexlmax)
  69. //@function: UpdateApi
  70. //@description: 根据id更新api
  71. //@param: api model.SysApi
  72. //@return: err error
  73. func (apiService *ApiService) UpdateApi(api dao.SysApi) (err error) {
  74. oldA, err := dao.QueryApiById(api.ID)
  75. if oldA.Path != api.Path || oldA.Method != api.Method {
  76. var duplicateApi dao.SysApi
  77. duplicateApi, err := dao.QueryApiByPathMethod(api.Path, api.Method)
  78. if err != nil {
  79. if !errors.Is(err, gorm.ErrRecordNotFound) {
  80. return err
  81. }
  82. }
  83. if duplicateApi.ID != api.ID {
  84. return errors.New("存在相同api路径")
  85. }
  86. }
  87. if err != nil {
  88. return err
  89. }
  90. err = CasbinServiceApp.UpdateCasbinApi(oldA.Path, api.Path, oldA.Method, api.Method)
  91. if err != nil {
  92. return err
  93. }
  94. return api.SaveApi()
  95. }
  96. //@author: [piexlmax](https://github.com/piexlmax)
  97. //@function: DeleteApis
  98. //@description: 删除选中API
  99. //@param: apis []model.SysApi
  100. //@return: err error
  101. func (apiService *ApiService) DeleteApisByIds(ids request.IdsReq) (err error) {
  102. return global.GVA_DB.Transaction(func(tx *gorm.DB) error {
  103. var apis []dao.SysApi
  104. err = tx.Find(&apis, "id in ?", ids.Ids).Error
  105. if err != nil {
  106. return err
  107. }
  108. err = tx.Delete(&[]dao.SysApi{}, "id in ?", ids.Ids).Error
  109. if err != nil {
  110. return err
  111. }
  112. for _, sysApi := range apis {
  113. CasbinServiceApp.ClearCasbin(1, sysApi.Path, sysApi.Method)
  114. }
  115. return err
  116. })
  117. }