From f55dee577d016a3e9abfbfc2f667e9d746fac5af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sun, 21 Jul 2024 12:19:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E8=B7=AF=E7=94=B1=E4=B8=AD?= =?UTF-8?q?=E9=97=B4=E4=BB=B6=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- router/register.go | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/router/register.go b/router/register.go index 4861187..ea5ceea 100644 --- a/router/register.go +++ b/router/register.go @@ -55,15 +55,23 @@ func parseController(controller any) { routerPrefixFuncType.NumOut() == 1 && // 只能有一个返回值 routerPrefixFuncType.Out(0).Kind() == reflect.String { // 返回值必须是字符串 routerPrefix = routerPrefixFunc.Func.Call([]reflect.Value{controllerValue})[0].String() - // .Call(nil)[0].String() } - // 请求组的中间件 - /*routerMiddlewareFunc := controllerType.MethodByName(MiddlewareFuncName) - routerMiddlewareFuncType := routerMiddlewareFunc.Type() - if routerMiddlewareFuncType.NumIn() == 0 && // 无需任何参数 + } + // 请求组的中间件 + middlewareList := make([]gin.HandlerFunc, 0) + routerMiddlewareFunc, routerMiddlewareFuncExist := controllerType.MethodByName(MiddlewareFuncName) + if routerMiddlewareFuncExist { + routerMiddlewareFuncType := routerMiddlewareFunc.Type + if routerMiddlewareFuncType.NumIn() == 1 && // 无需任何参数 routerMiddlewareFuncType.NumOut() == 1 && // 只能有一个返回值 routerMiddlewareFuncType.Out(0).String() == "[]gin.HandlerFunc" { // 返回值必须是gin.HandlerFunc - }*/ + res := routerMiddlewareFunc.Func.Call([]reflect.Value{controllerValue}) + if !res[0].IsNil() { + middlewareList = res[0].Interface().([]gin.HandlerFunc) + } + } + fmt.Println(111, middlewareList) } + fmt.Println(routerPrefix) }