Browse Source

企业微信-通讯录管理 (#601)

markwang 3 years ago
parent
commit
cac3072199
5 changed files with 130 additions and 0 deletions
  1. 11 0
      doc/api/work.md
  2. 17 0
      work/addresslist/client.go
  3. 47 0
      work/addresslist/department.go
  4. 49 0
      work/addresslist/user.go
  5. 6 0
      work/work.go

+ 11 - 0
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
 
+

+ 17 - 0
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,
+	}
+}

+ 47 - 0
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
+}

+ 49 - 0
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
+}

+ 6 - 0
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)
+}