From 3cff50f83b356029809fab62ff49462e01c56753 Mon Sep 17 00:00:00 2001 From: eatmoreapple <15055461510@163.com> Date: Sat, 23 Oct 2021 10:24:48 +0800 Subject: [PATCH] =?UTF-8?q?:pencli2:=20=E4=BF=AE=E6=94=B9mode=E8=A1=8C?= =?UTF-8?q?=E4=B8=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot.go | 2 +- caller.go | 2 +- client.go | 38 ++++++++------------------------ mode.go | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ url.go | 16 +++++++------- 5 files changed, 85 insertions(+), 39 deletions(-) create mode 100644 mode.go diff --git a/bot.go b/bot.go index ecc584d..81669a6 100644 --- a/bot.go +++ b/bot.go @@ -360,7 +360,7 @@ func NewBot() *Bot { // DefaultBot 默认的Bot的构造方法, // mode不传入默认为openwechat.Normal,详情见mode // bot := openwechat.DefaultBot(openwechat.Desktop) -func DefaultBot(modes ...mode) *Bot { +func DefaultBot(modes ...Mode) *Bot { bot := NewBot() if len(modes) > 0 { bot.Caller.Client.SetMode(modes[0]) diff --git a/caller.go b/caller.go index 15991df..1383d1e 100644 --- a/caller.go +++ b/caller.go @@ -402,7 +402,7 @@ func (c *Caller) WebWxRenameChatRoom(request *BaseRequest, info *LoginInfo, newT } // SetMode 设置Client的模式 -func (c *Client) SetMode(mode mode) { +func (c *Client) SetMode(mode Mode) { c.mode = mode } diff --git a/client.go b/client.go index 8694217..14274e8 100644 --- a/client.go +++ b/client.go @@ -41,7 +41,7 @@ type Client struct { HttpHooks HttpHooks *http.Client Domain WechatDomain - mode mode + mode Mode mu sync.Mutex cookies map[string][]*http.Cookie } @@ -59,7 +59,7 @@ func DefaultClient() *Client { CheckRedirect: func(req *http.Request, via []*http.Request) error { return http.ErrUseLastResponse }, - Jar: jar, + Jar: jar, Timeout: timeout, } c := NewClient(client) @@ -113,22 +113,7 @@ func (c *Client) GetCookieMap() map[string][]*http.Cookie { // GetLoginUUID 获取登录的uuid func (c *Client) GetLoginUUID() (*http.Response, error) { - path, _ := url.Parse(jslogin) - 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) + return c.mode.GetLoginUUID(c) } // GetLoginQrcode 获取登录的二维吗 @@ -154,12 +139,7 @@ func (c *Client) CheckLogin(uuid string) (*http.Response, error) { // GetLoginInfo 请求获取LoginInfo func (c *Client) GetLoginInfo(path string) (*http.Response, error) { - req, _ := http.NewRequest(http.MethodGet, path, nil) - if c.mode == Desktop { - req.Header.Add("client-version", uosPatchClientVersion) - req.Header.Add("extspam", uosPatchExtspam) - } - return c.Do(req) + return c.mode.GetLoginInfo(c, path) } // WebInit 请求获取初始化信息 @@ -203,12 +183,12 @@ func (c *Client) WebWxStatusNotify(request *BaseRequest, response *WebInitRespon func (c *Client) SyncCheck(request *BaseRequest, info *LoginInfo, response *WebInitResponse) (*http.Response, error) { path, _ := url.Parse(c.Domain.SyncHost() + synccheck) params := url.Values{} - params.Add("r", strconv.FormatInt(time.Now().UnixNano() / 1e6, 10)) + params.Add("r", strconv.FormatInt(time.Now().UnixNano()/1e6, 10)) params.Add("skey", info.SKey) params.Add("sid", info.WxSid) params.Add("uin", strconv.FormatInt(info.WxUin, 10)) params.Add("deviceid", request.DeviceID) - params.Add("_", strconv.FormatInt(time.Now().UnixNano() / 1e6, 10)) + params.Add("_", strconv.FormatInt(time.Now().UnixNano()/1e6, 10)) var syncKeyStringSlice = make([]string, response.SyncKey.Count) // 将SyncKey里面的元素按照特定的格式拼接起来 for index, item := range response.SyncKey.List { @@ -226,7 +206,7 @@ func (c *Client) SyncCheck(request *BaseRequest, info *LoginInfo, response *WebI func (c *Client) WebWxGetContact(info *LoginInfo) (*http.Response, error) { path, _ := url.Parse(c.Domain.BaseHost() + webwxgetcontact) params := url.Values{} - params.Add("r", strconv.FormatInt(time.Now().UnixNano() / 1e6, 10)) + params.Add("r", strconv.FormatInt(time.Now().UnixNano()/1e6, 10)) params.Add("skey", info.SKey) params.Add("req", "0") path.RawQuery = params.Encode() @@ -239,7 +219,7 @@ func (c *Client) WebWxBatchGetContact(members Members, request *BaseRequest) (*h path, _ := url.Parse(c.Domain.BaseHost() + webwxbatchgetcontact) params := url.Values{} params.Add("type", "ex") - params.Add("r", strconv.FormatInt(time.Now().UnixNano() / 1e6, 10)) + params.Add("r", strconv.FormatInt(time.Now().UnixNano()/1e6, 10)) path.RawQuery = params.Encode() list := NewUserDetailItemList(members) content := map[string]interface{}{ @@ -511,7 +491,7 @@ func (c *Client) WebWxVerifyUser(storage *Storage, info RecommendInfo, verifyCon loginInfo := storage.LoginInfo path, _ := url.Parse(c.Domain.BaseHost() + webwxverifyuser) params := url.Values{} - params.Add("r", strconv.FormatInt(time.Now().UnixNano() / 1e6, 10)) + params.Add("r", strconv.FormatInt(time.Now().UnixNano()/1e6, 10)) params.Add("lang", "zh_CN") params.Add("pass_ticket", loginInfo.PassTicket) path.RawQuery = params.Encode() diff --git a/mode.go b/mode.go new file mode 100644 index 0000000..54abf5e --- /dev/null +++ b/mode.go @@ -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) +} diff --git a/url.go b/url.go index 133ffe9..f4ad9e7 100644 --- a/url.go +++ b/url.go @@ -1,13 +1,13 @@ package openwechat -// mode 类型限制 -type mode string - -// 向外暴露2种模式 -const ( - Normal mode = "normal" - Desktop mode = "desktop" // 突破网页版登录限制 -) +//// mode 类型限制 +//type mode string +// +//// 向外暴露2种模式 +//const ( +// Normal mode = "normal" +// Desktop mode = "desktop" // 突破网页版登录限制 +//) const ( webwxinit = "/cgi-bin/mmwebwx-bin/webwxinit"