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.

135 lines
3.5 KiB

  1. package model
  2. import (
  3. "gin-vue-admin/global"
  4. "github.com/jinzhu/gorm"
  5. "github.com/pkg/errors"
  6. )
  7. type SysApi struct {
  8. gorm.Model
  9. Path string `json:"path"`
  10. Description string `json:"description"`
  11. ApiGroup string `json:"apiGroup"`
  12. Method string `json:"method" gorm:"default:'POST'"`
  13. }
  14. type CreateApiParams struct {
  15. Path string `json:"path"`
  16. Description string `json:"description"`
  17. }
  18. type DeleteApiParams struct {
  19. ID uint `json:"id"`
  20. }
  21. // @title CreateApi
  22. // @description create base apis, 新增基础api
  23. // @auth (2020/04/05 20:22 )
  24. // @param FileMd5 string
  25. // @param FileName string
  26. // @param FilePath string
  27. // @return error
  28. func (a *SysApi) CreateApi() (err error) {
  29. findOne := global.GVA_DB.Where("path = ?", a.Path).Find(&SysApi{}).Error
  30. if findOne == nil {
  31. return errors.New("存在相同api")
  32. } else {
  33. err = global.GVA_DB.Create(a).Error
  34. }
  35. return err
  36. }
  37. // @title DeleteApi
  38. // @description delete base apis, 删除基础api
  39. // @auth (2020/04/05 20:22 )
  40. // @return error
  41. func (a *SysApi) DeleteApi() (err error) {
  42. err = global.GVA_DB.Delete(a).Error
  43. new(CasbinModel).ClearCasbin(1, a.Path)
  44. return err
  45. }
  46. // @title UpdateApi
  47. // @description update a base api, update api
  48. // @auth (2020/04/05 20:22 )
  49. // @return error
  50. func (a *SysApi) UpdateApi() (err error) {
  51. var oldA SysApi
  52. flag := global.GVA_DB.Where("path = ?", a.Path).Find(&SysApi{}).RecordNotFound()
  53. if !flag {
  54. return errors.New("存在相同api路径")
  55. }
  56. err = global.GVA_DB.Where("id = ?", a.ID).First(&oldA).Error
  57. if err != nil {
  58. return err
  59. } else {
  60. err = new(CasbinModel).UpdateCasbinApi(oldA.Path, a.Path)
  61. if err != nil {
  62. return err
  63. } else {
  64. err = global.GVA_DB.Save(a).Error
  65. }
  66. }
  67. return err
  68. }
  69. // @title GetApiById
  70. // @description get the apis of the selected user, 获取选中角色所拥有的api
  71. // @auth (2020/04/05 20:22 )
  72. // @param id float64
  73. // @return error
  74. func (a *SysApi) GetApiById(id float64) (err error, api SysApi) {
  75. err = global.GVA_DB.Where("id = ?", id).First(&api).Error
  76. return
  77. }
  78. // @title GetAllApis
  79. // @description get all apis, 获取所有的api
  80. // @auth (2020/04/05 20:22 )
  81. // @return err error
  82. // @return apis []SysApi
  83. func (a *SysApi) GetAllApis() (err error, apis []SysApi) {
  84. err = global.GVA_DB.Find(&apis).Error
  85. return
  86. }
  87. // @title GetInfoList
  88. // @description get apis by pagination, 分页获取数据
  89. // @auth (2020/04/05 20:22 )
  90. // @param info PageInfo
  91. // @return err error
  92. // @return list interface{}
  93. // @return total int
  94. func (a *SysApi) GetInfoList(info PageInfo) (err error, list interface{}, total int) {
  95. limit := info.PageSize
  96. offset := info.PageSize * (info.Page - 1)
  97. db := global.GVA_DB
  98. if err != nil {
  99. return
  100. } else {
  101. var apiList []SysApi
  102. if a.Path != "" {
  103. db = db.Where("path LIKE ?", "%"+a.Path+"%")
  104. }
  105. if a.Description != "" {
  106. db = db.Where("description LIKE ?", "%"+a.Description+"%")
  107. }
  108. if a.Method != "" {
  109. db = db.Where("method = ?", a.Method)
  110. }
  111. err = db.Find(&apiList).Count(&total).Error
  112. if err != nil {
  113. return err, apiList, total
  114. } else {
  115. err = db.Limit(limit).Offset(offset).Order("api_group", true).Find(&apiList).Error
  116. }
  117. return err, apiList, total
  118. }
  119. }