From 946f3be9ec76e66caec4dada8a04c144e19498fa Mon Sep 17 00:00:00 2001 From: ourines Date: Tue, 17 Oct 2023 16:57:22 +0800 Subject: [PATCH 1/4] feat(work): add department by id Refactor the DepartmentList function and add the DepartmentListByID function. --- doc/api/work.md | 2 ++ work/addresslist/department.go | 27 ++++++++++++++++++++++++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/doc/api/work.md b/doc/api/work.md index 3be65c7..445d6c0 100644 --- a/doc/api/work.md +++ b/doc/api/work.md @@ -90,10 +90,12 @@ host: https://qyapi.weixin.qq.com/ | 名称 | 请求方式 | URL | 是否已实现 | 使用方法 | 贡献者 | |:---------:|------|:----------------------------------------| ---------- | ------------------------------- |----------| | 获取子部门ID列表 | GET | /cgi-bin/department/simplelist | YES | (r *Client) DepartmentSimpleList| MARKWANG | +| 获取部门列表 | GET | /cgi-bin/department/list | YES | (r *Client) DepartmentList| just5325, ourines | | 获取部门成员 | GET | /cgi-bin/user/simplelist | YES | (r *Client) UserSimpleList | MARKWANG | | 获取成员ID列表 | Post | /cgi-bin/user/list_id | YES | (r *Client) UserListId | MARKWANG | + ## 素材管理 [官方文档](https://developer.work.weixin.qq.com/document/path/91054) diff --git a/work/addresslist/department.go b/work/addresslist/department.go index 3b41c38..0ce6409 100644 --- a/work/addresslist/department.go +++ b/work/addresslist/department.go @@ -12,7 +12,8 @@ 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" + departmentListURL = "https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token=%s" + departmentListByIDURL = "https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token=%s&id=%d" // departmentGetURL 获取单个部门详情 https://qyapi.weixin.qq.com/cgi-bin/department/get?access_token=ACCESS_TOKEN&id=ID departmentGetURL = "https://qyapi.weixin.qq.com/cgi-bin/department/get?access_token=%s&id=%d" ) @@ -106,19 +107,39 @@ func (r *Client) DepartmentSimpleList(departmentID int) ([]*DepartmentID, error) // DepartmentList 获取部门列表 // @desc https://developer.work.weixin.qq.com/document/path/90208 func (r *Client) DepartmentList() ([]*Department, error) { + return r.DepartmentListByID(0) +} + +// DepartmentListByID 获取部门列表 +// +// departmentID 部门id。获取指定部门及其下的子部门(以及子部门的子部门等等,递归) +// +// @desc https://developer.work.weixin.qq.com/document/path/90208 +func (r *Client) DepartmentListByID(departmentID int) ([]*Department, error) { + var formatUrl string + // 获取accessToken accessToken, err := r.GetAccessToken() if err != nil { return nil, err } + + if departmentID > 0 { + formatUrl = + fmt.Sprintf(departmentListByIDURL, accessToken, departmentID) + } else { + formatUrl = + fmt.Sprintf(departmentListURL, accessToken) + } + // 发起http请求 - response, err := util.HTTPGet(fmt.Sprintf(departmentListURL, accessToken)) + response, err := util.HTTPGet(formatUrl) if err != nil { return nil, err } // 按照结构体解析返回值 result := &DepartmentListResponse{} - err = util.DecodeWithError(response, result, "DepartmentList") + err = util.DecodeWithError(response, result, "DepartmentListByID") // 返回数据 return result.Department, err } From cf7bd13db373c9480f81cdca2f451179787e4d96 Mon Sep 17 00:00:00 2001 From: ourines Date: Tue, 17 Oct 2023 17:01:46 +0800 Subject: [PATCH 2/4] fix: api name --- work/addresslist/department.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/work/addresslist/department.go b/work/addresslist/department.go index 0ce6409..af38e31 100644 --- a/work/addresslist/department.go +++ b/work/addresslist/department.go @@ -139,7 +139,7 @@ func (r *Client) DepartmentListByID(departmentID int) ([]*Department, error) { } // 按照结构体解析返回值 result := &DepartmentListResponse{} - err = util.DecodeWithError(response, result, "DepartmentListByID") + err = util.DecodeWithError(response, result, "DepartmentList") // 返回数据 return result.Department, err } From 3e9c3ed3fc04bdff3951ba47929591024493cc0e Mon Sep 17 00:00:00 2001 From: ourines Date: Tue, 17 Oct 2023 17:05:36 +0800 Subject: [PATCH 3/4] fix: formatURL name --- work/addresslist/department.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/work/addresslist/department.go b/work/addresslist/department.go index af38e31..1074afc 100644 --- a/work/addresslist/department.go +++ b/work/addresslist/department.go @@ -116,7 +116,7 @@ func (r *Client) DepartmentList() ([]*Department, error) { // // @desc https://developer.work.weixin.qq.com/document/path/90208 func (r *Client) DepartmentListByID(departmentID int) ([]*Department, error) { - var formatUrl string + var formatURL string // 获取accessToken accessToken, err := r.GetAccessToken() @@ -125,15 +125,15 @@ func (r *Client) DepartmentListByID(departmentID int) ([]*Department, error) { } if departmentID > 0 { - formatUrl = + formatURL = fmt.Sprintf(departmentListByIDURL, accessToken, departmentID) } else { - formatUrl = + formatURL = fmt.Sprintf(departmentListURL, accessToken) } // 发起http请求 - response, err := util.HTTPGet(formatUrl) + response, err := util.HTTPGet(formatURL) if err != nil { return nil, err } From fa495ddf9590990697f87ce461b12a7c73ce16e6 Mon Sep 17 00:00:00 2001 From: ourines Date: Wed, 18 Oct 2023 12:06:18 +0800 Subject: [PATCH 4/4] chore: format --- work/addresslist/department.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/work/addresslist/department.go b/work/addresslist/department.go index 1074afc..b2feca0 100644 --- a/work/addresslist/department.go +++ b/work/addresslist/department.go @@ -125,11 +125,9 @@ func (r *Client) DepartmentListByID(departmentID int) ([]*Department, error) { } if departmentID > 0 { - formatURL = - fmt.Sprintf(departmentListByIDURL, accessToken, departmentID) + formatURL = fmt.Sprintf(departmentListByIDURL, accessToken, departmentID) } else { - formatURL = - fmt.Sprintf(departmentListURL, accessToken) + formatURL = fmt.Sprintf(departmentListURL, accessToken) } // 发起http请求