api.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package api
  2. import (
  3. "errors"
  4. "fmt"
  5. "net/http"
  6. vd "github.com/bytedance/go-tagexpr/v2/validator"
  7. "github.com/gin-gonic/gin"
  8. "github.com/gin-gonic/gin/binding"
  9. "gorm.io/gorm"
  10. "gogs.baozhida.cn/zoie/OAuth-core/logger"
  11. "gogs.baozhida.cn/zoie/OAuth-core/pkg"
  12. "gogs.baozhida.cn/zoie/OAuth-core/pkg/response"
  13. "gogs.baozhida.cn/zoie/OAuth-core/sdk"
  14. "gogs.baozhida.cn/zoie/OAuth-core/service"
  15. "gogs.baozhida.cn/zoie/OAuth-core/storage"
  16. "gogs.baozhida.cn/zoie/OAuth-core/tools/language"
  17. )
  18. var DefaultLanguage = "zh-CN"
  19. type Api struct {
  20. Context *gin.Context
  21. Logger *logger.Helper
  22. Orm *gorm.DB
  23. Errors error
  24. Cache storage.AdapterCache
  25. }
  26. func (e *Api) AddError(err error) {
  27. if e.Errors == nil {
  28. e.Errors = err
  29. } else if err != nil {
  30. e.Logger.Error(err)
  31. e.Errors = fmt.Errorf("%v; %w", e.Errors, err)
  32. }
  33. }
  34. // MakeContext 设置http上下文
  35. func (e *Api) MakeContext(c *gin.Context) *Api {
  36. e.Context = c
  37. e.Logger = GetRequestLogger(c)
  38. return e
  39. }
  40. // GetLogger 获取上下文提供的日志
  41. func (e Api) GetLogger() *logger.Helper {
  42. return GetRequestLogger(e.Context)
  43. }
  44. // Bind 参数校验
  45. func (e *Api) Bind(d interface{}, bindings ...binding.Binding) *Api {
  46. var err error
  47. if len(bindings) == 0 {
  48. bindings = constructor.GetBindingForGin(d)
  49. }
  50. for i := range bindings {
  51. if bindings[i] == nil {
  52. err = e.Context.ShouldBindUri(d)
  53. } else {
  54. err = e.Context.ShouldBindWith(d, bindings[i])
  55. }
  56. if err != nil && err.Error() == "EOF" {
  57. e.Logger.Warn("request body is not present anymore. ")
  58. err = nil
  59. continue
  60. }
  61. if err != nil {
  62. e.AddError(err)
  63. break
  64. }
  65. }
  66. //vd.SetErrorFactory(func(failPath, msg string) error {
  67. // return fmt.Errorf(`"validation failed: %s %s"`, failPath, msg)
  68. //})
  69. if err1 := vd.Validate(d); err1 != nil {
  70. e.AddError(err1)
  71. }
  72. return e
  73. }
  74. // GetOrm 获取Orm DB
  75. func (e Api) GetOrm() (*gorm.DB, error) {
  76. ormDB, err := pkg.GetOrm(e.Context)
  77. if err != nil {
  78. e.Logger.Error(http.StatusInternalServerError, err, "数据库连接获取失败")
  79. return nil, err
  80. }
  81. return ormDB, 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.AddError(err)
  89. return e
  90. }
  91. ormDB, err := pkg.GetOrm(e.Context)
  92. if err != nil {
  93. e.Logger.Error(http.StatusInternalServerError, err, "数据库连接获取失败")
  94. e.AddError(err)
  95. }
  96. e.Orm = ormDB
  97. return e
  98. }
  99. func (e *Api) MakeService(c *service.Service) *Api {
  100. c.Log = e.Logger
  101. c.Orm = e.Orm
  102. c.Cache = sdk.Runtime.GetCacheAdapter()
  103. e.Cache = c.Cache
  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. }
  122. func (e Api) Translate(form, to interface{}) {
  123. pkg.Translate(form, to)
  124. }
  125. // getAcceptLanguage 获取当前语言
  126. func (e *Api) getAcceptLanguage() string {
  127. languages := language.ParseAcceptLanguage(e.Context.GetHeader("Accept-Language"), nil)
  128. if len(languages) == 0 {
  129. return DefaultLanguage
  130. }
  131. return languages[0]
  132. }