diff --git a/internal/service/client_auth/service.go b/internal/service/client_auth/service.go index 39495af..cfdcc5a 100644 --- a/internal/service/client_auth/service.go +++ b/internal/service/client_auth/service.go @@ -692,7 +692,19 @@ func (s *Service) markAssetAsSold(ctx context.Context, tx *gorm.DB, assetType st } func (s *Service) issueLoginToken(ctx context.Context, customerID uint) (string, bool, error) { - token, err := s.jwtManager.GeneratePersonalCustomerToken(customerID, "") + // 查询用户已绑定的主手机号,写入 JWT,供后续接口直接从 context 取用 + var boundPhone string + needBindPhone := false + primaryPhone, phoneErr := s.phoneStore.GetPrimaryPhone(ctx, customerID) + if phoneErr == nil { + boundPhone = primaryPhone.Phone + } else if phoneErr != gorm.ErrRecordNotFound { + return "", false, errors.Wrap(errors.CodeInternalError, phoneErr, "查询手机号绑定关系失败") + } else if viper.GetBool("client.require_phone_binding") { + needBindPhone = true + } + + token, err := s.jwtManager.GeneratePersonalCustomerToken(customerID, boundPhone) if err != nil { return "", false, errors.Wrap(errors.CodeInternalError, err, "生成登录令牌失败") } @@ -712,15 +724,6 @@ func (s *Service) issueLoginToken(ctx context.Context, customerID uint) (string, return "", false, errors.Wrap(errors.CodeRedisError, err, "保存登录状态失败") } - needBindPhone := false - if viper.GetBool("client.require_phone_binding") { - if _, err := s.phoneStore.GetPrimaryPhone(ctx, customerID); err == gorm.ErrRecordNotFound { - needBindPhone = true - } else if err != nil { - return "", false, errors.Wrap(errors.CodeInternalError, err, "查询手机号绑定关系失败") - } - } - return token, needBindPhone, nil }