feat: 自动字段注入支持解析二级匿名字段

This commit is contained in:
2025-10-31 14:51:00 +08:00
parent 463523f0b8
commit 2deff38f9d
4 changed files with 72 additions and 23 deletions

View File

@ -46,10 +46,11 @@ func (s *server) getFormInitValue(ctx *gin.Context, uriCfg UriConfig) (any, erro
func (s *server) RequestHandler(uriCfg UriConfig) gin.HandlerFunc {
return func(ctx *gin.Context) {
var (
err error
ok bool
e exception.IException
formValue any
err error
ok bool
e exception.IException
formValue any
firstParam reflect.Value
)
if formValue, err = s.getFormInitValue(ctx, uriCfg); nil != err {
@ -60,6 +61,17 @@ func (s *server) RequestHandler(uriCfg UriConfig) gin.HandlerFunc {
ctx.Abort()
return
}
// 表单数据
inputValue := reflect.ValueOf(formValue)
// 注入公共参数
if err = s.injectCommonParam(ctx, inputValue); nil != err {
e = exception.NewFromError(500, err)
response.SendWithException(ctx, e, &define.ResponseOption{
ContentType: consts.MimeTypeJson,
})
ctx.Abort()
return
}
isSuccess := false
// 初始化响应之后logic
@ -94,11 +106,9 @@ func (s *server) RequestHandler(uriCfg UriConfig) gin.HandlerFunc {
}()
}()
// 执行逻辑
inputValue := reflect.ValueOf(formValue)
if uriCfg.FormDataType.Kind() != reflect.Ptr {
inputValue = inputValue.Elem()
}
var firstParam reflect.Value
if uriCfg.CtxType == CustomContextType {
customCtx := ctx.MustGet(define.CustomContextKey)
firstParam = reflect.ValueOf(customCtx)
@ -109,7 +119,7 @@ func (s *server) RequestHandler(uriCfg UriConfig) gin.HandlerFunc {
if resList[1].IsNil() {
// 请求成功
isSuccess = true
response.SuccessWithExtension(ctx, resList[0].Interface(), &define.ResponseOption{ContentType: "application/json;charset=utf-8"})
response.SuccessWithExtension(ctx, resList[0].Interface(), &define.ResponseOption{ContentType: consts.MimeTypeJson})
return
}
// 请求失败
@ -123,7 +133,7 @@ func (s *server) RequestHandler(uriCfg UriConfig) gin.HandlerFunc {
})
}
response.SendWithException(ctx, e, &define.ResponseOption{
ContentType: "application/json;charset=utf-8",
ContentType: consts.MimeTypeJson,
})
return
}