Merge pull request #52 from li-xunhuan/master

 开放几个变量和函数支持外部访问
This commit is contained in:
Ivy 2021-08-01 10:49:23 +08:00 committed by GitHub
commit 9c069eeab8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 27 deletions

46
bot.go
View File

@ -15,7 +15,7 @@ type Bot struct {
UUIDCallback func(uuid string) // 获取UUID的回调函数 UUIDCallback func(uuid string) // 获取UUID的回调函数
MessageHandler MessageHandler // 获取消息成功的handle MessageHandler MessageHandler // 获取消息成功的handle
GetMessageErrorHandler func(err error) // 获取消息发生错误的handle GetMessageErrorHandler func(err error) // 获取消息发生错误的handle
isHot bool // 是否为热登录模式 IsHot bool // 是否为热登录模式
once sync.Once once sync.Once
err error err error
context context.Context context context.Context
@ -23,7 +23,7 @@ type Bot struct {
Caller *Caller Caller *Caller
self *Self self *Self
storage *Storage storage *Storage
hotReloadStorage HotReloadStorage HotReloadStorage HotReloadStorage
} }
// Alive 判断当前用户是否正常在线 // Alive 判断当前用户是否正常在线
@ -58,8 +58,8 @@ func (b *Bot) GetCurrentUser() (*Self, error) {
// err := bot.HotLogin(storage, true) // err := bot.HotLogin(storage, true)
// fmt.Println(err) // fmt.Println(err)
func (b *Bot) HotLogin(storage HotReloadStorage, retry ...bool) error { func (b *Bot) HotLogin(storage HotReloadStorage, retry ...bool) error {
b.isHot = true b.IsHot = true
b.hotReloadStorage = storage b.HotReloadStorage = storage
var err error var err error
@ -69,13 +69,13 @@ func (b *Bot) HotLogin(storage HotReloadStorage, retry ...bool) error {
return b.Login() return b.Login()
} }
if err = b.hotLoginInit(); err != nil { if err = b.HotLoginInit(); err != nil {
return err return err
} }
// 如果webInit出错,则说明可能身份信息已经失效 // 如果webInit出错,则说明可能身份信息已经失效
// 如果retry为True的话,则进行正常登陆 // 如果retry为True的话,则进行正常登陆
if err = b.webInit(); err != nil { if err = b.WebInit(); err != nil {
if len(retry) > 0 && retry[0] { if len(retry) > 0 && retry[0] {
return b.Login() return b.Login()
} }
@ -83,9 +83,9 @@ func (b *Bot) HotLogin(storage HotReloadStorage, retry ...bool) error {
return err return err
} }
// 热登陆初始化 // HotLoginInit 热登陆初始化
func (b *Bot) hotLoginInit() error { func (b *Bot) HotLoginInit() error {
item := b.hotReloadStorage.GetHotReloadStorageItem() item := b.HotReloadStorage.GetHotReloadStorageItem()
cookies := item.Cookies cookies := item.Cookies
for u, ck := range cookies { for u, ck := range cookies {
path, err := url.Parse(u) path, err := url.Parse(u)
@ -118,20 +118,20 @@ func (b *Bot) Login() error {
return err return err
} }
switch resp.Code { switch resp.Code {
case statusSuccess: case StatusSuccess:
// 判断是否有登录回调,如果有执行它 // 判断是否有登录回调,如果有执行它
if b.LoginCallBack != nil { if b.LoginCallBack != nil {
b.LoginCallBack(resp.Raw) b.LoginCallBack(resp.Raw)
} }
return b.handleLogin(resp.Raw) return b.HandleLogin(resp.Raw)
case statusScanned: case StatusScanned:
// 执行扫码回调 // 执行扫码回调
if b.ScanCallBack != nil { if b.ScanCallBack != nil {
b.ScanCallBack(resp.Raw) b.ScanCallBack(resp.Raw)
} }
case statusTimeout: case StatusTimeout:
return errors.New("login time out") return errors.New("login time out")
case statusWait: case StatusWait:
continue continue
} }
} }
@ -153,8 +153,8 @@ func (b *Bot) Logout() error {
return errors.New("user not login") return errors.New("user not login")
} }
// 登录逻辑 // HandleLogin 登录逻辑
func (b *Bot) handleLogin(data []byte) error { func (b *Bot) HandleLogin(data []byte) error {
// 获取登录的一些基本的信息 // 获取登录的一些基本的信息
info, err := b.Caller.GetLoginInfo(data) info, err := b.Caller.GetLoginInfo(data)
if err != nil { if err != nil {
@ -175,17 +175,17 @@ func (b *Bot) handleLogin(data []byte) error {
b.storage.Request = request b.storage.Request = request
// 如果是热登陆,则将当前的重要信息写入hotReloadStorage // 如果是热登陆,则将当前的重要信息写入hotReloadStorage
if b.isHot { if b.IsHot {
if err = b.DumpHotReloadStorage(); err != nil { if err = b.DumpHotReloadStorage(); err != nil {
return err return err
} }
} }
return b.webInit() return b.WebInit()
} }
// 根据有效凭证获取和初始化用户信息 // WebInit 根据有效凭证获取和初始化用户信息
func (b *Bot) webInit() error { func (b *Bot) WebInit() error {
req := b.storage.Request req := b.storage.Request
info := b.storage.LoginInfo info := b.storage.LoginInfo
// 获取初始化的用户信息和一些必要的参数 // 获取初始化的用户信息和一些必要的参数
@ -297,8 +297,8 @@ func (b *Bot) MessageOnError(h func(err error)) {
// DumpHotReloadStorage 写入HotReloadStorage // DumpHotReloadStorage 写入HotReloadStorage
func (b *Bot) DumpHotReloadStorage() error { func (b *Bot) DumpHotReloadStorage() error {
if b.hotReloadStorage == nil { if b.HotReloadStorage == nil {
return errors.New("hotReloadStorage can be nil") return errors.New("HotReloadStorage can be nil")
} }
cookies := b.Caller.Client.GetCookieMap() cookies := b.Caller.Client.GetCookieMap()
item := HotReloadStorageItem{ item := HotReloadStorageItem{
@ -307,7 +307,7 @@ func (b *Bot) DumpHotReloadStorage() error {
LoginInfo: b.storage.LoginInfo, LoginInfo: b.storage.LoginInfo,
WechatDomain: b.Caller.Client.domain, WechatDomain: b.Caller.Client.domain,
} }
return b.hotReloadStorage.Dump(item) return b.HotReloadStorage.Dump(item)
} }
// OnLogin is a setter for LoginCallBack // OnLogin is a setter for LoginCallBack

View File

@ -87,10 +87,10 @@ const (
// 登录状态 // 登录状态
const ( const (
statusSuccess = "200" StatusSuccess = "200"
statusScanned = "201" StatusScanned = "201"
statusTimeout = "400" StatusTimeout = "400"
statusWait = "408" StatusWait = "408"
) )
// errors // errors