From 5df55a364d036f8f298f0db9129c8a64207401a8 Mon Sep 17 00:00:00 2001 From: zhaoyupeng Date: Fri, 4 Jul 2025 10:27:44 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20tool=20=E6=B7=BB=E5=8A=A0=20gin=20local?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- resp/resp.go | 5 +++++ tool/gin.go | 12 ++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 tool/gin.go diff --git a/resp/resp.go b/resp/resp.go index c8e2890..a961dc6 100644 --- a/resp/resp.go +++ b/resp/resp.go @@ -3,6 +3,7 @@ package resp import ( "errors" "github.com/gin-gonic/gin" + "github.com/spf13/cast" ) type res struct { @@ -53,6 +54,10 @@ func _r(c *gin.Context, r *res, args ...any) { r.Data = args[1] case 3: r.Err = args[2] + case 4: + if code, err := cast.ToIntE(args[3]); err == nil { + r.Code = code + } } if r.Msg == "" { diff --git a/tool/gin.go b/tool/gin.go new file mode 100644 index 0000000..1a0ac00 --- /dev/null +++ b/tool/gin.go @@ -0,0 +1,12 @@ +package tool + +import "github.com/gin-gonic/gin" + +func Local(c *gin.Context, key string) any { + data, ok := c.Get(key) + if !ok { + return nil + } + + return data +}