[feat]: 支持用户自定义热存储数据的序列化和反序列化 (#222)
This commit is contained in:
parent
e9c89f9ac8
commit
d77bb0a4cb
19
bot.go
19
bot.go
@ -2,7 +2,6 @@ package openwechat
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
@ -20,13 +19,14 @@ type Bot struct {
|
|||||||
SyncCheckCallback func(resp SyncCheckResponse) // 心跳回调
|
SyncCheckCallback func(resp SyncCheckResponse) // 心跳回调
|
||||||
MessageHandler MessageHandler // 获取消息成功的handle
|
MessageHandler MessageHandler // 获取消息成功的handle
|
||||||
MessageErrorHandler func(err error) bool // 获取消息发生错误的handle, 返回true则尝试继续监听
|
MessageErrorHandler func(err error) bool // 获取消息发生错误的handle, 返回true则尝试继续监听
|
||||||
|
Serializer Serializer // 序列化器, 默认为json
|
||||||
|
Storage *Storage
|
||||||
|
Caller *Caller
|
||||||
once sync.Once
|
once sync.Once
|
||||||
err error
|
err error
|
||||||
context context.Context
|
context context.Context
|
||||||
cancel context.CancelFunc
|
cancel context.CancelFunc
|
||||||
Caller *Caller
|
|
||||||
self *Self
|
self *Self
|
||||||
Storage *Storage
|
|
||||||
hotReloadStorage HotReloadStorage
|
hotReloadStorage HotReloadStorage
|
||||||
uuid string
|
uuid string
|
||||||
loginUUID *string
|
loginUUID *string
|
||||||
@ -296,7 +296,7 @@ func (b *Bot) DumpTo(writer io.Writer) error {
|
|||||||
WechatDomain: b.Caller.Client.Domain,
|
WechatDomain: b.Caller.Client.Domain,
|
||||||
UUID: b.uuid,
|
UUID: b.uuid,
|
||||||
}
|
}
|
||||||
return json.NewEncoder(writer).Encode(item)
|
return b.Serializer.Encode(writer, item)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsHot returns true if is hot login otherwise false
|
// IsHot returns true if is hot login otherwise false
|
||||||
@ -328,8 +328,7 @@ func (b *Bot) reload() error {
|
|||||||
return errors.New("hotReloadStorage is nil")
|
return errors.New("hotReloadStorage is nil")
|
||||||
}
|
}
|
||||||
var item HotReloadStorageItem
|
var item HotReloadStorageItem
|
||||||
err := json.NewDecoder(b.hotReloadStorage).Decode(&item)
|
if err := b.Serializer.Decode(b.hotReloadStorage, &item); err != nil {
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
b.Caller.Client.SetCookieJar(item.Jar)
|
b.Caller.Client.SetCookieJar(item.Jar)
|
||||||
@ -347,7 +346,13 @@ func NewBot(c context.Context) *Bot {
|
|||||||
// 默认行为为网页版微信模式
|
// 默认行为为网页版微信模式
|
||||||
caller.Client.SetMode(normal)
|
caller.Client.SetMode(normal)
|
||||||
ctx, cancel := context.WithCancel(c)
|
ctx, cancel := context.WithCancel(c)
|
||||||
return &Bot{Caller: caller, Storage: &Storage{}, context: ctx, cancel: cancel}
|
return &Bot{
|
||||||
|
Caller: caller,
|
||||||
|
Storage: &Storage{},
|
||||||
|
Serializer: &JsonSerializer{},
|
||||||
|
context: ctx,
|
||||||
|
cancel: cancel,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// DefaultBot 默认的Bot的构造方法,
|
// DefaultBot 默认的Bot的构造方法,
|
||||||
|
25
serializer.go
Normal file
25
serializer.go
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
package openwechat
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Serializer is an interface for encoding and decoding data.
|
||||||
|
type Serializer interface {
|
||||||
|
Encode(writer io.Writer, v interface{}) error
|
||||||
|
Decode(reader io.Reader, v interface{}) error
|
||||||
|
}
|
||||||
|
|
||||||
|
// JsonSerializer is a serializer for json.
|
||||||
|
type JsonSerializer struct{}
|
||||||
|
|
||||||
|
// Encode encodes v to writer.
|
||||||
|
func (j JsonSerializer) Encode(writer io.Writer, v interface{}) error {
|
||||||
|
return json.NewEncoder(writer).Encode(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decode decodes data from reader to v.
|
||||||
|
func (j JsonSerializer) Decode(reader io.Reader, v interface{}) error {
|
||||||
|
return json.NewDecoder(reader).Decode(v)
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user