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.

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