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