修改HotReloadStorage接口定义

This commit is contained in:
eatmoreapple 2021-08-01 13:54:21 +08:00
commit 21c58b700e
3 changed files with 132 additions and 23 deletions

62
appmessagetype_string.go Normal file
View File

@ -0,0 +1,62 @@
// Code generated by "stringer -type=AppMessageType -linecomment=true"; DO NOT EDIT.
package openwechat
import "strconv"
func _() {
// An "invalid array index" compiler error signifies that the constant values have changed.
// Re-run the stringer command to generate them again.
var x [1]struct{}
_ = x[AppMsgTypeText-1]
_ = x[AppMsgTypeImg-2]
_ = x[AppMsgTypeAudio-3]
_ = x[AppMsgTypeVideo-4]
_ = x[AppMsgTypeUrl-5]
_ = x[AppMsgTypeAttach-6]
_ = x[AppMsgTypeOpen-7]
_ = x[AppMsgTypeEmoji-8]
_ = x[AppMsgTypeVoiceRemind-9]
_ = x[AppMsgTypeScanGood-10]
_ = x[AppMsgTypeGood-13]
_ = x[AppMsgTypeEmotion-15]
_ = x[AppMsgTypeCardTicket-16]
_ = x[AppMsgTypeRealtimeShareLocation-17]
_ = x[AppMsgTypeTransfers-2000]
_ = x[AppMsgTypeRedEnvelopes-2001]
_ = x[AppMsgTypeReaderType-100001]
}
const (
_AppMessageType_name_0 = "文本消息图片消息语音消息视频消息文章消息附件消息Open表情消息VoiceRemindScanGood"
_AppMessageType_name_1 = "Good"
_AppMessageType_name_2 = "Emotion名片消息地理位置消息"
_AppMessageType_name_3 = "转账消息红包消息"
_AppMessageType_name_4 = "自定义的消息"
)
var (
_AppMessageType_index_0 = [...]uint8{0, 12, 24, 36, 48, 60, 72, 76, 88, 99, 107}
_AppMessageType_index_2 = [...]uint8{0, 7, 19, 37}
_AppMessageType_index_3 = [...]uint8{0, 12, 24}
)
func (i AppMessageType) String() string {
switch {
case 1 <= i && i <= 10:
i -= 1
return _AppMessageType_name_0[_AppMessageType_index_0[i]:_AppMessageType_index_0[i+1]]
case i == 13:
return _AppMessageType_name_1
case 15 <= i && i <= 17:
i -= 15
return _AppMessageType_name_2[_AppMessageType_index_2[i]:_AppMessageType_index_2[i+1]]
case 2000 <= i && i <= 2001:
i -= 2000
return _AppMessageType_name_3[_AppMessageType_index_3[i]:_AppMessageType_index_3[i+1]]
case i == 100001:
return _AppMessageType_name_4
default:
return "AppMessageType(" + strconv.FormatInt(int64(i), 10) + ")"
}
}

58
bot.go
View File

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

View File

@ -44,7 +44,11 @@ const (
// 详见 message_test.go
type MessageType int
// AppMessageType以Go惯用形式定义了PC微信所有的官方App消息类型。
type AppMessageType int
//go:generate stringer -type=MessageType -linecomment=true
//go:generate stringer -type=AppMessageType -linecomment=true
// https://res.wx.qq.com/a/wx_fed/webwx/res/static/js/index_c7d281c.js
// MSGTYPE_TEXT
@ -63,8 +67,6 @@ type MessageType int
// MSGTYPE_MICROVIDEO
// MSGTYPE_SYS
// MSGTYPE_RECALLED
// varcaser.Caser{
// From: varcaser.ScreamingSnakeCase, To: varcaser.UpperCamelCaseKeepCaps}
const (
MsgTypeText MessageType = 1 // 文本消息
@ -85,12 +87,33 @@ const (
MsgTypeRecalled MessageType = 10002 // 消息撤回
)
const (
AppMsgTypeText AppMessageType = 1 // 文本消息
AppMsgTypeImg AppMessageType = 2 // 图片消息
AppMsgTypeAudio AppMessageType = 3 // 语音消息
AppMsgTypeVideo AppMessageType = 4 // 视频消息
AppMsgTypeUrl AppMessageType = 5 // 文章消息
AppMsgTypeAttach AppMessageType = 6 // 附件消息
AppMsgTypeOpen AppMessageType = 7 // Open
AppMsgTypeEmoji AppMessageType = 8 // 表情消息
AppMsgTypeVoiceRemind AppMessageType = 9 // VoiceRemind
AppMsgTypeScanGood AppMessageType = 10 // ScanGood
AppMsgTypeGood AppMessageType = 13 // Good
AppMsgTypeEmotion AppMessageType = 15 // Emotion
AppMsgTypeCardTicket AppMessageType = 16 // 名片消息
AppMsgTypeRealtimeShareLocation AppMessageType = 17 // 地理位置消息
AppMsgTypeTransfers AppMessageType = 2000 // 转账消息
AppMsgTypeRedEnvelopes AppMessageType = 2001 // 红包消息
AppMsgTypeReaderType AppMessageType = 100001 //自定义的消息
)
// 登录状态
const (
statusSuccess = "200"
statusScanned = "201"
statusTimeout = "400"
statusWait = "408"
StatusSuccess = "200"
StatusScanned = "201"
StatusTimeout = "400"
StatusWait = "408"
)
// errors