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.

131 lines
3.3 KiB

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