更新字符串清理和token报错

This commit is contained in:
Cold
2025-12-03 16:39:55 +08:00
committed by 张斌
parent 0738f6f957
commit 17cc8a371a
3 changed files with 32 additions and 18 deletions

View File

@@ -4,6 +4,7 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"strconv" "strconv"
"strings"
"github.com/gogf/gf/contrib/trace/otlphttp/v2" "github.com/gogf/gf/contrib/trace/otlphttp/v2"
"github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/frame/g"
@@ -28,7 +29,17 @@ func NewTracer(r *ghttp.Request) {
defer span.End() defer span.End()
span.SetAttributes(attribute.String("request", getParams(r))) span.SetAttributes(attribute.String("request", getParams(r)))
r.Middleware.Next() 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 { func getParams(r *ghttp.Request) string {
params := map[string]interface{}{} params := map[string]interface{}{}

View File

@@ -56,20 +56,6 @@ func Find(ctx context.Context, filter bson.M, result interface{}, collection str
return 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条记录 // FindOne 查询1条记录
func FindOne(ctx context.Context, filter bson.M, result interface{}, collection string, opts ...options.Lister[options.FindOneOptions]) (err error) { func FindOne(ctx context.Context, filter bson.M, result interface{}, collection string, opts ...options.Lister[options.FindOneOptions]) (err error) {
if len(filter) == 0 { if len(filter) == 0 {

View File

@@ -3,6 +3,9 @@ package utils
import ( import (
"context" "context"
"fmt" "fmt"
"reflect"
"time"
"gitee.com/red-future---jilin-g/common/do" "gitee.com/red-future---jilin-g/common/do"
"github.com/gogf/gf/v2/database/gredis" "github.com/gogf/gf/v2/database/gredis"
"github.com/gogf/gf/v2/errors/gcode" "github.com/gogf/gf/v2/errors/gcode"
@@ -10,8 +13,6 @@ import (
"github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/util/gconv" "github.com/gogf/gf/v2/util/gconv"
"github.com/tiger1103/gfast-token/gftoken" "github.com/tiger1103/gfast-token/gftoken"
"reflect"
"time"
) )
// ValidStructPtr 验证是否为结构体指针 // ValidStructPtr 验证是否为结构体指针
@@ -69,7 +70,23 @@ func GetUserInfo(ctx context.Context) (user do.User, err error) {
Address: redisAddr, Address: redisAddr,
Db: 1, 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) dataMap := gconv.Map(data.Data)
user.UserName = dataMap["userName"] user.UserName = dataMap["userName"]
user.TenantId = dataMap["tenantId"] user.TenantId = dataMap["tenantId"]