提交5个文件, 修改了mongo方法, 如果token获取错误, 然后获取完租户id后, 把错误清除
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user