Skip to content

Commit

Permalink
update mode behaviors (#487)
Browse files Browse the repository at this point in the history
  • Loading branch information
eatmoreapple authored Apr 30, 2024
1 parent 940132f commit 20cc8b8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 41 deletions.
18 changes: 15 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,11 @@ func (c *Client) HTTPClient() *http.Client {

// GetLoginUUID 获取登录的uuid
func (c *Client) GetLoginUUID(ctx context.Context) (*http.Response, error) {
return c.mode.GetLoginUUID(ctx, c)
req, err := c.mode.BuildGetLoginUUIDRequest(ctx)
if err != nil {
return nil, err
}
return c.Do(req)
}

// GetLoginQrcode 获取登录的二维吗
Expand Down Expand Up @@ -221,7 +225,11 @@ func (c *Client) CheckLogin(ctx context.Context, uuid, tip string) (*http.Respon

// GetLoginInfo 请求获取LoginInfo
func (c *Client) GetLoginInfo(ctx context.Context, path *url.URL) (*http.Response, error) {
return c.mode.GetLoginInfo(ctx, c, path.String())
req, err := c.mode.BuildGetLoginInfoRequest(ctx, path.String())
if err != nil {
return nil, err
}
return c.Do(req)
}

// WebInit 请求获取初始化信息
Expand Down Expand Up @@ -1057,7 +1065,11 @@ func (c *Client) WebWxRelationPin(ctx context.Context, opt *ClientWebWxRelationP

// WebWxPushLogin 免扫码登陆接口
func (c *Client) WebWxPushLogin(ctx context.Context, uin int64) (*http.Response, error) {
return c.mode.PushLogin(ctx, c, uin)
req, err := c.mode.BuildPushLoginRequest(ctx, c.Domain.BaseHost(), uin)
if err != nil {
return nil, err
}
return c.Do(req)
}

// WebWxSendVideoMsg 发送视频消息接口
Expand Down
69 changes: 31 additions & 38 deletions mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
)

type Mode interface {
GetLoginUUID(ctx context.Context, client *Client) (*http.Response, error)
GetLoginInfo(ctx context.Context, client *Client, path string) (*http.Response, error)
PushLogin(ctx context.Context, client *Client, uin int64) (*http.Response, error)
BuildGetLoginUUIDRequest(ctx context.Context) (*http.Request, error)
BuildGetLoginInfoRequest(ctx context.Context, path string) (*http.Request, error)
BuildPushLoginRequest(ctx context.Context, host string, uin int64) (*http.Request, error)
}

var (
Expand All @@ -24,22 +24,7 @@ var (

type normalMode struct{}

func (n normalMode) PushLogin(ctx context.Context, client *Client, uin int64) (*http.Response, error) {
path, err := url.Parse(client.Domain.BaseHost() + webwxpushloginurl)
if err != nil {
return nil, err
}
params := url.Values{}
params.Add("uin", strconv.FormatInt(uin, 10))
path.RawQuery = params.Encode()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, path.String(), nil)
if err != nil {
return nil, err
}
return client.Do(req)
}

func (n normalMode) GetLoginUUID(ctx context.Context, client *Client) (*http.Response, error) {
func (n normalMode) BuildGetLoginUUIDRequest(ctx context.Context) (*http.Request, error) {
path, err := url.Parse(jslogin)
if err != nil {
return nil, err
Expand All @@ -55,25 +40,42 @@ func (n normalMode) GetLoginUUID(ctx context.Context, client *Client) (*http.Res
params.Add("lang", "zh_CN")
params.Add("_", strconv.FormatInt(time.Now().UnixNano()/1e6, 10))
path.RawQuery = params.Encode()
return http.NewRequestWithContext(ctx, http.MethodGet, path.String(), nil)
}

func (n normalMode) BuildGetLoginInfoRequest(ctx context.Context, path string) (*http.Request, error) {
return http.NewRequestWithContext(ctx, http.MethodGet, path, nil)
}

func (n normalMode) PushLogin(ctx context.Context, client *Client, uin int64) (*http.Response, error) {
path, err := url.Parse(client.Domain.BaseHost() + webwxpushloginurl)
if err != nil {
return nil, err
}
params := url.Values{}
params.Add("uin", strconv.FormatInt(uin, 10))
path.RawQuery = params.Encode()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, path.String(), nil)
if err != nil {
return nil, err
}
return client.Do(req)
}

func (n normalMode) GetLoginInfo(ctx context.Context, client *Client, path string) (*http.Response, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, path, nil)
func (n normalMode) BuildPushLoginRequest(ctx context.Context, host string, uin int64) (*http.Request, error) {
path, err := url.Parse(host + webwxpushloginurl)
if err != nil {
return nil, err
}
return client.Do(req)
params := url.Values{}
params.Add("uin", strconv.FormatInt(uin, 10))
path.RawQuery = params.Encode()
return http.NewRequestWithContext(ctx, http.MethodGet, path.String(), nil)
}

type desktopMode struct{}

func (n desktopMode) GetLoginUUID(ctx context.Context, client *Client) (*http.Response, error) {
func (n desktopMode) BuildGetLoginUUIDRequest(ctx context.Context) (*http.Request, error) {
path, err := url.Parse(jslogin)
if err != nil {
return nil, err
Expand All @@ -91,36 +93,27 @@ func (n desktopMode) GetLoginUUID(ctx context.Context, client *Client) (*http.Re
params.Add("lang", "zh_CN")
params.Add("_", strconv.FormatInt(time.Now().UnixNano()/1e6, 10))
path.RawQuery = params.Encode()

req, err := http.NewRequestWithContext(ctx, http.MethodGet, path.String(), nil)
if err != nil {
return nil, err
}
return client.Do(req)
return http.NewRequestWithContext(ctx, http.MethodGet, path.String(), nil)
}

func (n desktopMode) GetLoginInfo(ctx context.Context, client *Client, path string) (*http.Response, error) {
func (n desktopMode) BuildGetLoginInfoRequest(ctx context.Context, path string) (*http.Request, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, err
}
req.Header.Add("client-version", uosPatchClientVersion)
req.Header.Add("extspam", uosPatchExtspam)
return client.Do(req)
return req, nil
}

func (n desktopMode) PushLogin(ctx context.Context, client *Client, uin int64) (*http.Response, error) {
path, err := url.Parse(client.Domain.BaseHost() + webwxpushloginurl)
func (n desktopMode) BuildPushLoginRequest(ctx context.Context, host string, uin int64) (*http.Request, error) {
path, err := url.Parse(host + webwxpushloginurl)
if err != nil {
return nil, err
}
params := url.Values{}
params.Add("uin", strconv.FormatInt(uin, 10))
params.Add("mod", "desktop")
path.RawQuery = params.Encode()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, path.String(), nil)
if err != nil {
return nil, err
}
return client.Do(req)
return http.NewRequestWithContext(ctx, http.MethodGet, path.String(), nil)
}

0 comments on commit 20cc8b8

Please sign in to comment.