perf(auth): 降低会话校验请求与重复查库
This commit is contained in:
@@ -58,6 +58,15 @@ func (r *UserRepository) FindByID(ctx context.Context, id uint64) (*model.User,
|
||||
|
||||
// FindActiveForToken 校验用户仍可用且令牌版本未被撤销。
|
||||
func (r *UserRepository) FindActiveForToken(ctx context.Context, id uint64, tokenVersion int64) (*model.User, error) {
|
||||
return r.findActiveForToken(ctx, id, tokenVersion, false)
|
||||
}
|
||||
|
||||
// FindActiveForTokenWithProfile 校验令牌并一次加载完整用户资料,供 /api/me 使用。
|
||||
func (r *UserRepository) FindActiveForTokenWithProfile(ctx context.Context, id uint64, tokenVersion int64) (*model.User, error) {
|
||||
return r.findActiveForToken(ctx, id, tokenVersion, true)
|
||||
}
|
||||
|
||||
func (r *UserRepository) findActiveForToken(ctx context.Context, id uint64, tokenVersion int64, withProfile bool) (*model.User, error) {
|
||||
if r == nil || r.db == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
@@ -67,9 +76,10 @@ func (r *UserRepository) FindActiveForToken(ctx context.Context, id uint64, toke
|
||||
query = query.Where("tenant_id = ? AND distribution_site_id = 0", tenantID)
|
||||
}
|
||||
query = scopeUserQueryToBoundSite(ctx, query)
|
||||
if err := query.
|
||||
Select("users.id, users.phone, users.status, users.token_version").
|
||||
First(&user, id).Error; err != nil {
|
||||
if !withProfile {
|
||||
query = query.Select("users.id, users.phone, users.status, users.token_version")
|
||||
}
|
||||
if err := query.First(&user, id).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if user.Status != "active" {
|
||||
|
||||
Reference in New Issue
Block a user