[refactor] pushLogin 设置二维码回调和扫码回调默认不触发 (#197)
This commit is contained in:
parent
cc4d650796
commit
dc3669dcad
19
bot.go
19
bot.go
@ -29,6 +29,7 @@ type Bot struct {
|
|||||||
hotReloadStorage HotReloadStorage
|
hotReloadStorage HotReloadStorage
|
||||||
uuid string
|
uuid string
|
||||||
deviceId string // 设备Id
|
deviceId string // 设备Id
|
||||||
|
loginOptionGroup BotOptionGroup
|
||||||
}
|
}
|
||||||
|
|
||||||
// Alive 判断当前用户是否正常在线
|
// Alive 判断当前用户是否正常在线
|
||||||
@ -67,8 +68,8 @@ func (b *Bot) GetCurrentUser() (*Self, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// login 这里对进行一些对登录前后的hook
|
// login 这里对进行一些对登录前后的hook
|
||||||
func (b *Bot) login(login BotLogin, opts ...BotLoginOption) (err error) {
|
func (b *Bot) login(login BotLogin) (err error) {
|
||||||
opt := BotOptionGroup(opts)
|
opt := b.loginOptionGroup
|
||||||
opt.Prepare(b)
|
opt.Prepare(b)
|
||||||
if err = login.Login(b); err != nil {
|
if err = login.Login(b); err != nil {
|
||||||
err = opt.OnError(b, err)
|
err = opt.OnError(b, err)
|
||||||
@ -81,11 +82,7 @@ func (b *Bot) login(login BotLogin, opts ...BotLoginOption) (err error) {
|
|||||||
|
|
||||||
// Login 用户登录
|
// Login 用户登录
|
||||||
func (b *Bot) Login() error {
|
func (b *Bot) Login() error {
|
||||||
scanLogin := &SacnLogin{
|
scanLogin := &SacnLogin{}
|
||||||
UUIDCallback: b.UUIDCallback,
|
|
||||||
ScanCallBack: b.ScanCallBack,
|
|
||||||
LoginCallBack: b.LoginCallBack,
|
|
||||||
}
|
|
||||||
return b.login(scanLogin)
|
return b.login(scanLogin)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -94,8 +91,8 @@ func (b *Bot) HotLogin(storage HotReloadStorage, opts ...BotLoginOption) error {
|
|||||||
hotLogin := &HotLogin{storage: storage}
|
hotLogin := &HotLogin{storage: storage}
|
||||||
// 进行相关设置。
|
// 进行相关设置。
|
||||||
// 如果相对默认的行为进行修改,在opts里面进行追加即可。
|
// 如果相对默认的行为进行修改,在opts里面进行追加即可。
|
||||||
opts = append(hotLoginDefaultOptions[:], opts...)
|
b.loginOptionGroup = append(hotLoginDefaultOptions[:], opts...)
|
||||||
return b.login(hotLogin, opts...)
|
return b.login(hotLogin)
|
||||||
}
|
}
|
||||||
|
|
||||||
// PushLogin 免扫码登录
|
// PushLogin 免扫码登录
|
||||||
@ -106,8 +103,8 @@ func (b *Bot) PushLogin(storage HotReloadStorage, opts ...BotLoginOption) error
|
|||||||
pushLogin := &PushLogin{storage: storage}
|
pushLogin := &PushLogin{storage: storage}
|
||||||
// 进行相关设置。
|
// 进行相关设置。
|
||||||
// 如果相对默认的行为进行修改,在opts里面进行追加即可。
|
// 如果相对默认的行为进行修改,在opts里面进行追加即可。
|
||||||
opts = append(pushLoginDefaultOptions[:], opts...)
|
b.loginOptionGroup = append(pushLoginDefaultOptions[:], opts...)
|
||||||
return b.login(pushLogin, opts...)
|
return b.login(pushLogin)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Logout 用户退出
|
// Logout 用户退出
|
||||||
|
100
bot_login.go
100
bot_login.go
@ -64,24 +64,22 @@ var DoNothingBotLoginOption = &BaseBotLoginOption{}
|
|||||||
// RetryLoginOption 在登录失败后进行扫码登录
|
// RetryLoginOption 在登录失败后进行扫码登录
|
||||||
type RetryLoginOption struct {
|
type RetryLoginOption struct {
|
||||||
BaseBotLoginOption
|
BaseBotLoginOption
|
||||||
SacnLogin
|
MaxRetryCount int
|
||||||
}
|
currentRetryTime int
|
||||||
|
|
||||||
// Prepare 实现了 BotLoginOption 接口
|
|
||||||
func (r *RetryLoginOption) Prepare(bot *Bot) {
|
|
||||||
r.UUIDCallback = bot.UUIDCallback
|
|
||||||
r.LoginCallBack = bot.LoginCallBack
|
|
||||||
r.ScanCallBack = bot.ScanCallBack
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// OnError 实现了 BotLoginOption 接口
|
// OnError 实现了 BotLoginOption 接口
|
||||||
// 当登录失败后,会调用此方法进行扫码登录
|
// 当登录失败后,会调用此方法进行扫码登录
|
||||||
func (r *RetryLoginOption) OnError(bot *Bot, _ error) error {
|
func (r *RetryLoginOption) OnError(bot *Bot, err error) error {
|
||||||
return r.Login(bot)
|
if r.currentRetryTime >= r.MaxRetryCount {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
r.currentRetryTime++
|
||||||
|
return bot.Login()
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRetryLoginOption() BotLoginOption {
|
func NewRetryLoginOption() BotLoginOption {
|
||||||
return &RetryLoginOption{}
|
return &RetryLoginOption{MaxRetryCount: 1}
|
||||||
}
|
}
|
||||||
|
|
||||||
// SyncReloadDataLoginOption 在登录成功后进行数据定时同步到指定的storage中
|
// SyncReloadDataLoginOption 在登录成功后进行数据定时同步到指定的storage中
|
||||||
@ -105,38 +103,6 @@ func NewSyncReloadDataLoginOption(duration time.Duration) BotLoginOption {
|
|||||||
return &SyncReloadDataLoginOption{SyncLoopDuration: duration}
|
return &SyncReloadDataLoginOption{SyncLoopDuration: duration}
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithoutLoginCallbackOption 不使用登录回调
|
|
||||||
type WithoutLoginCallbackOption struct{ BaseBotLoginOption }
|
|
||||||
|
|
||||||
// Prepare 实现了 BotLoginOption 接口
|
|
||||||
// 将设置的 LoginCallback 置为 nil
|
|
||||||
func (w WithoutLoginCallbackOption) Prepare(b *Bot) { b.LoginCallBack = nil }
|
|
||||||
|
|
||||||
func NewWithoutLoginCallbackOption() BotLoginOption {
|
|
||||||
return &WithoutLoginCallbackOption{}
|
|
||||||
}
|
|
||||||
|
|
||||||
// WithoutScanCallbackOption 不使用扫码回调
|
|
||||||
type WithoutScanCallbackOption struct{ BaseBotLoginOption }
|
|
||||||
|
|
||||||
// Prepare 实现了 BotLoginOption 接口
|
|
||||||
func (w WithoutScanCallbackOption) Prepare(b *Bot) { b.ScanCallBack = nil }
|
|
||||||
|
|
||||||
func NewWithoutScanCallbackOption() BotLoginOption {
|
|
||||||
return &WithoutScanCallbackOption{}
|
|
||||||
}
|
|
||||||
|
|
||||||
// WithoutUUIDCallbackOption 不使用UUID回调
|
|
||||||
type WithoutUUIDCallbackOption struct{ BaseBotLoginOption }
|
|
||||||
|
|
||||||
// Prepare 实现了 BotLoginOption 接口
|
|
||||||
// 将设置的 UUIDCallback 置为 nil
|
|
||||||
func (w WithoutUUIDCallbackOption) Prepare(bot *Bot) { bot.UUIDCallback = nil }
|
|
||||||
|
|
||||||
func NewWithoutUUIDCallbackOption() BotLoginOption {
|
|
||||||
return &WithoutUUIDCallbackOption{}
|
|
||||||
}
|
|
||||||
|
|
||||||
// WithModeOption 指定使用哪种客户端模式
|
// WithModeOption 指定使用哪种客户端模式
|
||||||
type WithModeOption struct {
|
type WithModeOption struct {
|
||||||
mode Mode
|
mode Mode
|
||||||
@ -168,11 +134,7 @@ type BotLogin interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// SacnLogin 扫码登录
|
// SacnLogin 扫码登录
|
||||||
type SacnLogin struct {
|
type SacnLogin struct{}
|
||||||
UUIDCallback func(uuid string)
|
|
||||||
LoginCallBack func(body []byte)
|
|
||||||
ScanCallBack func(body []byte)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Login 实现了 BotLogin 接口
|
// Login 实现了 BotLogin 接口
|
||||||
func (s *SacnLogin) Login(bot *Bot) error {
|
func (s *SacnLogin) Login(bot *Bot) error {
|
||||||
@ -189,9 +151,9 @@ func (s *SacnLogin) checkLogin(bot *Bot, uuid string) error {
|
|||||||
loginChecker := &LoginChecker{
|
loginChecker := &LoginChecker{
|
||||||
Bot: bot,
|
Bot: bot,
|
||||||
Tip: "0",
|
Tip: "0",
|
||||||
UUIDCallback: s.UUIDCallback,
|
UUIDCallback: bot.UUIDCallback,
|
||||||
LoginCallBack: s.LoginCallBack,
|
LoginCallBack: bot.LoginCallBack,
|
||||||
ScanCallBack: s.ScanCallBack,
|
ScanCallBack: bot.ScanCallBack,
|
||||||
}
|
}
|
||||||
return loginChecker.CheckLogin()
|
return loginChecker.CheckLogin()
|
||||||
}
|
}
|
||||||
@ -200,10 +162,7 @@ var (
|
|||||||
hotLoginDefaultOptions = [...]BotLoginOption{
|
hotLoginDefaultOptions = [...]BotLoginOption{
|
||||||
NewSyncReloadDataLoginOption(defaultHotStorageSyncDuration),
|
NewSyncReloadDataLoginOption(defaultHotStorageSyncDuration),
|
||||||
}
|
}
|
||||||
pushLoginDefaultOptions = [...]BotLoginOption{
|
pushLoginDefaultOptions = hotLoginDefaultOptions
|
||||||
NewSyncReloadDataLoginOption(defaultHotStorageSyncDuration),
|
|
||||||
NewWithoutScanCallbackOption(),
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// HotLogin 热登录模式
|
// HotLogin 热登录模式
|
||||||
@ -252,12 +211,12 @@ func (p *PushLogin) pushLoginInit(bot *Bot) error {
|
|||||||
// checkLogin 登录检查
|
// checkLogin 登录检查
|
||||||
func (p *PushLogin) checkLogin(bot *Bot, uuid string) error {
|
func (p *PushLogin) checkLogin(bot *Bot, uuid string) error {
|
||||||
bot.uuid = uuid
|
bot.uuid = uuid
|
||||||
|
// 为什么把 UUIDCallback 和 ScanCallBack 置为nil呢?
|
||||||
|
// 因为这两个对用户是无感知的。
|
||||||
loginChecker := &LoginChecker{
|
loginChecker := &LoginChecker{
|
||||||
Bot: bot,
|
Bot: bot,
|
||||||
Tip: "1",
|
Tip: "1",
|
||||||
UUIDCallback: bot.UUIDCallback,
|
|
||||||
LoginCallBack: bot.LoginCallBack,
|
LoginCallBack: bot.LoginCallBack,
|
||||||
ScanCallBack: bot.ScanCallBack,
|
|
||||||
}
|
}
|
||||||
return loginChecker.CheckLogin()
|
return loginChecker.CheckLogin()
|
||||||
}
|
}
|
||||||
@ -329,33 +288,6 @@ func HotLoginWithSyncReloadData(duration time.Duration) BotLoginOption {
|
|||||||
return NewSyncReloadDataLoginOption(duration)
|
return NewSyncReloadDataLoginOption(duration)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Deprecated: 请使用 NewWithoutLoginCallbackOption 代替
|
|
||||||
// PushLoginWithoutUUIDCallback 免扫码登录模式,不执行UUID回调
|
|
||||||
func PushLoginWithoutUUIDCallback(flag bool) BotLoginOption {
|
|
||||||
if !flag {
|
|
||||||
return DoNothingBotLoginOption
|
|
||||||
}
|
|
||||||
return NewWithoutLoginCallbackOption()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Deprecated: 请使用 NewWithoutScanCallbackOption 代替
|
|
||||||
// PushLoginWithoutScanCallback 免扫码登录模式,不执行扫码回调
|
|
||||||
func PushLoginWithoutScanCallback(flag bool) BotLoginOption {
|
|
||||||
if !flag {
|
|
||||||
return DoNothingBotLoginOption
|
|
||||||
}
|
|
||||||
return NewWithoutScanCallbackOption()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Deprecated: 请使用 NewWithoutLoginCallbackOption 代替
|
|
||||||
// PushLoginWithoutLoginCallback 免扫码登录模式,不执行登录回调
|
|
||||||
func PushLoginWithoutLoginCallback(flag bool) BotLoginOption {
|
|
||||||
if !flag {
|
|
||||||
return DoNothingBotLoginOption
|
|
||||||
}
|
|
||||||
return NewWithoutLoginCallbackOption()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Deprecated: 请使用 NewRetryLoginOption 代替
|
// Deprecated: 请使用 NewRetryLoginOption 代替
|
||||||
// PushLoginWithRetry 免扫码登录模式,如果登录失败会重试
|
// PushLoginWithRetry 免扫码登录模式,如果登录失败会重试
|
||||||
func PushLoginWithRetry(flag bool) BotLoginOption {
|
func PushLoginWithRetry(flag bool) BotLoginOption {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user