提交5个文件, 修改了mongo方法, 如果token获取错误, 然后获取完租户id后, 把错误清除

This commit is contained in:
Cold
2025-12-24 18:33:11 +08:00
committed by 张斌
parent 3dc3ebc9ad
commit ecf3c29759
5 changed files with 88 additions and 41 deletions

View File

@@ -261,31 +261,58 @@ func GetTenantInfo(ctx context.Context) (user do.User, err error) {
return
}
// 2. token 获取失败,尝试从请求参数获取 customerServiceId
// 2. token 获取失败,尝试从请求参数或context获取 accountName
var accountName string
// 2.1 尝试从request获取HTTP请求场景
req := g.RequestFromCtx(ctx)
if req == nil {
return user, gerror.New("无法获取租户信息:无 token 且无 request")
if req != nil {
accountName = req.Get("accountName").String()
if accountName == "" {
accountName = req.Get("account_name").String()
}
// 兼容旧参数名
if accountName == "" {
accountName = req.Get("customerServiceId").String()
}
if accountName == "" {
accountName = req.Get("customer_service_id").String()
}
}
customerServiceId := req.Get("customerServiceId").String()
if customerServiceId == "" {
customerServiceId = req.Get("customer_service_id").String()
// 2.2 request不存在或未获取到尝试从context.Value获取WebSocket场景
if accountName == "" {
if val := ctx.Value("accountName"); val != nil {
if str, ok := val.(string); ok {
accountName = str
}
}
// 兼容旧参数名
if accountName == "" {
if val := ctx.Value("customerServiceId"); val != nil {
if str, ok := val.(string); ok {
accountName = str
}
}
}
}
if customerServiceId == "" {
return user, gerror.New("无法获取租户信息:无 token 且无 customerServiceId 参数")
if accountName == "" {
return user, gerror.New("无法获取租户信息:无 token 且无 accountName 参数")
}
// 3. 直接查询 customer_service_account 表获取 tenantId
filter := bson.M{"customerServiceId": customerServiceId, "isDeleted": false}
filter := bson.M{"accountName": accountName, "isDeleted": false}
var account struct {
TenantId interface{} `bson:"tenantId"`
}
if findErr := db.Collection("customer_service_account").FindOne(ctx, filter).Decode(&account); findErr != nil {
return user, gerror.Newf("通过 customerServiceId 查询租户失败: %v", findErr)
return user, gerror.Newf("通过 accountName 查询租户失败: %v", findErr)
}
user.TenantId = account.TenantId
user.UserName = customerServiceId
user.UserName = accountName
err = nil // 清空之前从token获取时的错误
return
}