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.

153 lines
3.8 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: CreateApi
  32. //@description: 自动创建api数据,
  33. //@param: api model.SysApi
  34. //@return: err error
  35. func AutoCreateApi(api model.SysApi) (err error) {
  36. err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
  37. var fApi model.SysApi
  38. var txErr error
  39. fxErr := tx.Where("path = ? AND method = ?", api.Path, api.Method).First(&fApi).Error
  40. if errors.Is(fxErr, gorm.ErrRecordNotFound) {
  41. txErr = tx.Create(&api).Error
  42. if txErr != nil{
  43. return txErr
  44. }
  45. }
  46. return nil
  47. })
  48. return err
  49. }
  50. //@author: [piexlmax](https://github.com/piexlmax)
  51. //@function: GetAPIInfoList
  52. //@description: 分页获取数据,
  53. //@param: api model.SysApi, info request.PageInfo, order string, desc bool
  54. //@return: err error
  55. func GetAPIInfoList(api model.SysApi, info request.PageInfo, order string, desc bool) (err error, list interface{}, total int64) {
  56. limit := info.PageSize
  57. offset := info.PageSize * (info.Page - 1)
  58. db := global.GVA_DB.Model(&model.SysApi{})
  59. var apiList []model.SysApi
  60. if api.Path != "" {
  61. db = db.Where("path LIKE ?", "%"+api.Path+"%")
  62. }
  63. if api.Description != "" {
  64. db = db.Where("description LIKE ?", "%"+api.Description+"%")
  65. }
  66. if api.Method != "" {
  67. db = db.Where("method = ?", api.Method)
  68. }
  69. if api.ApiGroup != "" {
  70. db = db.Where("api_group = ?", api.ApiGroup)
  71. }
  72. err = db.Count(&total).Error
  73. if err != nil {
  74. return err, apiList, total
  75. } else {
  76. db = db.Limit(limit).Offset(offset)
  77. if order != "" {
  78. var OrderStr string
  79. if desc {
  80. OrderStr = order + " desc"
  81. } else {
  82. OrderStr = order
  83. }
  84. err = db.Order(OrderStr).Find(&apiList).Error
  85. } else {
  86. err = db.Order("api_group").Find(&apiList).Error
  87. }
  88. }
  89. return err, apiList, total
  90. }
  91. //@author: [piexlmax](https://github.com/piexlmax)
  92. //@function: GetAllApis
  93. //@description: 获取所有的api
  94. //@return: err error, apis []model.SysApi
  95. func GetAllApis() (err error, apis []model.SysApi) {
  96. err = global.GVA_DB.Find(&apis).Error
  97. return
  98. }
  99. //@author: [piexlmax](https://github.com/piexlmax)
  100. //@function: GetApiById
  101. //@description: 根据id获取api
  102. //@param: id float64
  103. //@return: err error, api model.SysApi
  104. func GetApiById(id float64) (err error, api model.SysApi) {
  105. err = global.GVA_DB.Where("id = ?", id).First(&api).Error
  106. return
  107. }
  108. //@author: [piexlmax](https://github.com/piexlmax)
  109. //@function: UpdateApi
  110. //@description: 根据id更新api
  111. //@param: api model.SysApi
  112. //@return: err error
  113. func UpdateApi(api model.SysApi) (err error) {
  114. var oldA model.SysApi
  115. err = global.GVA_DB.Where("id = ?", api.ID).First(&oldA).Error
  116. if oldA.Path != api.Path || oldA.Method != api.Method {
  117. if !errors.Is(global.GVA_DB.Where("path = ? AND method = ?", api.Path, api.Method).First(&model.SysApi{}).Error, gorm.ErrRecordNotFound) {
  118. return errors.New("存在相同api路径")
  119. }
  120. }
  121. if err != nil {
  122. return err
  123. } else {
  124. err = UpdateCasbinApi(oldA.Path, api.Path, oldA.Method, api.Method)
  125. if err != nil {
  126. return err
  127. } else {
  128. err = global.GVA_DB.Save(&api).Error
  129. }
  130. }
  131. return err
  132. }