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.

152 lines
4.0 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package system
  2. import (
  3. "errors"
  4. "gin-vue-admin/global"
  5. "gin-vue-admin/model/common/request"
  6. "gin-vue-admin/model/system"
  7. "gorm.io/gorm"
  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. }
  16. var ApiServiceApp = new(ApiService)
  17. func (apiService *ApiService) CreateApi(api system.SysApi) (err error) {
  18. if !errors.Is(global.GVA_DB.Where("path = ? AND method = ?", api.Path, api.Method).First(&system.SysApi{}).Error, gorm.ErrRecordNotFound) {
  19. return errors.New("存在相同api")
  20. }
  21. return global.GVA_DB.Create(&api).Error
  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 system.SysApi) (err error) {
  29. err = global.GVA_DB.Delete(&api).Error
  30. CasbinServiceApp.ClearCasbin(1, api.Path, api.Method)
  31. return err
  32. }
  33. //@author: [piexlmax](https://github.com/piexlmax)
  34. //@function: GetAPIInfoList
  35. //@description: 分页获取数据,
  36. //@param: api model.SysApi, info request.PageInfo, order string, desc bool
  37. //@return: err error
  38. func (apiService *ApiService) GetAPIInfoList(api system.SysApi, info request.PageInfo, order string, desc bool) (err error, list interface{}, total int64) {
  39. limit := info.PageSize
  40. offset := info.PageSize * (info.Page - 1)
  41. db := global.GVA_DB.Model(&system.SysApi{})
  42. var apiList []system.SysApi
  43. if api.Path != "" {
  44. db = db.Where("path LIKE ?", "%"+api.Path+"%")
  45. }
  46. if api.Description != "" {
  47. db = db.Where("description LIKE ?", "%"+api.Description+"%")
  48. }
  49. if api.Method != "" {
  50. db = db.Where("method = ?", api.Method)
  51. }
  52. if api.ApiGroup != "" {
  53. db = db.Where("api_group = ?", api.ApiGroup)
  54. }
  55. err = db.Count(&total).Error
  56. if err != nil {
  57. return err, apiList, total
  58. } else {
  59. db = db.Limit(limit).Offset(offset)
  60. if order != "" {
  61. var OrderStr string
  62. if desc {
  63. OrderStr = order + " desc"
  64. } else {
  65. OrderStr = order
  66. }
  67. err = db.Order(OrderStr).Find(&apiList).Error
  68. } else {
  69. err = db.Order("api_group").Find(&apiList).Error
  70. }
  71. }
  72. return err, apiList, total
  73. }
  74. //@author: [piexlmax](https://github.com/piexlmax)
  75. //@function: GetAllApis
  76. //@description: 获取所有的api
  77. //@return: err error, apis []model.SysApi
  78. func (apiService *ApiService) GetAllApis() (err error, apis []system.SysApi) {
  79. err = global.GVA_DB.Find(&apis).Error
  80. return
  81. }
  82. //@author: [piexlmax](https://github.com/piexlmax)
  83. //@function: GetApiById
  84. //@description: 根据id获取api
  85. //@param: id float64
  86. //@return: err error, api model.SysApi
  87. func (apiService *ApiService) GetApiById(id float64) (err error, api system.SysApi) {
  88. err = global.GVA_DB.Where("id = ?", id).First(&api).Error
  89. return
  90. }
  91. //@author: [piexlmax](https://github.com/piexlmax)
  92. //@function: UpdateApi
  93. //@description: 根据id更新api
  94. //@param: api model.SysApi
  95. //@return: err error
  96. func (apiService *ApiService) UpdateApi(api system.SysApi) (err error) {
  97. var oldA system.SysApi
  98. err = global.GVA_DB.Where("id = ?", api.ID).First(&oldA).Error
  99. if oldA.Path != api.Path || oldA.Method != api.Method {
  100. if !errors.Is(global.GVA_DB.Where("path = ? AND method = ?", api.Path, api.Method).First(&system.SysApi{}).Error, gorm.ErrRecordNotFound) {
  101. return errors.New("存在相同api路径")
  102. }
  103. }
  104. if err != nil {
  105. return err
  106. } else {
  107. err = CasbinServiceApp.UpdateCasbinApi(oldA.Path, api.Path, oldA.Method, api.Method)
  108. if err != nil {
  109. return err
  110. } else {
  111. err = global.GVA_DB.Save(&api).Error
  112. }
  113. }
  114. return err
  115. }
  116. //@author: [piexlmax](https://github.com/piexlmax)
  117. //@function: DeleteApis
  118. //@description: 删除选中API
  119. //@param: apis []model.SysApi
  120. //@return: err error
  121. func (apiService *ApiService) DeleteApisByIds(ids request.IdsReq) (err error) {
  122. err = global.GVA_DB.Delete(&[]system.SysApi{}, "id in ?", ids.Ids).Error
  123. return err
  124. }
  125. func (apiService *ApiService) DeleteApiByIds(ids []string) (err error) {
  126. return global.GVA_DB.Delete(system.SysApi{}, ids).Error
  127. }