:pencli2: 修改mode行为

This commit is contained in:
eatmoreapple 2021-10-23 10:24:48 +08:00
parent 58fd3a752c
commit 3cff50f83b
5 changed files with 85 additions and 39 deletions

2
bot.go
View File

@ -360,7 +360,7 @@ func NewBot() *Bot {
// DefaultBot 默认的Bot的构造方法, // DefaultBot 默认的Bot的构造方法,
// mode不传入默认为openwechat.Normal,详情见mode // mode不传入默认为openwechat.Normal,详情见mode
// bot := openwechat.DefaultBot(openwechat.Desktop) // bot := openwechat.DefaultBot(openwechat.Desktop)
func DefaultBot(modes ...mode) *Bot { func DefaultBot(modes ...Mode) *Bot {
bot := NewBot() bot := NewBot()
if len(modes) > 0 { if len(modes) > 0 {
bot.Caller.Client.SetMode(modes[0]) bot.Caller.Client.SetMode(modes[0])

View File

@ -402,7 +402,7 @@ func (c *Caller) WebWxRenameChatRoom(request *BaseRequest, info *LoginInfo, newT
} }
// SetMode 设置Client的模式 // SetMode 设置Client的模式
func (c *Client) SetMode(mode mode) { func (c *Client) SetMode(mode Mode) {
c.mode = mode c.mode = mode
} }

View File

@ -41,7 +41,7 @@ type Client struct {
HttpHooks HttpHooks HttpHooks HttpHooks
*http.Client *http.Client
Domain WechatDomain Domain WechatDomain
mode mode mode Mode
mu sync.Mutex mu sync.Mutex
cookies map[string][]*http.Cookie cookies map[string][]*http.Cookie
} }
@ -113,22 +113,7 @@ func (c *Client) GetCookieMap() map[string][]*http.Cookie {
// GetLoginUUID 获取登录的uuid // GetLoginUUID 获取登录的uuid
func (c *Client) GetLoginUUID() (*http.Response, error) { func (c *Client) GetLoginUUID() (*http.Response, error) {
path, _ := url.Parse(jslogin) return c.mode.GetLoginUUID(c)
params := url.Values{}
redirectUrl, _ := url.Parse(webwxnewloginpage)
if c.mode == Desktop {
p := url.Values{"mod": {"desktop"}}
redirectUrl.RawQuery = p.Encode()
}
params.Add("redirect_uri", redirectUrl.String())
params.Add("appid", appId)
params.Add("fun", "new")
params.Add("lang", "zh_CN")
params.Add("_", strconv.FormatInt(time.Now().UnixNano() / 1e6, 10))
path.RawQuery = params.Encode()
req, _ := http.NewRequest(http.MethodGet, path.String(), nil)
return c.Do(req)
} }
// GetLoginQrcode 获取登录的二维吗 // GetLoginQrcode 获取登录的二维吗
@ -154,12 +139,7 @@ func (c *Client) CheckLogin(uuid string) (*http.Response, error) {
// GetLoginInfo 请求获取LoginInfo // GetLoginInfo 请求获取LoginInfo
func (c *Client) GetLoginInfo(path string) (*http.Response, error) { func (c *Client) GetLoginInfo(path string) (*http.Response, error) {
req, _ := http.NewRequest(http.MethodGet, path, nil) return c.mode.GetLoginInfo(c, path)
if c.mode == Desktop {
req.Header.Add("client-version", uosPatchClientVersion)
req.Header.Add("extspam", uosPatchExtspam)
}
return c.Do(req)
} }
// WebInit 请求获取初始化信息 // WebInit 请求获取初始化信息

66
mode.go Normal file
View File

@ -0,0 +1,66 @@
package openwechat
import (
"net/http"
"net/url"
"strconv"
"time"
)
type Mode interface {
GetLoginUUID(client *Client) (*http.Response, error)
GetLoginInfo(client *Client, path string) (*http.Response, error)
}
var (
Normal Mode = normalMode{}
Desktop Mode = desktopMode{}
)
type normalMode struct{}
func (n normalMode) GetLoginUUID(client *Client) (*http.Response, error) {
path, _ := url.Parse(jslogin)
params := url.Values{}
redirectUrl, _ := url.Parse(webwxnewloginpage)
params.Add("redirect_uri", redirectUrl.String())
params.Add("appid", appId)
params.Add("fun", "new")
params.Add("lang", "zh_CN")
params.Add("_", strconv.FormatInt(time.Now().UnixNano()/1e6, 10))
path.RawQuery = params.Encode()
req, _ := http.NewRequest(http.MethodGet, path.String(), nil)
return client.Do(req)
}
func (n normalMode) GetLoginInfo(client *Client, path string) (*http.Response, error) {
req, _ := http.NewRequest(http.MethodGet, path, nil)
return client.Do(req)
}
type desktopMode struct{}
func (n desktopMode) GetLoginUUID(client *Client) (*http.Response, error) {
path, _ := url.Parse(jslogin)
params := url.Values{}
redirectUrl, _ := url.Parse(webwxnewloginpage)
p := url.Values{"mod": {"desktop"}}
redirectUrl.RawQuery = p.Encode()
params.Add("redirect_uri", redirectUrl.String())
params.Add("appid", appId)
params.Add("fun", "new")
params.Add("lang", "zh_CN")
params.Add("_", strconv.FormatInt(time.Now().UnixNano()/1e6, 10))
path.RawQuery = params.Encode()
req, _ := http.NewRequest(http.MethodGet, path.String(), nil)
return client.Do(req)
}
func (n desktopMode) GetLoginInfo(client *Client, path string) (*http.Response, error) {
req, _ := http.NewRequest(http.MethodGet, path, nil)
req.Header.Add("client-version", uosPatchClientVersion)
req.Header.Add("extspam", uosPatchExtspam)
return client.Do(req)
}

16
url.go
View File

@ -1,13 +1,13 @@
package openwechat package openwechat
// mode 类型限制 //// mode 类型限制
type mode string //type mode string
//
// 向外暴露2种模式 //// 向外暴露2种模式
const ( //const (
Normal mode = "normal" // Normal mode = "normal"
Desktop mode = "desktop" // 突破网页版登录限制 // Desktop mode = "desktop" // 突破网页版登录限制
) //)
const ( const (
webwxinit = "/cgi-bin/mmwebwx-bin/webwxinit" webwxinit = "/cgi-bin/mmwebwx-bin/webwxinit"