From 8b7d66a534d14d8246b7753a40d78d0f557147d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AF=BB=E6=AC=A2?= Date: Tue, 20 Sep 2022 11:25:02 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E6=96=B0=E5=A2=9E=E8=87=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E8=AE=BE=E5=A4=87Id=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot.go | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/bot.go b/bot.go index af5cb75..f829f6f 100644 --- a/bot.go +++ b/bot.go @@ -29,6 +29,7 @@ type Bot struct { Storage *Storage HotReloadStorage HotReloadStorage uuid string + deviceId string // 设备Id } // Alive 判断当前用户是否正常在线 @@ -44,12 +45,21 @@ func (b *Bot) Alive() bool { } } +// SetDeviceId +// @description: 设置设备Id +// @receiver b +// @param deviceId +func (b *Bot) SetDeviceId(deviceId string) { + b.deviceId = deviceId +} + // GetCurrentUser 获取当前的用户 -// self, err := bot.GetCurrentUser() -// if err != nil { -// return -// } -// fmt.Println(self.NickName) +// +// self, err := bot.GetCurrentUser() +// if err != nil { +// return +// } +// fmt.Println(self.NickName) func (b *Bot) GetCurrentUser() (*Self, error) { if b.self == nil { return nil, errors.New("user not login") @@ -59,9 +69,10 @@ func (b *Bot) GetCurrentUser() (*Self, error) { // HotLogin 热登录,可实现重复登录, // retry设置为true可在热登录失效后进行普通登录行为 -// Storage := NewJsonFileHotReloadStorage("Storage.json") -// err := bot.HotLogin(Storage, true) -// fmt.Println(err) +// +// Storage := NewJsonFileHotReloadStorage("Storage.json") +// err := bot.HotLogin(Storage, true) +// fmt.Println(err) func (b *Bot) HotLogin(storage HotReloadStorage, retry ...bool) error { b.isHot = true b.HotReloadStorage = storage @@ -172,12 +183,17 @@ func (b *Bot) HandleLogin(data []byte) error { // 将LoginInfo存到storage里面 b.Storage.LoginInfo = info + // 处理设备Id + if b.deviceId == "" { + b.deviceId = GetRandomDeviceId() + } + // 构建BaseRequest request := &BaseRequest{ Uin: info.WxUin, Sid: info.WxSid, Skey: info.SKey, - DeviceID: GetRandomDeviceId(), + DeviceID: b.deviceId, } // 将BaseRequest存到storage里面方便后续调用 @@ -374,7 +390,8 @@ func NewBot() *Bot { // DefaultBot 默认的Bot的构造方法, // mode不传入默认为 openwechat.Desktop,详情见mode -// bot := openwechat.DefaultBot(openwechat.Desktop) +// +// bot := openwechat.DefaultBot(openwechat.Desktop) func DefaultBot(modes ...Mode) *Bot { bot := NewBot() if len(modes) > 0 {