添加 LoginChecker (#181)

This commit is contained in:
多吃点苹果 2023-01-06 01:48:33 +08:00 committed by GitHub
parent 953c1ce0be
commit 721f56314e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -20,37 +20,12 @@ func (s *SacnLogin) Login(bot *Bot) error {
// checkLogin 该方法会一直阻塞,直到用户扫码登录,或者二维码过期 // checkLogin 该方法会一直阻塞,直到用户扫码登录,或者二维码过期
func (s *SacnLogin) checkLogin(bot *Bot, uuid string) error { func (s *SacnLogin) checkLogin(bot *Bot, uuid string) error {
bot.uuid = uuid bot.uuid = uuid
// 二维码获取回调 bot.uuid = uuid
if bot.UUIDCallback != nil { loginChecker := &LoginChecker{
bot.UUIDCallback(uuid) Bot: bot,
} Tip: "0",
for {
// 长轮询检查是否扫码登录
resp, err := bot.Caller.CheckLogin(uuid, "0")
if err != nil {
return err
}
switch resp.Code {
case StatusSuccess:
// 判断是否有登录回调,如果有执行它
if err = bot.HandleLogin(resp.Raw); err != nil {
return err
}
if bot.LoginCallBack != nil {
bot.LoginCallBack(resp.Raw)
}
return nil
case StatusScanned:
// 执行扫码回调
if bot.ScanCallBack != nil {
bot.ScanCallBack(resp.Raw)
}
case StatusTimeout:
return ErrLoginTimeout
case StatusWait:
continue
}
} }
return loginChecker.CheckLogin()
} }
type hotLoginOption struct { type hotLoginOption struct {
@ -155,7 +130,7 @@ func (p PushLogin) Login(bot *Bot) error {
if err = resp.Err(); err != nil { if err = resp.Err(); err != nil {
return err return err
} }
err = p.checkLogin(bot, resp.UUID, "1") err = p.checkLogin(bot, resp.UUID)
if err != nil && p.opt.withRetry { if err != nil && p.opt.withRetry {
scanLogin := SacnLogin{} scanLogin := SacnLogin{}
return scanLogin.Login(bot) return scanLogin.Login(bot)
@ -169,16 +144,36 @@ func (p PushLogin) pushLoginInit(bot *Bot) error {
} }
// checkLogin 登录检查 // checkLogin 登录检查
func (p PushLogin) checkLogin(bot *Bot, uuid, tip string) error { func (p PushLogin) checkLogin(bot *Bot, uuid string) error {
// todo 将checkLogin剥离出来
bot.uuid = uuid bot.uuid = uuid
// 二维码获取回调 loginChecker := &LoginChecker{
if bot.UUIDCallback != nil && !p.opt.withoutUUIDCallback { Bot: bot,
bot.UUIDCallback(uuid) Tip: "1",
WithLoginCallback: p.opt.withoutLoginCallback,
WithoutUUIDCallback: p.opt.withoutUUIDCallback,
WithScanCallback: p.opt.withoutScanCallback,
} }
return loginChecker.CheckLogin()
}
type LoginChecker struct {
Bot *Bot
Tip string
WithoutUUIDCallback bool
WithLoginCallback bool
WithScanCallback bool
}
func (l *LoginChecker) CheckLogin() error {
uuid := l.Bot.UUID()
// 二维码获取回调
if l.Bot.UUIDCallback != nil && !l.WithoutUUIDCallback {
l.Bot.UUIDCallback(uuid)
}
var tip = l.Tip
for { for {
// 长轮询检查是否扫码登录 // 长轮询检查是否扫码登录
resp, err := bot.Caller.CheckLogin(uuid, tip) resp, err := l.Bot.Caller.CheckLogin(uuid, tip)
if err != nil { if err != nil {
return err return err
} }
@ -188,17 +183,17 @@ func (p PushLogin) checkLogin(bot *Bot, uuid, tip string) error {
switch resp.Code { switch resp.Code {
case StatusSuccess: case StatusSuccess:
// 判断是否有登录回调,如果有执行它 // 判断是否有登录回调,如果有执行它
if err = bot.HandleLogin(resp.Raw); err != nil { if err = l.Bot.HandleLogin(resp.Raw); err != nil {
return err return err
} }
if bot.LoginCallBack != nil && !p.opt.withoutLoginCallback { if l.Bot.LoginCallBack != nil && !l.WithLoginCallback {
bot.LoginCallBack(resp.Raw) l.Bot.LoginCallBack(resp.Raw)
} }
return nil return nil
case StatusScanned: case StatusScanned:
// 执行扫码回调 // 执行扫码回调
if bot.ScanCallBack != nil && !p.opt.withoutScanCallback { if l.Bot.ScanCallBack != nil && !l.WithScanCallback {
bot.ScanCallBack(resp.Raw) l.Bot.ScanCallBack(resp.Raw)
} }
case StatusTimeout: case StatusTimeout:
return ErrLoginTimeout return ErrLoginTimeout