bind_item.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package binding
  2. import (
  3. "git.sxidc.com/go-framework/baize/framework/core/api"
  4. "git.sxidc.com/go-framework/baize/framework/core/api/request"
  5. "git.sxidc.com/go-framework/baize/framework/core/domain"
  6. "git.sxidc.com/go-tools/utils/reflectutils"
  7. "git.sxidc.com/go-tools/utils/strutils"
  8. "github.com/pkg/errors"
  9. "net/http"
  10. "reflect"
  11. "strings"
  12. )
  13. func Bind[O any](binder *Binder, item *BindItem[O], middlewares ...Middleware) {
  14. item.bind(binder, middlewares...)
  15. }
  16. // BindItem 通用BindItem
  17. type BindItem[O any] struct {
  18. Method string
  19. *SimpleBindItem[O]
  20. }
  21. func (item *BindItem[O]) bind(binder *Binder, middlewares ...Middleware) {
  22. if strutils.IsStringEmpty(item.Path) {
  23. panic("需要指定路径")
  24. }
  25. if strutils.IsStringEmpty(item.Method) {
  26. panic("需要指定方法")
  27. }
  28. if item.SendResponseFunc == nil {
  29. panic("需要指定响应函数")
  30. }
  31. if item.ServiceFunc == nil {
  32. panic("需要指定应用服务函数")
  33. }
  34. var outputZero O
  35. outputZeroValue := reflect.ValueOf(outputZero)
  36. if outputZeroValue.IsValid() && outputZeroValue.Kind() == reflect.Pointer {
  37. panic("bind的输出类型不能使用指针类型")
  38. }
  39. if outputZeroValue.IsValid() && strings.Contains(outputZeroValue.String(), "response.InfosData") {
  40. infosField := outputZeroValue.FieldByName("Infos")
  41. if infosField.IsValid() && infosField.Type().Elem().Kind() == reflect.Pointer {
  42. panic("bind的输出类型不能使用指针类型")
  43. }
  44. }
  45. apiMiddlewares := make([]api.Handler, len(middlewares))
  46. for i, middleware := range middlewares {
  47. innerMiddleware := middleware
  48. apiMiddlewares[i] = func(c *api.Context) {
  49. innerMiddleware(c, binder.i)
  50. }
  51. }
  52. handlers := append(apiMiddlewares, func(c *api.Context) {
  53. var params request.Params
  54. // 有请求数据
  55. if item.RequestParams != nil {
  56. requestParamsType := reflect.TypeOf(item.RequestParams)
  57. if !reflectutils.IsTypeStructOrStructPointer(requestParamsType) {
  58. item.SendResponseFunc(c, http.StatusOK, outputZero, errors.New("请求参数不是结构或结构指针"))
  59. return
  60. }
  61. if requestParamsType.Kind() == reflect.Pointer {
  62. params = reflect.New(requestParamsType.Elem()).Interface().(request.Params)
  63. } else {
  64. params = reflect.New(requestParamsType).Interface().(request.Params)
  65. }
  66. // 将请求数据解析到请求参数中
  67. if item.BindRequestParamsFunc != nil {
  68. err := item.BindRequestParamsFunc(c, params)
  69. if err != nil {
  70. item.SendResponseFunc(c, http.StatusBadRequest, outputZero, err)
  71. return
  72. }
  73. } else {
  74. switch item.Method {
  75. case http.MethodPost:
  76. fallthrough
  77. case http.MethodPut:
  78. err := request.BindJsonBody(c, params)
  79. if err != nil {
  80. item.SendResponseFunc(c, http.StatusBadRequest, outputZero, err)
  81. return
  82. }
  83. case http.MethodDelete:
  84. fallthrough
  85. case http.MethodGet:
  86. err := request.BindQueryParams(c, params)
  87. if err != nil {
  88. item.SendResponseFunc(c, http.StatusBadRequest, outputZero, err)
  89. return
  90. }
  91. }
  92. }
  93. }
  94. // 进行领域对象转化
  95. var domainObjects []domain.Object
  96. if item.FormDomainObjectsFunc != nil {
  97. innerDomainObjects, err := item.FormDomainObjectsFunc(c, params)
  98. if err != nil {
  99. item.SendResponseFunc(c, http.StatusOK, outputZero, err)
  100. return
  101. }
  102. domainObjects = innerDomainObjects
  103. } else {
  104. if item.Objects != nil && len(item.Objects) != 0 {
  105. for _, object := range item.Objects {
  106. if object == nil {
  107. continue
  108. }
  109. objectType := reflect.TypeOf(object)
  110. if !reflectutils.IsTypeStructOrStructPointer(objectType) {
  111. item.SendResponseFunc(c, http.StatusOK, outputZero, errors.New("领域对象不是结构或结构指针"))
  112. return
  113. }
  114. obj := reflect.New(reflectutils.PointerTypeElem(objectType)).Interface().(domain.Object)
  115. if params != nil {
  116. err := request.AssignRequestParamsToDomainObject(params, obj)
  117. if err != nil {
  118. item.SendResponseFunc(c, http.StatusOK, outputZero, err)
  119. return
  120. }
  121. }
  122. domainObjects = append(domainObjects, obj)
  123. }
  124. }
  125. }
  126. // 执行业务函数
  127. outputModel, err := item.ServiceFunc(c, params, domainObjects, binder.i)
  128. item.SendResponseFunc(c, http.StatusOK, outputModel, err)
  129. return
  130. })
  131. // 所有的函数加入到执行链中
  132. binder.router.AddRoute(item.Method, item.Path, handlers...)
  133. }