From cac3072199cb8f4017ecf1dcee49b151075a6c4e Mon Sep 17 00:00:00 2001 From: markwang <2951177317@qq.com> Date: Sun, 14 Aug 2022 19:54:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=81=E4=B8=9A=E5=BE=AE=E4=BF=A1-=E9=80=9A?= =?UTF-8?q?=E8=AE=AF=E5=BD=95=E7=AE=A1=E7=90=86=20(#601)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/api/work.md | 11 ++++++++ work/addresslist/client.go | 17 ++++++++++++ work/addresslist/department.go | 47 ++++++++++++++++++++++++++++++++ work/addresslist/user.go | 49 ++++++++++++++++++++++++++++++++++ work/work.go | 6 +++++ 5 files changed, 130 insertions(+) create mode 100644 work/addresslist/client.go create mode 100644 work/addresslist/department.go create mode 100644 work/addresslist/user.go diff --git a/doc/api/work.md b/doc/api/work.md index b046188..bdae0ed 100644 --- a/doc/api/work.md +++ b/doc/api/work.md @@ -68,7 +68,18 @@ host: https://qyapi.weixin.qq.com/ | 获取「群聊数据统计」数据 (按群主聚合的方式) | POST | /cgi-bin/externalcontact/groupchat/statistic | YES | (r *Client) GetGroupChatStat | MARKWANG | | 获取「群聊数据统计」数据 (按自然日聚合的方式) | POST | /cgi-bin/externalcontact/groupchat/statistic_group_by_day | YES | (r *Client) GetGroupChatStatByDay | MARKWANG | + +## 通讯录管理 +[官方文档](https://developer.work.weixin.qq.com/document/path/95350/90200) + +| 名称 | 请求方式 | URL | 是否已实现 | 使用方法 | 贡献者 | +|:---------:|------|:----------------------------------------| ---------- | ------------------------------- |----------| +| 获取子部门ID列表 | GET | /cgi-bin/department/simplelist | YES | (r *Client) DepartmentSimpleList| MARKWANG | +| 获取部门成员 | GET | /cgi-bin/user/simplelist | YES | (r *Client) UserSimpleList | MARKWANG | +======= + ## 应用管理 TODO + diff --git a/work/addresslist/client.go b/work/addresslist/client.go new file mode 100644 index 0000000..7b8cf4c --- /dev/null +++ b/work/addresslist/client.go @@ -0,0 +1,17 @@ +package addresslist + +import ( + "github.com/silenceper/wechat/v2/work/context" +) + +// Client 通讯录管理接口实例 +type Client struct { + *context.Context +} + +// NewClient 初始化实例 +func NewClient(ctx *context.Context) *Client { + return &Client{ + ctx, + } +} diff --git a/work/addresslist/department.go b/work/addresslist/department.go new file mode 100644 index 0000000..79a7e46 --- /dev/null +++ b/work/addresslist/department.go @@ -0,0 +1,47 @@ +package addresslist + +import ( + "fmt" + + "github.com/silenceper/wechat/v2/util" +) + +const ( + // DepartmentSimpleListURL 获取子部门ID列表 + DepartmentSimpleListURL = "https://qyapi.weixin.qq.com/cgi-bin/department/simplelist?access_token=%s&id=%d" +) + +type ( + // DepartmentSimpleListResponse 获取子部门ID列表响应 + DepartmentSimpleListResponse struct { + util.CommonError + DepartmentID []*DepartmentID `json:"department_id"` + } + // DepartmentID 子部门ID + DepartmentID struct { + ID int `json:"id"` + ParentID int `json:"parentid"` + Order int `json:"order"` + } +) + +// DepartmentSimpleList 获取子部门ID列表 +// see https://developer.work.weixin.qq.com/document/path/95350 +func (r *Client) DepartmentSimpleList(departmentID int) ([]*DepartmentID, error) { + var ( + accessToken string + err error + ) + if accessToken, err = r.GetAccessToken(); err != nil { + return nil, err + } + var response []byte + if response, err = util.HTTPGet(fmt.Sprintf(DepartmentSimpleListURL, accessToken, departmentID)); err != nil { + return nil, err + } + result := &DepartmentSimpleListResponse{} + if err = util.DecodeWithError(response, result, "DepartmentSimpleList"); err != nil { + return nil, err + } + return result.DepartmentID, nil +} diff --git a/work/addresslist/user.go b/work/addresslist/user.go new file mode 100644 index 0000000..2847a1c --- /dev/null +++ b/work/addresslist/user.go @@ -0,0 +1,49 @@ +package addresslist + +import ( + "fmt" + + "github.com/silenceper/wechat/v2/util" +) + +const ( + // UserSimpleListURL 获取部门成员 + UserSimpleListURL = "https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?access_token=%s&department_id=%d" +) + +type ( + // UserSimpleListResponse 获取部门成员响应 + UserSimpleListResponse struct { + util.CommonError + UserList []*UserList + } + // UserList 部门成员 + UserList struct { + UserID string `json:"userid"` + Name string `json:"name"` + Department []int `json:"department"` + OpenUserID string `json:"open_userid"` + } +) + +// UserSimpleList 获取部门成员 +// @see https://developer.work.weixin.qq.com/document/path/90200 +func (r *Client) UserSimpleList(departmentID int) ([]*UserList, error) { + var ( + accessToken string + err error + ) + if accessToken, err = r.GetAccessToken(); err != nil { + return nil, err + } + var response []byte + if response, err = util.HTTPGet(fmt.Sprintf(UserSimpleListURL, accessToken, departmentID)); err != nil { + return nil, err + } + result := &UserSimpleListResponse{} + err = util.DecodeWithError(response, result, "UserSimpleList") + if err != nil { + return nil, err + } + return result.UserList, nil +} diff --git a/work/work.go b/work/work.go index f9d2029..dd463bf 100644 --- a/work/work.go +++ b/work/work.go @@ -2,6 +2,7 @@ package work import ( "github.com/silenceper/wechat/v2/credential" + "github.com/silenceper/wechat/v2/work/addresslist" "github.com/silenceper/wechat/v2/work/config" "github.com/silenceper/wechat/v2/work/context" "github.com/silenceper/wechat/v2/work/externalcontact" @@ -49,3 +50,8 @@ func (wk *Work) GetKF() (*kf.Client, error) { func (wk *Work) GetExternalContact() *externalcontact.Client { return externalcontact.NewClient(wk.ctx) } + +// GetAddressList get address_list +func (wk *Work) GetAddressList() *addresslist.Client { + return addresslist.NewClient(wk.ctx) +}