sys_api.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package system
  2. import (
  3. "errors"
  4. "fmt"
  5. "lcfns/global"
  6. "lcfns/model/common/request"
  7. "lcfns/model/system"
  8. "gorm.io/gorm"
  9. )
  10. //@author: [piexlmax](https://github.com/piexlmax)
  11. //@function: CreateApi
  12. //@description: 新增基础api
  13. //@param: api model.SysApi
  14. //@return: err error
  15. type ApiService struct{}
  16. var ApiServiceApp = new(ApiService)
  17. func (apiService *ApiService) CreateApi(api system.SysApi) (err error) {
  18. if !errors.Is(global.Db.Where("path = ? AND method = ?", api.Path, api.Method).First(&system.SysApi{}).Error, gorm.ErrRecordNotFound) {
  19. return errors.New("存在相同api")
  20. }
  21. return global.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. var entity system.SysApi
  30. err = global.Db.Where("id = ?", api.ID).First(&entity).Error // 根据id查询api记录
  31. if errors.Is(err, gorm.ErrRecordNotFound) { // api记录不存在
  32. return err
  33. }
  34. err = global.Db.Delete(&entity).Error
  35. if err != nil {
  36. return err
  37. }
  38. CasbinServiceApp.ClearCasbin(1, entity.Path, entity.Method)
  39. e := CasbinServiceApp.Casbin()
  40. err = e.InvalidateCache()
  41. if err != nil {
  42. return err
  43. }
  44. return nil
  45. }
  46. //@author: [piexlmax](https://github.com/piexlmax)
  47. //@function: GetAPIInfoList
  48. //@description: 分页获取数据,
  49. //@param: api model.SysApi, info request.PageInfo, order string, desc bool
  50. //@return: list interface{}, total int64, err error
  51. func (apiService *ApiService) GetAPIInfoList(api system.SysApi, info request.PageInfo, order string, desc bool) (list interface{}, total int64, err error) {
  52. limit := info.PageSize
  53. offset := info.PageSize * (info.Page - 1)
  54. db := global.Db.Model(&system.SysApi{})
  55. var apiList []system.SysApi
  56. if api.Path != "" {
  57. db = db.Where("path LIKE ?", "%"+api.Path+"%")
  58. }
  59. if api.Description != "" {
  60. db = db.Where("description LIKE ?", "%"+api.Description+"%")
  61. }
  62. if api.Method != "" {
  63. db = db.Where("method = ?", api.Method)
  64. }
  65. if api.ApiGroup != "" {
  66. db = db.Where("api_group = ?", api.ApiGroup)
  67. }
  68. err = db.Count(&total).Error
  69. if err != nil {
  70. return apiList, total, err
  71. } else {
  72. db = db.Limit(limit).Offset(offset)
  73. if order != "" {
  74. var OrderStr string
  75. // 设置有效排序key 防止sql注入
  76. // 感谢 Tom4t0 提交漏洞信息
  77. orderMap := make(map[string]bool, 5)
  78. orderMap["id"] = true
  79. orderMap["path"] = true
  80. orderMap["api_group"] = true
  81. orderMap["description"] = true
  82. orderMap["method"] = true
  83. if orderMap[order] {
  84. if desc {
  85. OrderStr = order + " desc"
  86. } else {
  87. OrderStr = order
  88. }
  89. } else { // didn't matched any order key in `orderMap`
  90. err = fmt.Errorf("非法的排序字段: %v", order)
  91. return apiList, total, err
  92. }
  93. err = db.Order(OrderStr).Find(&apiList).Error
  94. } else {
  95. err = db.Order("api_group").Find(&apiList).Error
  96. }
  97. }
  98. return apiList, total, err
  99. }
  100. //@author: [piexlmax](https://github.com/piexlmax)
  101. //@function: GetAllApis
  102. //@description: 获取所有的api
  103. //@return: apis []model.SysApi, err error
  104. func (apiService *ApiService) GetAllApis() (apis []system.SysApi, err error) {
  105. err = global.Db.Find(&apis).Error
  106. return
  107. }
  108. //@author: [piexlmax](https://github.com/piexlmax)
  109. //@function: GetApiById
  110. //@description: 根据id获取api
  111. //@param: id float64
  112. //@return: api model.SysApi, err error
  113. func (apiService *ApiService) GetApiById(id int) (api system.SysApi, err error) {
  114. err = global.Db.Where("id = ?", id).First(&api).Error
  115. return
  116. }
  117. //@author: [piexlmax](https://github.com/piexlmax)
  118. //@function: UpdateApi
  119. //@description: 根据id更新api
  120. //@param: api model.SysApi
  121. //@return: err error
  122. func (apiService *ApiService) UpdateApi(api system.SysApi) (err error) {
  123. var oldA system.SysApi
  124. err = global.Db.Where("id = ?", api.ID).First(&oldA).Error
  125. if oldA.Path != api.Path || oldA.Method != api.Method {
  126. if !errors.Is(global.Db.Where("path = ? AND method = ?", api.Path, api.Method).First(&system.SysApi{}).Error, gorm.ErrRecordNotFound) {
  127. return errors.New("存在相同api路径")
  128. }
  129. }
  130. if err != nil {
  131. return err
  132. } else {
  133. err = CasbinServiceApp.UpdateCasbinApi(oldA.Path, api.Path, oldA.Method, api.Method)
  134. if err != nil {
  135. return err
  136. } else {
  137. err = global.Db.Omit("created_at").Save(&api).Error
  138. }
  139. }
  140. return err
  141. }
  142. //@author: [piexlmax](https://github.com/piexlmax)
  143. //@function: DeleteApis
  144. //@description: 删除选中API
  145. //@param: apis []model.SysApi
  146. //@return: err error
  147. func (apiService *ApiService) DeleteApisByIds(ids request.IdsReq) (err error) {
  148. var apis []system.SysApi
  149. err = global.Db.Find(&apis, "id in ?", ids.Ids).Delete(&apis).Error
  150. if err != nil {
  151. return err
  152. } else {
  153. for _, sysApi := range apis {
  154. CasbinServiceApp.ClearCasbin(1, sysApi.Path, sysApi.Method)
  155. }
  156. e := CasbinServiceApp.Casbin()
  157. err = e.InvalidateCache()
  158. if err != nil {
  159. return err
  160. }
  161. }
  162. return err
  163. }
  164. func (apiService *ApiService) FreshCasbin() (err error) {
  165. e := CasbinServiceApp.Casbin()
  166. err = e.LoadPolicy()
  167. return err
  168. }