From 17cc8a371ae9e7d82b124b1d7437ba348d283adb Mon Sep 17 00:00:00 2001 From: Cold <16419454+cold502@user.noreply.gitee.com> Date: Wed, 3 Dec 2025 16:39:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=AD=97=E7=AC=A6=E4=B8=B2?= =?UTF-8?q?=E6=B8=85=E7=90=86=E5=92=8Ctoken=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jaeger/jaeger.go | 13 ++++++++++++- mongo/mongo.go | 14 -------------- utils/utils.go | 23 ++++++++++++++++++++--- 3 files changed, 32 insertions(+), 18 deletions(-) diff --git a/jaeger/jaeger.go b/jaeger/jaeger.go index 9116cc4..31e50e4 100644 --- a/jaeger/jaeger.go +++ b/jaeger/jaeger.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "strconv" + "strings" "github.com/gogf/gf/contrib/trace/otlphttp/v2" "github.com/gogf/gf/v2/frame/g" @@ -28,7 +29,17 @@ func NewTracer(r *ghttp.Request) { defer span.End() span.SetAttributes(attribute.String("request", getParams(r))) r.Middleware.Next() - span.SetAttributes(attribute.String("response", r.Response.BufferString())) + + // 清理响应字符串,确保 UTF-8 有效(处理二进制数据如 ZIP 文件) + response := r.Response.BufferString() + cleanResponse := strings.ToValidUTF8(response, "") + + // 如果响应太大(如文件下载),只记录前 1000 字符 + if len(cleanResponse) > 1000 { + cleanResponse = cleanResponse[:1000] + "... (truncated)" + } + + span.SetAttributes(attribute.String("response", cleanResponse)) } func getParams(r *ghttp.Request) string { params := map[string]interface{}{} diff --git a/mongo/mongo.go b/mongo/mongo.go index b7928ff..92f0c2a 100644 --- a/mongo/mongo.go +++ b/mongo/mongo.go @@ -56,20 +56,6 @@ func Find(ctx context.Context, filter bson.M, result interface{}, collection str return } -// FindWithoutTenant 查询多条记录(不过滤租户,用于导出等场景) -func FindWithoutTenant(ctx context.Context, filter bson.M, result interface{}, collection string, opts ...options.Lister[options.FindOptions]) (err error) { - if err = utils.ValidStructPtr(result); err != nil { - return - } - // 不添加 tenantId 过滤条件 - cur, err := db.Collection(collection).Find(ctx, filter, opts...) - if err != nil { - return - } - err = cur.All(ctx, result) - return -} - // FindOne 查询1条记录 func FindOne(ctx context.Context, filter bson.M, result interface{}, collection string, opts ...options.Lister[options.FindOneOptions]) (err error) { if len(filter) == 0 { diff --git a/utils/utils.go b/utils/utils.go index c2d049f..1e45148 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -3,6 +3,9 @@ package utils import ( "context" "fmt" + "reflect" + "time" + "gitee.com/red-future---jilin-g/common/do" "github.com/gogf/gf/v2/database/gredis" "github.com/gogf/gf/v2/errors/gcode" @@ -10,8 +13,6 @@ import ( "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/util/gconv" "github.com/tiger1103/gfast-token/gftoken" - "reflect" - "time" ) // ValidStructPtr 验证是否为结构体指针 @@ -69,7 +70,23 @@ func GetUserInfo(ctx context.Context) (user do.User, err error) { Address: redisAddr, Db: 1, })) - data, _ := gft.ParseToken(g.RequestFromCtx(ctx)) + + // 解析 token,不再忽略错误 + data, err := gft.ParseToken(g.RequestFromCtx(ctx)) + if err != nil { + return user, gerror.Wrap(err, "token 解析失败") + } + + // 检查 data 是否为 nil + if data == nil { + return user, gerror.New("token 数据为空") + } + + // 检查 data.Data 是否为 nil + if data.Data == nil { + return user, gerror.New("用户信息为空") + } + dataMap := gconv.Map(data.Data) user.UserName = dataMap["userName"] user.TenantId = dataMap["tenantId"]