sys_api.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package dao
  2. import (
  3. "fmt"
  4. "server/global"
  5. )
  6. type SysApi struct {
  7. global.GVA_MODEL
  8. Path string `json:"path" gorm:"comment:api路径"` // api路径
  9. Description string `json:"description" gorm:"comment:api中文描述"` // api中文描述
  10. ApiGroup string `json:"apiGroup" gorm:"comment:api组"` // api组
  11. Method string `json:"method" gorm:"default:POST;comment:方法"` // 方法:创建POST(默认)|查看GET|更新PUT|删除DELETE
  12. }
  13. func (SysApi) TableName() string {
  14. return "sys_apis"
  15. }
  16. // TODO:Api查询
  17. // QueryApiByPathMethod 查询api 按路径和方法
  18. func QueryApiByPathMethod(path, method string) (sysApi SysApi, err error) {
  19. err = global.GVA_DB.Where("path = ? AND method = ?", path, method).First(&sysApi).Error
  20. return
  21. }
  22. // QueryApiById 查询api 按id
  23. func QueryApiById(id uint) (sysApi SysApi, err error) {
  24. err = global.GVA_DB.First(&sysApi, "id = ?", id).Error
  25. return
  26. }
  27. //@author: [piexlmax](https://github.com/piexlmax)
  28. //@function: GetAPIInfoList
  29. //@description: 分页获取数据,
  30. //@param: api model.SysApi, info request.PageInfo, order string, desc bool
  31. //@return: list interface{}, total int64, err error
  32. func (api SysApi) GetAPIInfoList(limit, offset int, order string, desc bool) (apiList []SysApi, total int64, err error) {
  33. db := global.GVA_DB.Model(&SysApi{})
  34. if api.Path != "" {
  35. db = db.Where("path LIKE ?", "%"+api.Path+"%")
  36. }
  37. if api.Description != "" {
  38. db = db.Where("description LIKE ?", "%"+api.Description+"%")
  39. }
  40. if api.Method != "" {
  41. db = db.Where("method = ?", api.Method)
  42. }
  43. if api.ApiGroup != "" {
  44. db = db.Where("api_group = ?", api.ApiGroup)
  45. }
  46. err = db.Count(&total).Error
  47. if err != nil {
  48. return apiList, total, err
  49. }
  50. db = db.Limit(limit).Offset(offset)
  51. OrderStr := "id desc"
  52. if order != "" {
  53. orderMap := make(map[string]bool, 5)
  54. orderMap["id"] = true
  55. orderMap["path"] = true
  56. orderMap["api_group"] = true
  57. orderMap["description"] = true
  58. orderMap["method"] = true
  59. if !orderMap[order] {
  60. err = fmt.Errorf("非法的排序字段: %v", order)
  61. return apiList, total, err
  62. }
  63. OrderStr = order
  64. if desc {
  65. OrderStr = order + " desc"
  66. }
  67. }
  68. err = db.Order(OrderStr).Find(&apiList).Error
  69. return apiList, total, err
  70. }
  71. // GetAllApis 查询所有api
  72. func GetAllApis() (apis []SysApi, err error) {
  73. err = global.GVA_DB.Find(&apis).Error
  74. return
  75. }
  76. // TODO:Api新增
  77. // CreateApi 新增api
  78. func (api SysApi) CreateApi() error {
  79. return global.GVA_DB.Create(&api).Error
  80. }
  81. // TODO:Api修改
  82. // SaveApi 更新api
  83. func (api SysApi) SaveApi() error {
  84. return global.GVA_DB.Save(&api).Error
  85. }
  86. // TODO:Api删除
  87. // DeleteApi 删除api
  88. func (api SysApi) DeleteApi() error {
  89. return global.GVA_DB.Delete(&api).Error
  90. }