新增设置UUID接口,可使用该接口解耦登录逻辑 (#215)

* 🎨 优化是否有人加入了群聊判断逻辑

*  新增设置UUID接口,可使用该接口解耦登录逻辑
This commit is contained in:
李寻欢 2023-01-30 20:30:32 +08:00 committed by GitHub
parent b9bbaed369
commit 7cbb2ae1bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 3 deletions

8
bot.go
View File

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

View File

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

View File

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