ソースを参照

新增获取部门列表接口:https://qyapi.weixin.qq.com/cgi-bin/department/list (#698)

黄翠刚 2 年 前
コミット
c84eb7b550
1 ファイル変更39 行追加0 行削除
  1. 39 0
      work/addresslist/department.go

+ 39 - 0
work/addresslist/department.go

@@ -9,6 +9,8 @@ import (
 const (
 	// departmentSimpleListURL 获取子部门ID列表
 	departmentSimpleListURL = "https://qyapi.weixin.qq.com/cgi-bin/department/simplelist?access_token=%s&id=%d"
+	// departmentListURL 获取部门列表
+	departmentListURL = "https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token=%s"
 )
 
 type (
@@ -23,6 +25,21 @@ type (
 		ParentID int `json:"parentid"`
 		Order    int `json:"order"`
 	}
+
+	// DepartmentListResponse 获取部门列表响应
+	DepartmentListResponse struct {
+		util.CommonError
+		Department []*Department `json:"department"`
+	}
+	// Department 部门列表数据
+	Department struct {
+		ID               int      `json:"id"`                // 创建的部门id
+		Name             string   `json:"name"`              // 部门名称
+		NameEn           string   `json:"name_en"`           // 英文名称
+		DepartmentLeader []string `json:"department_leader"` // 部门负责人的UserID
+		ParentID         int      `json:"parentid"`          // 父部门id。根部门为1
+		Order            int      `json:"order"`             // 在父部门中的次序值。order值大的排序靠前
+	}
 )
 
 // DepartmentSimpleList 获取子部门ID列表
@@ -45,3 +62,25 @@ func (r *Client) DepartmentSimpleList(departmentID int) ([]*DepartmentID, error)
 	}
 	return result.DepartmentID, nil
 }
+
+// DepartmentList 获取部门列表
+// @desc https://developer.work.weixin.qq.com/document/path/90208
+func (r *Client) DepartmentList() ([]*Department, error) {
+	// 获取accessToken
+	accessToken, err := r.GetAccessToken()
+	if err != nil {
+		return nil, err
+	}
+	// 发起http请求
+	response, err := util.HTTPGet(fmt.Sprintf(departmentListURL, accessToken))
+	if err != nil {
+		return nil, err
+	}
+	// 按照结构体解析返回值
+	result := &DepartmentListResponse{}
+	if err = util.DecodeWithError(response, result, "DepartmentList"); err != nil {
+		return nil, err
+	}
+	// 返回数据
+	return result.Department, err
+}