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.

164 lines
4.4 KiB

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. "github.com/flipped-aurora/gin-vue-admin/server/global"
  5. "github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
  6. "github.com/flipped-aurora/gin-vue-admin/server/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. // 设置有效排序key 防止sql注入
  63. // 感谢 Tom4t0 提交漏洞信息
  64. orderMap := make(map[string]bool, 5)
  65. orderMap["id"] = true
  66. orderMap["path"] = true
  67. orderMap["api_group"] = true
  68. orderMap["description"] = true
  69. orderMap["method"] = true
  70. if orderMap[order] {
  71. if desc {
  72. OrderStr = order + " desc"
  73. } else {
  74. OrderStr = order
  75. }
  76. }
  77. err = db.Order(OrderStr).Find(&apiList).Error
  78. } else {
  79. err = db.Order("api_group").Find(&apiList).Error
  80. }
  81. }
  82. return err, apiList, total
  83. }
  84. //@author: [piexlmax](https://github.com/piexlmax)
  85. //@function: GetAllApis
  86. //@description: 获取所有的api
  87. //@return: err error, apis []model.SysApi
  88. func (apiService *ApiService) GetAllApis() (err error, apis []system.SysApi) {
  89. err = global.GVA_DB.Find(&apis).Error
  90. return
  91. }
  92. //@author: [piexlmax](https://github.com/piexlmax)
  93. //@function: GetApiById
  94. //@description: 根据id获取api
  95. //@param: id float64
  96. //@return: err error, api model.SysApi
  97. func (apiService *ApiService) GetApiById(id float64) (err error, api system.SysApi) {
  98. err = global.GVA_DB.Where("id = ?", id).First(&api).Error
  99. return
  100. }
  101. //@author: [piexlmax](https://github.com/piexlmax)
  102. //@function: UpdateApi
  103. //@description: 根据id更新api
  104. //@param: api model.SysApi
  105. //@return: err error
  106. func (apiService *ApiService) UpdateApi(api system.SysApi) (err error) {
  107. var oldA system.SysApi
  108. err = global.GVA_DB.Where("id = ?", api.ID).First(&oldA).Error
  109. if oldA.Path != api.Path || oldA.Method != api.Method {
  110. if !errors.Is(global.GVA_DB.Where("path = ? AND method = ?", api.Path, api.Method).First(&system.SysApi{}).Error, gorm.ErrRecordNotFound) {
  111. return errors.New("存在相同api路径")
  112. }
  113. }
  114. if err != nil {
  115. return err
  116. } else {
  117. err = CasbinServiceApp.UpdateCasbinApi(oldA.Path, api.Path, oldA.Method, api.Method)
  118. if err != nil {
  119. return err
  120. } else {
  121. err = global.GVA_DB.Save(&api).Error
  122. }
  123. }
  124. return err
  125. }
  126. //@author: [piexlmax](https://github.com/piexlmax)
  127. //@function: DeleteApis
  128. //@description: 删除选中API
  129. //@param: apis []model.SysApi
  130. //@return: err error
  131. func (apiService *ApiService) DeleteApisByIds(ids request.IdsReq) (err error) {
  132. err = global.GVA_DB.Delete(&[]system.SysApi{}, "id in ?", ids.Ids).Error
  133. return err
  134. }
  135. func (apiService *ApiService) DeleteApiByIds(ids []string) (err error) {
  136. return global.GVA_DB.Delete(&system.SysApi{}, "id in ?", ids).Error
  137. }