Skip to content

Commit

Permalink
[update]: 更新微信登录 v2 版本 (#282)
Browse files Browse the repository at this point in the history
  • Loading branch information
eatmoreapple authored Feb 20, 2023
1 parent d514d88 commit ea1e987
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
9 changes: 3 additions & 6 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"net/url"
"os/exec"
"runtime"
"sync"
)

type Bot struct {
Expand All @@ -22,7 +21,6 @@ type Bot struct {
Serializer Serializer // 序列化器, 默认为json
Storage *Storage
Caller *Caller
once sync.Once
err error
context context.Context
cancel context.CancelFunc
Expand Down Expand Up @@ -184,8 +182,7 @@ func (b *Bot) WebInit() error {
}
// 开启协程,轮询获取是否有新的消息返回

// FIX: 当bot在线的情况下执行热登录,会开启多次事件监听
go b.once.Do(func() {
go func() {
if b.MessageErrorHandler == nil {
b.MessageErrorHandler = defaultSyncCheckErrHandler(b)
}
Expand All @@ -201,7 +198,7 @@ func (b *Bot) WebInit() error {
break
}
}
})
}()
return nil
}

Expand Down Expand Up @@ -293,7 +290,7 @@ func (b *Bot) Block() error {
return errors.New("`Block` must be called after user login")
}
<-b.Context().Done()
return nil
return b.CrashReason()
}

// Exit 主动退出,让 Block 不在阻塞
Expand Down
16 changes: 11 additions & 5 deletions caller.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"io"
"net/http"
"net/url"
"os"
)
Expand Down Expand Up @@ -67,16 +66,23 @@ func (c *Caller) CheckLogin(uuid, tip string) (CheckLoginResponse, error) {
// GetLoginInfo 获取登录信息
func (c *Caller) GetLoginInfo(path *url.URL) (*LoginInfo, error) {
// 从响应体里面获取需要跳转的url
query := path.Query()
query.Set("version", "v2")
path.RawQuery = query.Encode()
resp, err := c.Client.GetLoginInfo(path)
if err != nil {
return nil, err
}
// 判断是否重定向
if resp.StatusCode != http.StatusMovedPermanently {
return nil, fmt.Errorf("%w: try to login with Desktop Mode", ErrForbidden)
}
// 微信 v2 版本修复了301 response missing Location header 的问题
defer func() { _ = resp.Body.Close() }()

if _, exists := CookieGroup(resp.Cookies()).GetByName("wxuin"); !exists {
err = ErrForbidden
if c.Client.mode != desktop {
err = fmt.Errorf("%w: try to login with desktop mode", err)
}
return nil, err
}
var loginInfo LoginInfo
// xml结构体序列化储存
if err := scanXml(resp.Body, &loginInfo); err != nil {
Expand Down

0 comments on commit ea1e987

Please sign in to comment.