完善 RequestHandler 逻辑
This commit is contained in:
parent
196c437cc5
commit
77ea723e86
@ -94,6 +94,7 @@ func (c controller) methodConfig(methodType reflect.Type) (cfg UriConfig, needRe
|
||||
needRegister = false
|
||||
return
|
||||
}
|
||||
|
||||
// 解析第一个返回值, 要求必须是结构体或者是map
|
||||
if methodType.Out(0).Kind() != reflect.Struct && methodType.Out(0).Kind() != reflect.Map {
|
||||
needRegister = false
|
||||
@ -121,5 +122,29 @@ func (c controller) methodConfig(methodType reflect.Type) (cfg UriConfig, needRe
|
||||
cfg.Method = metaField.Tag.Get(TagNameMethod)
|
||||
cfg.Desc = metaField.Tag.Get(TagNameDesc)
|
||||
cfg.TagList = strings.Split(metaField.Tag.Get(TagNameUriTag), ",")
|
||||
// 解析参数配置
|
||||
cfg.ParamList = c.parseParamConfig(formType)
|
||||
return
|
||||
}
|
||||
|
||||
// parseParamConfig 解析参数配置
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 14:35 2025/2/7
|
||||
func (c controller) parseParamConfig(formDataType reflect.Type) []UriParam {
|
||||
res := make([]UriParam, 0)
|
||||
for i := 0; i < formDataType.NumField(); i++ {
|
||||
structField := formDataType.Field(i)
|
||||
if structField.Name == FieldNameMeta {
|
||||
// Meta 字段, 忽略
|
||||
continue
|
||||
}
|
||||
jsonTag := structField.Tag.Get("json")
|
||||
if jsonTag == "" {
|
||||
jsonTag = structField.Name
|
||||
}
|
||||
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
@ -20,6 +20,9 @@ const (
|
||||
TagNameUriTag = "tag" // 接口的tag
|
||||
TagNameDesc = "desc" // 接口的描述
|
||||
TagNameStrict = "strict" // 接口是否为严格模式 : 不配置, 则为严格模式.严格模式 : POST 仅解析 BODY , GET 仅解析 QUERY
|
||||
TagNameBinding = "binding" // gin 内置的验证规则tag
|
||||
TagNameValidate = "validate" // validator v10 默认的验证规则tag
|
||||
TagNameErrMsg = "err" // 验证失败错误信息tag
|
||||
)
|
||||
|
||||
// UriConfig 接口配置
|
||||
@ -33,8 +36,9 @@ type UriConfig struct {
|
||||
TagList []string `json:"tag_list"` // 接口分组
|
||||
Desc string `json:"desc"` // 接口描述
|
||||
Strict bool `json:"strict"` // 接口是否为严格模式 : 不配置, 则为严格模式.严格模式 : POST 仅解析 BODY , GET 仅解析 QUERY
|
||||
ParamTable map[string]UriParam `json:"param_table"` // 参数信息表
|
||||
ParamList []UriParam `json:"param_list"` // 参数信息表
|
||||
FormDataType reflect.Type `json:"-"` // 表单数据类型
|
||||
ApiLogicFunc reflect.Method `json:"-"` // 自定义的接口逻辑
|
||||
}
|
||||
|
||||
// UriParam 接口参数配置
|
||||
@ -43,8 +47,11 @@ type UriConfig struct {
|
||||
//
|
||||
// Date : 15:40 2025/1/27
|
||||
type UriParam struct {
|
||||
Field string `json:"field"` // 结构体字段
|
||||
Name string `json:"name"` // 参数名称
|
||||
Type string `json:"type"` // 参数类型
|
||||
Validate string `json:"validate"` // 验证规则: validator/v10 库
|
||||
ErrorMsg string `json:"error_msg"` // 验证失败的错误信息
|
||||
DisableAutoType bool `json:"disable_auto_type"` // 禁用自动类型转换
|
||||
Sort string `json:"sort"` // 参数读取顺序: 默认 POST : body > query > path GET : query > path > body
|
||||
}
|
||||
|
@ -8,6 +8,7 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"strings"
|
||||
@ -28,8 +29,21 @@ func Group(router *gin.Engine, routerPrefix string, middlewareList []gin.Handler
|
||||
method := strings.ToUpper(itemUriCfg.Method)
|
||||
switch method {
|
||||
case http.MethodGet:
|
||||
g.HEAD(itemUriCfg.Path)
|
||||
|
||||
g.GET(itemUriCfg.Path, RequestHandler(itemUriCfg))
|
||||
case http.MethodHead:
|
||||
g.HEAD(itemUriCfg.Path, RequestHandler(itemUriCfg))
|
||||
case http.MethodPost:
|
||||
g.POST(itemUriCfg.Path, RequestHandler(itemUriCfg))
|
||||
case http.MethodPut:
|
||||
g.PUT(itemUriCfg.Path, RequestHandler(itemUriCfg))
|
||||
case http.MethodPatch:
|
||||
g.PATCH(itemUriCfg.Path, RequestHandler(itemUriCfg))
|
||||
case http.MethodDelete:
|
||||
g.DELETE(itemUriCfg.Path, RequestHandler(itemUriCfg))
|
||||
case http.MethodOptions:
|
||||
g.OPTIONS(itemUriCfg.Path, RequestHandler(itemUriCfg))
|
||||
case http.MethodTrace:
|
||||
return errors.New(`method Trace is not supported`)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -7,7 +7,13 @@
|
||||
// Date : 2025-01-27 19:42
|
||||
package router
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
import (
|
||||
"git.zhangdeman.cn/zhangdeman/exception"
|
||||
"git.zhangdeman.cn/zhangdeman/gin/request"
|
||||
"git.zhangdeman.cn/zhangdeman/gin/response"
|
||||
"github.com/gin-gonic/gin"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// RequestHandler 获取请求处理方法 TODO : 待完成
|
||||
//
|
||||
@ -16,6 +22,38 @@ import "github.com/gin-gonic/gin"
|
||||
// Date : 19:44 2025/1/27
|
||||
func RequestHandler(uriCfg UriConfig) gin.HandlerFunc {
|
||||
return func(ctx *gin.Context) {
|
||||
var (
|
||||
err error
|
||||
ok bool
|
||||
e exception.IException
|
||||
)
|
||||
|
||||
formData := reflect.New(uriCfg.FormDataType).Interface()
|
||||
// 表单解析
|
||||
if err = request.Form.Parse(ctx, formData); nil != err {
|
||||
return
|
||||
}
|
||||
// 执行逻辑
|
||||
resList := uriCfg.ApiLogicFunc.Func.Call([]reflect.Value{reflect.ValueOf(ctx), reflect.ValueOf(formData)})
|
||||
if resList[1].IsNil() {
|
||||
// 请求成功
|
||||
response.Success(ctx, resList[0].Interface())
|
||||
return
|
||||
}
|
||||
// 请求失败
|
||||
if e, ok = resList[1].Interface().(exception.IException); ok {
|
||||
response.SendWithException(ctx, e, nil)
|
||||
return
|
||||
}
|
||||
if err, ok = resList[1].Interface().(error); ok {
|
||||
e = exception.NewFromError(-1, err)
|
||||
response.SendWithException(ctx, e, nil)
|
||||
return
|
||||
}
|
||||
e = exception.NewWithCodeAndData(-1, map[string]any{
|
||||
"err": resList[1].Interface(),
|
||||
})
|
||||
response.SendWithException(ctx, e, nil)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user