|
|
@@ -52,108 +52,103 @@ func (item *BindItem[O]) bind(binder *Binder, middlewares ...Middleware) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // 给单个路由增加中间件
|
|
|
- handlers := []api.Handler{
|
|
|
- func(c *api.Context) {
|
|
|
- var params request.Params
|
|
|
-
|
|
|
- // 有请求数据
|
|
|
- if item.RequestParams != nil {
|
|
|
- requestParamsType := reflect.TypeOf(item.RequestParams)
|
|
|
- if !reflectutils.IsTypeStructOrStructPointer(requestParamsType) {
|
|
|
- item.SendResponseFunc(c, http.StatusOK, outputZero, errors.New("请求参数不是结构或结构指针"))
|
|
|
- return
|
|
|
- }
|
|
|
+ apiMiddlewares := make([]api.Handler, len(middlewares))
|
|
|
+ for i, middleware := range middlewares {
|
|
|
+ innerMiddleware := middleware
|
|
|
+ apiMiddlewares[i] = func(c *api.Context) {
|
|
|
+ innerMiddleware(c, binder.i)
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- if requestParamsType.Kind() == reflect.Pointer {
|
|
|
- params = reflect.New(requestParamsType.Elem()).Interface().(request.Params)
|
|
|
- } else {
|
|
|
- params = reflect.New(requestParamsType).Interface().(request.Params)
|
|
|
- }
|
|
|
+ handlers := append(apiMiddlewares, func(c *api.Context) {
|
|
|
+ var params request.Params
|
|
|
|
|
|
- // 将请求数据解析到请求参数中
|
|
|
- if item.BindRequestParamsFunc != nil {
|
|
|
- err := item.BindRequestParamsFunc(c, params)
|
|
|
+ // 有请求数据
|
|
|
+ if item.RequestParams != nil {
|
|
|
+ requestParamsType := reflect.TypeOf(item.RequestParams)
|
|
|
+ if !reflectutils.IsTypeStructOrStructPointer(requestParamsType) {
|
|
|
+ item.SendResponseFunc(c, http.StatusOK, outputZero, errors.New("请求参数不是结构或结构指针"))
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ if requestParamsType.Kind() == reflect.Pointer {
|
|
|
+ params = reflect.New(requestParamsType.Elem()).Interface().(request.Params)
|
|
|
+ } else {
|
|
|
+ params = reflect.New(requestParamsType).Interface().(request.Params)
|
|
|
+ }
|
|
|
+
|
|
|
+ // 将请求数据解析到请求参数中
|
|
|
+ if item.BindRequestParamsFunc != nil {
|
|
|
+ err := item.BindRequestParamsFunc(c, params)
|
|
|
+ if err != nil {
|
|
|
+ item.SendResponseFunc(c, http.StatusBadRequest, outputZero, err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ switch item.Method {
|
|
|
+ case http.MethodPost:
|
|
|
+ fallthrough
|
|
|
+ case http.MethodPut:
|
|
|
+ err := request.BindJsonBody(c, params)
|
|
|
if err != nil {
|
|
|
item.SendResponseFunc(c, http.StatusBadRequest, outputZero, err)
|
|
|
return
|
|
|
}
|
|
|
- } else {
|
|
|
- switch item.Method {
|
|
|
- case http.MethodPost:
|
|
|
- fallthrough
|
|
|
- case http.MethodPut:
|
|
|
- err := request.BindJsonBody(c, params)
|
|
|
- if err != nil {
|
|
|
- item.SendResponseFunc(c, http.StatusBadRequest, outputZero, err)
|
|
|
- return
|
|
|
- }
|
|
|
- case http.MethodDelete:
|
|
|
- fallthrough
|
|
|
- case http.MethodGet:
|
|
|
- err := request.BindQueryParams(c, params)
|
|
|
- if err != nil {
|
|
|
- item.SendResponseFunc(c, http.StatusBadRequest, outputZero, err)
|
|
|
- return
|
|
|
- }
|
|
|
+ case http.MethodDelete:
|
|
|
+ fallthrough
|
|
|
+ case http.MethodGet:
|
|
|
+ err := request.BindQueryParams(c, params)
|
|
|
+ if err != nil {
|
|
|
+ item.SendResponseFunc(c, http.StatusBadRequest, outputZero, err)
|
|
|
+ return
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- // 进行领域对象转化
|
|
|
- var domainObjects []domain.Object
|
|
|
- if item.FormDomainObjectsFunc != nil {
|
|
|
- innerDomainObjects, err := item.FormDomainObjectsFunc(c, params)
|
|
|
- if err != nil {
|
|
|
- item.SendResponseFunc(c, http.StatusOK, outputZero, err)
|
|
|
- return
|
|
|
- }
|
|
|
+ // 进行领域对象转化
|
|
|
+ var domainObjects []domain.Object
|
|
|
+ if item.FormDomainObjectsFunc != nil {
|
|
|
+ innerDomainObjects, err := item.FormDomainObjectsFunc(c, params)
|
|
|
+ if err != nil {
|
|
|
+ item.SendResponseFunc(c, http.StatusOK, outputZero, err)
|
|
|
+ return
|
|
|
+ }
|
|
|
|
|
|
- domainObjects = innerDomainObjects
|
|
|
- } else {
|
|
|
- if item.Objects != nil && len(item.Objects) != 0 {
|
|
|
- for _, object := range item.Objects {
|
|
|
- if object == nil {
|
|
|
- continue
|
|
|
- }
|
|
|
+ domainObjects = innerDomainObjects
|
|
|
+ } else {
|
|
|
+ if item.Objects != nil && len(item.Objects) != 0 {
|
|
|
+ for _, object := range item.Objects {
|
|
|
+ if object == nil {
|
|
|
+ continue
|
|
|
+ }
|
|
|
|
|
|
- objectType := reflect.TypeOf(object)
|
|
|
- if !reflectutils.IsTypeStructOrStructPointer(objectType) {
|
|
|
- item.SendResponseFunc(c, http.StatusOK, outputZero, errors.New("领域对象不是结构或结构指针"))
|
|
|
- return
|
|
|
- }
|
|
|
+ objectType := reflect.TypeOf(object)
|
|
|
+ if !reflectutils.IsTypeStructOrStructPointer(objectType) {
|
|
|
+ item.SendResponseFunc(c, http.StatusOK, outputZero, errors.New("领域对象不是结构或结构指针"))
|
|
|
+ return
|
|
|
+ }
|
|
|
|
|
|
- obj := reflect.New(reflectutils.PointerTypeElem(objectType)).Interface().(domain.Object)
|
|
|
+ obj := reflect.New(reflectutils.PointerTypeElem(objectType)).Interface().(domain.Object)
|
|
|
|
|
|
- if params != nil {
|
|
|
- err := request.AssignRequestParamsToDomainObject(params, obj)
|
|
|
- if err != nil {
|
|
|
- item.SendResponseFunc(c, http.StatusOK, outputZero, err)
|
|
|
- return
|
|
|
- }
|
|
|
+ if params != nil {
|
|
|
+ err := request.AssignRequestParamsToDomainObject(params, obj)
|
|
|
+ if err != nil {
|
|
|
+ item.SendResponseFunc(c, http.StatusOK, outputZero, err)
|
|
|
+ return
|
|
|
}
|
|
|
-
|
|
|
- domainObjects = append(domainObjects, obj)
|
|
|
}
|
|
|
+
|
|
|
+ domainObjects = append(domainObjects, obj)
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- // 执行业务函数
|
|
|
- outputModel, err := item.ServiceFunc(c, params, domainObjects, binder.i)
|
|
|
- item.SendResponseFunc(c, http.StatusOK, outputModel, err)
|
|
|
- return
|
|
|
- },
|
|
|
- }
|
|
|
-
|
|
|
- apiMiddlewares := make([]api.Handler, len(middlewares))
|
|
|
- for i, middleware := range middlewares {
|
|
|
- innerMiddleware := middleware
|
|
|
- apiMiddlewares[i] = func(c *api.Context) {
|
|
|
- innerMiddleware(c, binder.i)
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- handlers = append(handlers, apiMiddlewares...)
|
|
|
+ // 执行业务函数
|
|
|
+ outputModel, err := item.ServiceFunc(c, params, domainObjects, binder.i)
|
|
|
+ item.SendResponseFunc(c, http.StatusOK, outputModel, err)
|
|
|
+ return
|
|
|
+ })
|
|
|
|
|
|
// 所有的函数加入到执行链中
|
|
|
binder.router.AddRoute(item.Method, item.Path, handlers...)
|