fix: 登录时将主手机号写入 JWT,修复 bound_phone 返回空的问题
Some checks failed
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Has been cancelled

This commit is contained in:
2026-04-14 11:09:42 +08:00
parent 37706bc7ce
commit c0b64c9e30

View File

@@ -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
}