From 97e1af59040bde76b270fe311020b7082c8c31e8 Mon Sep 17 00:00:00 2001 From: oscar Date: Tue, 2 Jan 2024 17:19:59 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=94=AF=E6=8C=81=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E5=85=A8=E5=B1=80HTTPClient=20(#761)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [新增]支持设置httpClient * 增加说明 * 增加说明 * 格式化imports * [fix]1.21.5 和golangci 有兼容性问题 * [fix]优化imports --- .github/workflows/go.yml | 2 +- util/http.go | 24 +++++++++++++----------- wechat.go | 7 +++++++ 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index c66040b..9d56eb5 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -10,7 +10,7 @@ jobs: golangci: strategy: matrix: - go-version: [ '1.16','1.17','1.18','1.19','1.20','1.21' ] + go-version: [ '1.16','1.17','1.18','1.19','1.20','1.21.4' ] name: golangci-lint runs-on: ubuntu-latest steps: diff --git a/util/http.go b/util/http.go index b364774..26e4a7b 100644 --- a/util/http.go +++ b/util/http.go @@ -22,6 +22,9 @@ type URIModifier func(uri string) string var uriModifier URIModifier +// DefaultHTTPClient 默认httpClient +var DefaultHTTPClient = http.DefaultClient + // SetURIModifier 设置URI修改器 func SetURIModifier(fn URIModifier) { uriModifier = fn @@ -41,7 +44,7 @@ func HTTPGetContext(ctx context.Context, uri string) ([]byte, error) { if err != nil { return nil, err } - response, err := http.DefaultClient.Do(request) + response, err := DefaultHTTPClient.Do(request) if err != nil { return nil, err } @@ -73,7 +76,7 @@ func HTTPPostContext(ctx context.Context, uri string, data []byte, header map[st request.Header.Set(key, value) } - response, err := http.DefaultClient.Do(request) + response, err := DefaultHTTPClient.Do(request) if err != nil { return nil, err } @@ -102,7 +105,7 @@ func PostJSONContext(ctx context.Context, uri string, obj interface{}) ([]byte, return nil, err } req.Header.Set("Content-Type", "application/json;charset=utf-8") - response, err := http.DefaultClient.Do(req) + response, err := DefaultHTTPClient.Do(req) if err != nil { return nil, err } @@ -129,7 +132,7 @@ func PostJSONWithRespContentType(uri string, obj interface{}) ([]byte, string, e return nil, "", err } - response, err := http.Post(uri, "application/json;charset=utf-8", jsonBuf) + response, err := DefaultHTTPClient.Post(uri, "application/json;charset=utf-8", jsonBuf) if err != nil { return nil, "", err } @@ -205,7 +208,7 @@ func PostMultipartForm(fields []MultipartFormField, uri string) (respBody []byte contentType := bodyWriter.FormDataContentType() bodyWriter.Close() - resp, e := http.Post(uri, contentType, bodyBuf) + resp, e := DefaultHTTPClient.Post(uri, contentType, bodyBuf) if e != nil { err = e return @@ -229,7 +232,7 @@ func PostXML(uri string, obj interface{}) ([]byte, error) { } body := bytes.NewBuffer(xmlData) - response, err := http.Post(uri, "application/xml;charset=utf-8", body) + response, err := DefaultHTTPClient.Post(uri, "application/xml;charset=utf-8", body) if err != nil { return nil, err } @@ -252,11 +255,10 @@ func httpWithTLS(rootCa, key string) (*http.Client, error) { config := &tls.Config{ Certificates: []tls.Certificate{cert}, } - tr := &http.Transport{ - TLSClientConfig: config, - DisableCompression: true, - } - client = &http.Client{Transport: tr} + trans := (DefaultHTTPClient.Transport.(*http.Transport)).Clone() + trans.TLSClientConfig = config + trans.DisableCompression = true + client = &http.Client{Transport: trans} return client, nil } diff --git a/wechat.go b/wechat.go index 29b86e0..dc2585f 100644 --- a/wechat.go +++ b/wechat.go @@ -1,6 +1,7 @@ package wechat import ( + "net/http" "os" log "github.com/sirupsen/logrus" @@ -14,6 +15,7 @@ import ( openConfig "github.com/silenceper/wechat/v2/openplatform/config" "github.com/silenceper/wechat/v2/pay" payConfig "github.com/silenceper/wechat/v2/pay/config" + "github.com/silenceper/wechat/v2/util" "github.com/silenceper/wechat/v2/work" workConfig "github.com/silenceper/wechat/v2/work/config" ) @@ -81,3 +83,8 @@ func (wc *Wechat) GetWork(cfg *workConfig.Config) *work.Work { } return work.NewWork(cfg) } + +// SetHTTPClient 设置HTTPClient +func (wc *Wechat) SetHTTPClient(client *http.Client) { + util.DefaultHTTPClient = client +} From 97c9f7d9083e98bde4c86c32f15607cd1e9185cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=B9=E6=99=B6?= Date: Tue, 2 Jan 2024 17:23:22 +0800 Subject: [PATCH 2/3] fix(work): fix 752 (#753) fix cannot unmarshal object into Go struct field ExternalUser.external_contact.external_profile of type string --- work/externalcontact/external_user.go | 60 ++++++++++++++++++++++----- 1 file changed, 50 insertions(+), 10 deletions(-) diff --git a/work/externalcontact/external_user.go b/work/externalcontact/external_user.go index 2c58d73..9ee219e 100644 --- a/work/externalcontact/external_user.go +++ b/work/externalcontact/external_user.go @@ -63,16 +63,16 @@ type ExternalUserDetailResponse struct { // ExternalUser 外部联系人 type ExternalUser struct { - ExternalUserID string `json:"external_userid"` - Name string `json:"name"` - Avatar string `json:"avatar"` - Type int64 `json:"type"` - Gender int64 `json:"gender"` - UnionID string `json:"unionid"` - Position string `json:"position"` - CorpName string `json:"corp_name"` - CorpFullName string `json:"corp_full_name"` - ExternalProfile string `json:"external_profile"` + ExternalUserID string `json:"external_userid"` + Name string `json:"name"` + Avatar string `json:"avatar"` + Type int64 `json:"type"` + Gender int64 `json:"gender"` + UnionID string `json:"unionid"` + Position string `json:"position"` + CorpName string `json:"corp_name"` + CorpFullName string `json:"corp_full_name"` + ExternalProfile *ExternalProfile `json:"external_profile,omitempty"` } // FollowUser 跟进用户(指企业内部用户) @@ -104,6 +104,46 @@ type WechatChannel struct { Source int `json:"source"` } +// ExternalProfile 外部联系人的自定义展示信息,可以有多个字段和多种类型,包括文本,网页和小程序 +type ExternalProfile struct { + ExternalCorpName string `json:"external_corp_name"` + WechatChannels WechatChannels `json:"wechat_channels"` + ExternalAttr []ExternalAttr `json:"external_attr"` +} + +// WechatChannels 视频号属性。须从企业绑定到企业微信的视频号中选择,可在“我的企业”页中查看绑定的视频号 +type WechatChannels struct { + Nickname string `json:"nickname"` + Status int `json:"status"` +} + +// ExternalAttr 属性列表,目前支持文本、网页、小程序三种类型 +type ExternalAttr struct { + Type int `json:"type"` + Name string `json:"name"` + Text *Text `json:"text,omitempty"` + Web *Web `json:"web,omitempty"` + MiniProgram *MiniProgram `json:"miniprogram,omitempty"` +} + +// Text 文本 +type Text struct { + Value string `json:"value"` +} + +// Web 网页 +type Web struct { + URL string `json:"url"` + Title string `json:"title"` +} + +// MiniProgram 小程序 +type MiniProgram struct { + AppID string `json:"appid"` + Pagepath string `json:"pagepath"` + Title string `json:"title"` +} + // GetExternalUserDetail 获取外部联系人详情 // @see https://developer.work.weixin.qq.com/document/path/92114 func (r *Client) GetExternalUserDetail(externalUserID string, nextCursor ...string) (*ExternalUserDetailResponse, error) { From 74e3e9c04e087500d005fb019dd3f667997cee64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=B9=E6=99=B6?= Date: Tue, 2 Jan 2024 17:24:17 +0800 Subject: [PATCH 3/3] feat(work): add member_version field in GroupChatListResponse (#757) add member_version field in GroupChatListResponse --- work/externalcontact/groupchat.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/work/externalcontact/groupchat.go b/work/externalcontact/groupchat.go index 018c397..3dc800f 100644 --- a/work/externalcontact/groupchat.go +++ b/work/externalcontact/groupchat.go @@ -76,13 +76,14 @@ type ( } //GroupChat 客户群详情 GroupChat struct { - ChatID string `json:"chat_id"` //客户群ID - Name string `json:"name"` //群名 - Owner string `json:"owner"` //群主ID - CreateTime int64 `json:"create_time"` //群的创建时间 - Notice string `json:"notice"` //群公告 - MemberList []GroupChatMember `json:"member_list"` //群成员列表 - AdminList []GroupChatAdmin `json:"admin_list"` //群管理员列表 + ChatID string `json:"chat_id"` //客户群ID + Name string `json:"name"` //群名 + Owner string `json:"owner"` //群主ID + CreateTime int64 `json:"create_time"` //群的创建时间 + Notice string `json:"notice"` //群公告 + MemberList []GroupChatMember `json:"member_list"` //群成员列表 + AdminList []GroupChatAdmin `json:"admin_list"` //群管理员列表 + MemberVersion string `json:"member_version"` //当前群成员版本号。可以配合客户群变更事件减少主动调用本接口的次数 } //GroupChatDetailResponse 客户群详情 返回值 GroupChatDetailResponse struct {