From 7cbb2ae1bbf2b97b445d6327c49538991cde843f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AF=BB=E6=AC=A2?= <72033265+lixh00@users.noreply.github.com> Date: Mon, 30 Jan 2023 20:30:32 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E6=96=B0=E5=A2=9E=E8=AE=BE?= =?UTF-8?q?=E7=BD=AEUUID=E6=8E=A5=E5=8F=A3=EF=BC=8C=E5=8F=AF=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E8=AF=A5=E6=8E=A5=E5=8F=A3=E8=A7=A3=E8=80=A6=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E9=80=BB=E8=BE=91=20(#215)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * :art: 优化是否有人加入了群聊判断逻辑 * :sparkles: 新增设置UUID接口,可使用该接口解耦登录逻辑 --- bot.go | 8 ++++++++ bot_login.go | 10 +++++++--- bot_test.go | 26 ++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/bot.go b/bot.go index d1480ba..4b2de9d 100644 --- a/bot.go +++ b/bot.go @@ -307,6 +307,14 @@ func (b *Bot) UUID() string { return b.uuid } +// SetUUID +// @description: 设置UUID,可以用来手动登录用 +// @receiver b +// @param uuid +func (b *Bot) SetUUID(uuid string) { + b.uuid = uuid +} + // Context returns current context of bot func (b *Bot) Context() context.Context { return b.context diff --git a/bot_login.go b/bot_login.go index d56c29b..5cde2ff 100644 --- a/bot_login.go +++ b/bot_login.go @@ -167,9 +167,13 @@ type SacnLogin struct{} // Login 实现了 BotLogin 接口 func (s *SacnLogin) Login(bot *Bot) error { - uuid, err := bot.Caller.GetLoginUUID() - if err != nil { - return err + uuid := bot.uuid + if uuid == "" { + var err error + uuid, err = bot.Caller.GetLoginUUID() + if err != nil { + return err + } } return s.checkLogin(bot, uuid) } diff --git a/bot_test.go b/bot_test.go index b1f6db9..70d754a 100644 --- a/bot_test.go +++ b/bot_test.go @@ -128,4 +128,30 @@ func TestSender(t *testing.T) { bot.Block() } +// TestGetUUID +// @description: 获取登录二维码(UUID) +// @param t +func TestGetUUID(t *testing.T) { + bot := DefaultBot(Desktop) + uuid, err := bot.Caller.GetLoginUUID() + if err != nil { + t.Error(err) + return + } + t.Log(uuid) +} + +// TestLoginWithUUID +// @description: 使用UUID登录 +// @param t +func TestLoginWithUUID(t *testing.T) { + uuid := "oZZsO0Qv8Q==" + bot := DefaultBot(Desktop) + bot.SetUUID(uuid) + err := bot.Login() + if err != nil { + t.Errorf("登录失败: %v", err.Error()) + return + } +}