feat: 增加注册注入参数的方法

This commit is contained in:
2025-10-30 15:48:43 +08:00
parent 1a9bc7b6dd
commit 028d16ac5a
3 changed files with 44 additions and 14 deletions

25
router/common_param.go Normal file
View File

@ -0,0 +1,25 @@
// Package router ...
//
// Description : router ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2025-10-30 15:39
package router
import "github.com/gin-gonic/gin"
// GetCommonParam 获取公共参数
type GetCommonParam func(ctx *gin.Context) (any, error)
// AddCommonParamRule 添加公共参数注入规则
func (s *server) AddCommonParamRule(fieldName string, getParamFunc GetCommonParam) {
s.commonParam[fieldName] = getParamFunc
}
// AddCommonParamRules 批量添加公共参数注入规则
func (s *server) AddCommonParamRules(rules map[string]GetCommonParam) {
for fieldName, rule := range rules {
s.AddCommonParamRule(fieldName, rule)
}
}

View File

@ -20,7 +20,7 @@ import (
"github.com/gin-gonic/gin"
)
func getFormInitValue(ctx *gin.Context, uriCfg UriConfig) (any, error) {
func (s *server) getFormInitValue(ctx *gin.Context, uriCfg UriConfig) (any, error) {
var (
formParam reflect.Value
formValue any
@ -43,7 +43,7 @@ func getFormInitValue(ctx *gin.Context, uriCfg UriConfig) (any, error) {
}
// RequestHandler 获取请求处理方法
func RequestHandler(uriCfg UriConfig) gin.HandlerFunc {
func (s *server) RequestHandler(uriCfg UriConfig) gin.HandlerFunc {
return func(ctx *gin.Context) {
var (
err error
@ -52,7 +52,7 @@ func RequestHandler(uriCfg UriConfig) gin.HandlerFunc {
formValue any
)
if formValue, err = getFormInitValue(ctx, uriCfg); nil != err {
if formValue, err = s.getFormInitValue(ctx, uriCfg); nil != err {
e = exception.NewFromError(400, err)
response.SendWithException(ctx, e, &define.ResponseOption{
ContentType: consts.MimeTypeJson,

View File

@ -11,6 +11,7 @@ import (
"fmt"
"net/http"
"strings"
"sync"
"git.zhangdeman.cn/zhangdeman/graceful"
@ -129,6 +130,7 @@ func NewServer(port int, optionList ...SetServerOptionFunc) *server {
uiInstance: apiDoc.NewSwaggerUI(option.serverInfo, option.serverList, apiDocEnum.SwaggerUITheme(option.swaggerUiTheme)),
port: port,
option: option,
lock: &sync.RWMutex{},
}
}
@ -137,6 +139,8 @@ type server struct {
port int
uiInstance *apiDoc.SwaggerUI
option *serverOption
commonParam map[string]GetCommonParam // 结构体字段名, 注意, 不是TAG
lock *sync.RWMutex
}
// Start 启动服务
@ -201,21 +205,22 @@ func (s *server) Group(routerPrefix string, middlewareList []gin.HandlerFunc, cL
for _, itemUriCfg := range urlTable {
_ = s.uiInstance.DocInstance().AddApiFromInAndOut(routerPrefix, itemUriCfg.FormDataType, itemUriCfg.ResultDataType)
method := strings.ToUpper(itemUriCfg.RequestMethod)
handleFunc := s.RequestHandler(itemUriCfg)
switch method {
case http.MethodGet:
g.GET(itemUriCfg.Path, RequestHandler(itemUriCfg))
g.GET(itemUriCfg.Path, handleFunc)
case http.MethodHead:
g.HEAD(itemUriCfg.Path, RequestHandler(itemUriCfg))
g.HEAD(itemUriCfg.Path, handleFunc)
case http.MethodPost:
g.POST(itemUriCfg.Path, RequestHandler(itemUriCfg))
g.POST(itemUriCfg.Path, handleFunc)
case http.MethodPut:
g.PUT(itemUriCfg.Path, RequestHandler(itemUriCfg))
g.PUT(itemUriCfg.Path, handleFunc)
case http.MethodPatch:
g.PATCH(itemUriCfg.Path, RequestHandler(itemUriCfg))
g.PATCH(itemUriCfg.Path, handleFunc)
case http.MethodDelete:
g.DELETE(itemUriCfg.Path, RequestHandler(itemUriCfg))
g.DELETE(itemUriCfg.Path, handleFunc)
case http.MethodOptions:
g.OPTIONS(itemUriCfg.Path, RequestHandler(itemUriCfg))
g.OPTIONS(itemUriCfg.Path, handleFunc)
case http.MethodTrace:
panic(`method Trace is not supported`)
default: