api.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package apis
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/gin-gonic/gin"
  6. "github.com/gin-gonic/gin/binding"
  7. "github.com/go-admin-team/go-admin-core/logger"
  8. "github.com/go-admin-team/go-admin-core/sdk/api"
  9. "github.com/go-admin-team/go-admin-core/sdk/pkg"
  10. "github.com/go-admin-team/go-admin-core/sdk/pkg/response"
  11. "github.com/go-admin-team/go-admin-core/sdk/service"
  12. "gorm.io/gorm"
  13. // "go-admin/common/service"
  14. )
  15. type Api struct {
  16. Context *gin.Context
  17. Logger *logger.Helper
  18. Orm *gorm.DB
  19. Errors error
  20. }
  21. func (e *Api) AddError(err error) {
  22. if e.Errors == nil {
  23. e.Errors = err
  24. } else if err != nil {
  25. e.Logger.Error(err)
  26. e.Errors = fmt.Errorf("%v; %w", e.Error, err)
  27. }
  28. }
  29. // MakeContext 设置http上下文
  30. func (e *Api) MakeContext(c *gin.Context) *Api {
  31. e.Context = c
  32. e.Logger = api.GetRequestLogger(c)
  33. return e
  34. }
  35. // GetLogger 获取上下文提供的日志
  36. func (e Api) GetLogger() *logger.Helper {
  37. return api.GetRequestLogger(e.Context)
  38. }
  39. func (e *Api) Bind(d interface{}, bindings ...binding.Binding) *Api {
  40. var err error
  41. if len(bindings) == 0 {
  42. bindings = append(bindings, binding.JSON, nil)
  43. }
  44. for i := range bindings {
  45. switch bindings[i] {
  46. case binding.JSON:
  47. err = e.Context.ShouldBindWith(d, binding.JSON)
  48. case binding.XML:
  49. err = e.Context.ShouldBindWith(d, binding.XML)
  50. case binding.Form:
  51. err = e.Context.ShouldBindWith(d, binding.Form)
  52. case binding.Query:
  53. err = e.Context.ShouldBindWith(d, binding.Query)
  54. case binding.FormPost:
  55. err = e.Context.ShouldBindWith(d, binding.FormPost)
  56. case binding.FormMultipart:
  57. err = e.Context.ShouldBindWith(d, binding.FormMultipart)
  58. case binding.ProtoBuf:
  59. err = e.Context.ShouldBindWith(d, binding.ProtoBuf)
  60. case binding.MsgPack:
  61. err = e.Context.ShouldBindWith(d, binding.MsgPack)
  62. case binding.YAML:
  63. err = e.Context.ShouldBindWith(d, binding.YAML)
  64. case binding.Header:
  65. err = e.Context.ShouldBindWith(d, binding.Header)
  66. default:
  67. err = e.Context.ShouldBindUri(d)
  68. }
  69. if err != nil {
  70. e.AddError(err)
  71. }
  72. }
  73. return e
  74. }
  75. // GetOrm 获取Orm DB
  76. func (e Api) GetOrm() (*gorm.DB, error) {
  77. db, err := pkg.GetOrm(e.Context)
  78. if err != nil {
  79. e.Error(500, err, "数据库连接获取失败")
  80. return nil, err
  81. }
  82. return db, nil
  83. }
  84. // MakeOrm 设置Orm DB
  85. func (e *Api) MakeOrm() *Api {
  86. var err error
  87. if e.Logger == nil {
  88. err = errors.New("at MakeOrm logger is nil")
  89. //e.Logger.Error(500, err, "at MakeOrm logger is nil")
  90. e.AddError(err)
  91. return e
  92. }
  93. db, err := pkg.GetOrm(e.Context)
  94. if err != nil {
  95. e.Logger.Error(500, err, "数据库连接获取失败")
  96. e.AddError(err)
  97. }
  98. e.Orm = db
  99. return e
  100. }
  101. func (e *Api) MakeService(c *service.Service) *Api {
  102. c.Log = e.Logger
  103. c.Orm = e.Orm
  104. return e
  105. }
  106. // Error 通常错误数据处理
  107. func (e Api) Error(code int, err error, msg string) {
  108. response.Error(e.Context, code, err, msg)
  109. }
  110. // OK 通常成功数据处理
  111. func (e Api) OK(data interface{}, msg string) {
  112. response.OK(e.Context, data, msg)
  113. }
  114. // PageOK 分页数据处理
  115. func (e Api) PageOK(result interface{}, count int, pageIndex int, pageSize int, msg string) {
  116. response.PageOK(e.Context, result, count, pageIndex, pageSize, msg)
  117. }
  118. // Custom 兼容函数
  119. func (e Api) Custom(data gin.H) {
  120. response.Custum(e.Context, data)
  121. }