sys_api.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package models
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "regexp"
  8. "strings"
  9. "github.com/bitly/go-simplejson"
  10. "github.com/go-admin-team/go-admin-core/sdk"
  11. "github.com/go-admin-team/go-admin-core/sdk/runtime"
  12. "github.com/go-admin-team/go-admin-core/storage"
  13. "go-admin/common/models"
  14. )
  15. type SysApi struct {
  16. Id int `json:"id" gorm:"primaryKey;autoIncrement;comment:主键编码"`
  17. Handle string `json:"handle" gorm:"size:128;comment:handle"`
  18. Title string `json:"title" gorm:"size:128;comment:标题"`
  19. Path string `json:"path" gorm:"size:128;comment:地址"`
  20. Action string `json:"action" gorm:"size:16;comment:请求类型"`
  21. Type string `json:"type" gorm:"size:16;comment:接口类型"`
  22. models.ModelTime
  23. models.ControlBy
  24. }
  25. func (SysApi) TableName() string {
  26. return "sys_api"
  27. }
  28. func (e *SysApi) Generate() models.ActiveRecord {
  29. o := *e
  30. return &o
  31. }
  32. func (e *SysApi) GetId() interface{} {
  33. return e.Id
  34. }
  35. func SaveSysApi(message storage.Messager) (err error) {
  36. var rb []byte
  37. rb, err = json.Marshal(message.GetValues())
  38. if err != nil {
  39. fmt.Errorf("json Marshal error, %s", err.Error())
  40. return err
  41. }
  42. var l runtime.Routers
  43. err = json.Unmarshal(rb, &l)
  44. if err != nil {
  45. fmt.Errorf("json Unmarshal error, %s", err.Error())
  46. return err
  47. }
  48. dbList := sdk.Runtime.GetDb()
  49. for _, d := range dbList {
  50. for _, v := range l.List {
  51. if v.HttpMethod != "HEAD" ||
  52. strings.Contains(v.RelativePath, "/swagger/") ||
  53. strings.Contains(v.RelativePath, "/static/") ||
  54. strings.Contains(v.RelativePath, "/form-generator/") ||
  55. strings.Contains(v.RelativePath, "/sys/tables") {
  56. // 根据接口方法注释里的@Summary填充接口名称,适用于代码生成器
  57. // 可在此处增加配置路径前缀的if判断,只对代码生成的自建应用进行定向的接口名称填充
  58. jsonFile, _ := ioutil.ReadFile("docs/swagger.json")
  59. jsonData, _ := simplejson.NewFromReader(bytes.NewReader(jsonFile))
  60. urlPath := v.RelativePath
  61. idPatten := "(.*)/:(\\w+)" // 正则替换,把:id换成{id}
  62. reg, _ := regexp.Compile(idPatten)
  63. if reg.MatchString(urlPath) {
  64. urlPath = reg.ReplaceAllString(v.RelativePath, "${1}/{${2}}") // 把:id换成{id}
  65. }
  66. apiTitle, _ := jsonData.Get("paths").Get(urlPath).Get(strings.ToLower(v.HttpMethod)).Get("summary").String()
  67. err := d.Debug().Where(SysApi{Path: v.RelativePath, Action: v.HttpMethod}).
  68. Attrs(SysApi{Handle: v.Handler, Title: apiTitle}).
  69. FirstOrCreate(&SysApi{}).
  70. //Update("handle", v.Handler).
  71. Error
  72. if err != nil {
  73. err := fmt.Errorf("Models SaveSysApi error: %s \r\n ", err.Error())
  74. return err
  75. }
  76. }
  77. }
  78. }
  79. return nil
  80. }