公共参数自动注入, 增加参数是否已赋值检测 #18
							
								
								
									
										25
									
								
								router/common_param.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								router/common_param.go
									
									
									
									
									
										Normal 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)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -20,7 +20,7 @@ import (
 | 
				
			|||||||
	"github.com/gin-gonic/gin"
 | 
						"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 (
 | 
						var (
 | 
				
			||||||
		formParam reflect.Value
 | 
							formParam reflect.Value
 | 
				
			||||||
		formValue any
 | 
							formValue any
 | 
				
			||||||
@ -43,7 +43,7 @@ func getFormInitValue(ctx *gin.Context, uriCfg UriConfig) (any, error) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// RequestHandler 获取请求处理方法
 | 
					// RequestHandler 获取请求处理方法
 | 
				
			||||||
func RequestHandler(uriCfg UriConfig) gin.HandlerFunc {
 | 
					func (s *server) RequestHandler(uriCfg UriConfig) gin.HandlerFunc {
 | 
				
			||||||
	return func(ctx *gin.Context) {
 | 
						return func(ctx *gin.Context) {
 | 
				
			||||||
		var (
 | 
							var (
 | 
				
			||||||
			err       error
 | 
								err       error
 | 
				
			||||||
@ -52,7 +52,7 @@ func RequestHandler(uriCfg UriConfig) gin.HandlerFunc {
 | 
				
			|||||||
			formValue any
 | 
								formValue any
 | 
				
			||||||
		)
 | 
							)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if formValue, err = getFormInitValue(ctx, uriCfg); nil != err {
 | 
							if formValue, err = s.getFormInitValue(ctx, uriCfg); nil != err {
 | 
				
			||||||
			e = exception.NewFromError(400, err)
 | 
								e = exception.NewFromError(400, err)
 | 
				
			||||||
			response.SendWithException(ctx, e, &define.ResponseOption{
 | 
								response.SendWithException(ctx, e, &define.ResponseOption{
 | 
				
			||||||
				ContentType: consts.MimeTypeJson,
 | 
									ContentType: consts.MimeTypeJson,
 | 
				
			||||||
 | 
				
			|||||||
@ -11,6 +11,7 @@ import (
 | 
				
			|||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
 | 
						"sync"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"git.zhangdeman.cn/zhangdeman/graceful"
 | 
						"git.zhangdeman.cn/zhangdeman/graceful"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -129,14 +130,17 @@ func NewServer(port int, optionList ...SetServerOptionFunc) *server {
 | 
				
			|||||||
		uiInstance: apiDoc.NewSwaggerUI(option.serverInfo, option.serverList, apiDocEnum.SwaggerUITheme(option.swaggerUiTheme)),
 | 
							uiInstance: apiDoc.NewSwaggerUI(option.serverInfo, option.serverList, apiDocEnum.SwaggerUITheme(option.swaggerUiTheme)),
 | 
				
			||||||
		port:       port,
 | 
							port:       port,
 | 
				
			||||||
		option:     option,
 | 
							option:     option,
 | 
				
			||||||
 | 
							lock:       &sync.RWMutex{},
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type server struct {
 | 
					type server struct {
 | 
				
			||||||
	router     *gin.Engine
 | 
						router      *gin.Engine
 | 
				
			||||||
	port       int
 | 
						port        int
 | 
				
			||||||
	uiInstance *apiDoc.SwaggerUI
 | 
						uiInstance  *apiDoc.SwaggerUI
 | 
				
			||||||
	option     *serverOption
 | 
						option      *serverOption
 | 
				
			||||||
 | 
						commonParam map[string]GetCommonParam // 结构体字段名, 注意, 不是TAG
 | 
				
			||||||
 | 
						lock        *sync.RWMutex
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Start 启动服务
 | 
					// Start 启动服务
 | 
				
			||||||
@ -201,21 +205,22 @@ func (s *server) Group(routerPrefix string, middlewareList []gin.HandlerFunc, cL
 | 
				
			|||||||
		for _, itemUriCfg := range urlTable {
 | 
							for _, itemUriCfg := range urlTable {
 | 
				
			||||||
			_ = s.uiInstance.DocInstance().AddApiFromInAndOut(routerPrefix, itemUriCfg.FormDataType, itemUriCfg.ResultDataType)
 | 
								_ = s.uiInstance.DocInstance().AddApiFromInAndOut(routerPrefix, itemUriCfg.FormDataType, itemUriCfg.ResultDataType)
 | 
				
			||||||
			method := strings.ToUpper(itemUriCfg.RequestMethod)
 | 
								method := strings.ToUpper(itemUriCfg.RequestMethod)
 | 
				
			||||||
 | 
								handleFunc := s.RequestHandler(itemUriCfg)
 | 
				
			||||||
			switch method {
 | 
								switch method {
 | 
				
			||||||
			case http.MethodGet:
 | 
								case http.MethodGet:
 | 
				
			||||||
				g.GET(itemUriCfg.Path, RequestHandler(itemUriCfg))
 | 
									g.GET(itemUriCfg.Path, handleFunc)
 | 
				
			||||||
			case http.MethodHead:
 | 
								case http.MethodHead:
 | 
				
			||||||
				g.HEAD(itemUriCfg.Path, RequestHandler(itemUriCfg))
 | 
									g.HEAD(itemUriCfg.Path, handleFunc)
 | 
				
			||||||
			case http.MethodPost:
 | 
								case http.MethodPost:
 | 
				
			||||||
				g.POST(itemUriCfg.Path, RequestHandler(itemUriCfg))
 | 
									g.POST(itemUriCfg.Path, handleFunc)
 | 
				
			||||||
			case http.MethodPut:
 | 
								case http.MethodPut:
 | 
				
			||||||
				g.PUT(itemUriCfg.Path, RequestHandler(itemUriCfg))
 | 
									g.PUT(itemUriCfg.Path, handleFunc)
 | 
				
			||||||
			case http.MethodPatch:
 | 
								case http.MethodPatch:
 | 
				
			||||||
				g.PATCH(itemUriCfg.Path, RequestHandler(itemUriCfg))
 | 
									g.PATCH(itemUriCfg.Path, handleFunc)
 | 
				
			||||||
			case http.MethodDelete:
 | 
								case http.MethodDelete:
 | 
				
			||||||
				g.DELETE(itemUriCfg.Path, RequestHandler(itemUriCfg))
 | 
									g.DELETE(itemUriCfg.Path, handleFunc)
 | 
				
			||||||
			case http.MethodOptions:
 | 
								case http.MethodOptions:
 | 
				
			||||||
				g.OPTIONS(itemUriCfg.Path, RequestHandler(itemUriCfg))
 | 
									g.OPTIONS(itemUriCfg.Path, handleFunc)
 | 
				
			||||||
			case http.MethodTrace:
 | 
								case http.MethodTrace:
 | 
				
			||||||
				panic(`method Trace is not supported`)
 | 
									panic(`method Trace is not supported`)
 | 
				
			||||||
			default:
 | 
								default:
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user